J-M-L:
Here is a quick example showing how I deal with this#include <EEPROM.h>
// attribute ((packed)) is to force the compiler to use the minimum required memory to represent the type
// see Using the GNU Compiler Collection (GCC)
struct attribute ((packed)) _paramS {
int16_t minimumT;
int16_t maximumT;
} myParamters;
const uint32_t keyword = 0xDEADBEEF;
const uint16_t keywordAddress = 0x00;
const uint16_t paramAddress = keywordAddress + sizeof(keyword);
void printParam()
{
Serial.println(F("\n************* PARAMS *************"));
Serial.print(F("Temp min =\t")); Serial.println(myParamters.minimumT);
Serial.print(F("Temp max =\t")); Serial.println(myParamters.maximumT);
}
void saveParam()
{
EEPROM.put(keywordAddress, keyword);
EEPROM.put(paramAddress, myParamters);
}
void getParam()
{
uint32_t tmpKey;
EEPROM.get(keywordAddress, tmpKey);
if (tmpKey == keyword) {
EEPROM.get(paramAddress, myParamters); // EEPROM was already initialized OK to read
} else {
// First run on this arduino, memory was never initialized. so establish default values
myParamters.minimumT = -22;
myParamters.maximumT = 44;
saveParam();
}
}
void setup() {
Serial.begin(115200);
getParam(); // first run will establish the defaults in EEPROM
printParam(); // see what the values are
myParamters.minimumT = -55; // change one of the parameters
saveParam(); // save it
printParam(); // show it has been saved
}
void loop() {}
all the values I want to save are in the `myParamters` structure. in the setup() you call the `getParam()` function which will either read what is already there in EEPROM if the memory was already initialized or will write there default values and initialize the structure every time you change the structure (one of your parameter) you just call `saveParam()` to update the EEPROM. --> run the program with your serial console opened at 115200 bauds. first time you run the program you'll see that default values are written, then the `minimumT` param is updated to -55° and if you run the code a second time (close and re-open the serial console for example) you'll see that the second time the `minimumT` param will already be at -55° instead of -22°
i ran your example code but both times it printed the same thing on the console. can you show me an example with an unsigned long please?