Arduino/Genuino Language Syntax

EEPROM

Storing values in the EEPROM enables you to preserve them between runs. The most useful functions are EEPROM.put(eepromAddress, value) and EEPROM.get(eepromAddress, variable). The values saved and retrieved can be of any type but you have to keep track of the location on the EEPROM where the value resides. The best way is to use the sizeof() function. Start with a pointer set at EEPROM address 0 and increment/decrement it as you go, e.g.

int eepromAddress = 0;
int i = 1;
float f = 1.2;
byte b = 0x02;
void setup(){
  EEPROM.put(eepromAddress, i);  // write an int value
  eepromAddress += sizeof(int);  //  increment by the length of an int in memory
  EEPROM.put(eepromAddress, f);  // write a float value
  eepromAddress += sizeof(float);  // increment by the length of a float in memory
  EEPROM.put(eepromAddress, b);  // write a byte value
}

To get the values back later…

int eepromAddress = 0;
int i;
float f;
byte b;
void setup(){
  EEPROM.get(eepromAddress, i);  // read an int value
  eepromAddress += sizeof(int);  //  increment by the length of an int in memory
  EEPROM.get(eepromAddress, f);  // read a float value
  eepromAddress += sizeof(float);  // increment by the length of a float in memory
  EEPROM.get(eepromAddress, b);  // read a byte value
}