# EEPROM technique

**URL:** <https://community.particle.io/t/eeprom-technique/29263>\
**Category:** Troubleshooting\
**Created:** [February 11, 2017, 7:58pm UTC](https://community.particle.io/t/eeprom-technique/29263 "2017-02-11T19:58:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enginerd](https://avatars.discourse-cdn.com/v4/letter/e/bcef8e/32.png) [@Enginerd](https://community.particle.io/u/Enginerd)\
**Post date:** [February 11, 2017, 7:58pm UTC](https://community.particle.io/t/eeprom-technique/29263/1 "2017-02-11T19:58:41Z")

</div>

Hi all,  
I’m trying to write an integer to EEPROM but have intermittent issues when I go to read it back. Below is the basic of what I’m trying to do:

```
int NextFeedTimeHr; // values from 1 to 23
int NextFeedTimeMin; // values 0 to 59

void setup(){
    EEPROM.get(1, NextFeedTimeHr);
    EEPROM.get(2, NextFeedTimeMin);
}

void loop(){
    //do stuff then go to Set_Feed_Time() to change next feed time.....(only happens twice a day)
}

void Set_Feed_Time() {
    EEPROM.put(1, NextFeedTimeHr);
    EEPROM.put(2, NextFeedTimeMin);
 }

```

Occasionally the value gets corrupted sometimes and i get a random large positive/negative number or it will not save to memory at all. Any suggestions?

FYI i just pull all power to the module and plug it back in to test to see if values will load back up correctly.

Thanks,  
-Jp-

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 11, 2017, 8:07pm UTC](https://community.particle.io/t/eeprom-technique/29263/2 "2017-02-11T20:07:17Z")

</div>

An `int` needs four bytes, but with your addresses `1` and `2` your integers are overlapping.

Either store your values in one-byte types like `uint8_t` or use addresses that allow for enough seperation like

```cpp
  EEPROM.put(0, intOne);
  EEPROM.put(4, intTwo);

```

Or use a `struct` and store that as a single entity.

---

<div class="post-metadata">

**Author:** ![jvanier](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jvanier/32/5542_2.png) [@jvanier](https://community.particle.io/u/jvanier)\
**Post date:** [February 13, 2017, 2:57pm UTC](https://community.particle.io/t/eeprom-technique/29263/3 "2017-02-13T14:57:16Z")

</div>

To expand on @ScruffR’s answer, I usually use a struct with a `version` field so I know when to initialize the storage.

```auto
const int currentVersion = 10;
struct {
  int version;
  int NextFeedTimeHr; // values from 1 to 23
  int NextFeedTimeMin; // values 0 to 59
} storage;

void setup() {
  EEPROM.get(0, storage);
  if (storage.version != currentVersion) {
    storage.version = currentVersion;
    storage.NextFeedTimeHr = 18;
    storage.NextFeedTimeMin = 0;
    saveStorage();
  }
}

void loop() {
  // do stuff and call saveStorage() to persist data
}

void saveStorage() {
  EEPROM.put(0, storage);
}

```

---

<div class="post-metadata">

**Author:** ![Enginerd](https://avatars.discourse-cdn.com/v4/letter/e/bcef8e/32.png) [@Enginerd](https://community.particle.io/u/Enginerd)\
**Post date:** [February 20, 2017, 12:26am UTC](https://community.particle.io/t/eeprom-technique/29263/4 "2017-02-20T00:26:08Z")

</div>

Thank you both for your replies!
