Maybe this is of interest.
The EEPROM function let you write or read an array which persists power loss or the deep_sleep command. The array is 100 byte wide. You can store real vars like int or float and others too. The trick is to use the C directive UNION to combine two data structure as one. It means two declared data structures share the same space in memory. How it goes?
First declare var data structure
struct structEEPROM {
int intValue; // integer var
float floatValue; // float var
char charValue; // char var
};
Then declare UNION adding a char[array] with the size of structEEPROM and give it a name, here EEPROMData
union {
structEEPROM eevar;
char eeArray[sizeof(structEEPROM)];
} EEPROMData;
Only two function are needed to exchange all data
void readEEPROM(void) {
for (int i=0; i<sizeof(structEEPROM); i++) {
EEPROMData.eeArray[i] = EEPROM.read(i);
}
}
void writeEEPROM(void) {
for (int i=0; i<sizeof(structEEPROM); i++) {
EEPROM.write(i, EEPROMData.eeArray[i]);
}
}
Read the EEPROM during setup()
void setup() {
readEEPROM();
// your other code
}
Save the data to EEPROM whenever you want
loop() {
// your code
writeEEPROM();
}
How to handle data in EEPROM?
//write data to vars
EEPROMData.eevar.intValue = 1337;
EEPROMData.eevar.floatValue = 3.14159;
EEPROMData.eevar.charValue = 13; //cr
//read data from vars
int Value1 = EEPROMData.eevar.intValue;
float Value2 = EEPROMData.eevar.floatValue;
char Value3 = EEPROMData.eevar.charValue;