Here is a basic example
upload that code to your arduino with serial console set à 115200 bauds
the way this code works is in the setup (there is no loop)
it reads the EEPROM starting from address 0 (eepromStartAddress in the code) and stores whatever was read in a struct composed of 3 elements
- a flag (sentinel)
- a long number
- a char buffer
If that flag sentinel is the magic code 0xBADCAFE
then it means you can trust the data in EEPROM to have been set (based on the likelihood that the first 4 bytes of memory would be set at this value randomly is very very very low)
So the code tests the sentinel flag against our magic key.
--> if it's OK then the code proceed after telling you what it read in memory.
--> if it's not OK, then it means this is the first time you use that Arduino (or data was corrupted), so you initialize the parameters to some default values and store them for next time in EEPROM.
so the first time you run this program the console will say
Data in EEPROM was not OK, setting default Value :
Saved Data
Parameters are:
IR Count = 12345
Message = Hello World
every time you press reset now on your Arduino you'll see
[color=green]Data OK in EEPROM:
Parameters are:
IR Count = 12345
Message = Hello World
[/color]
showing that the magic key was recognized
if you change the values in the Parameters structure and call the function storeParametersInEEPROM() then your new values will become the default for next time (don't do that in the loop without control otherwise you'll kill your 100,000 cycles quickly. Only write in EEPROM when user has changed the params you want to save)
I added at the end of the program (commented out) a few lines to empty the EEPROM (write a wrong magic key) so that if you want to be back in a state where the memory is not OK, just uncomment that part and run the program.
hope this helps
#include <EEPROM.h>
const unsigned long magicKey = 0xBADCAFE; // likelyhood of this being in EEPROM is very low
struct savedInEEPROM
{
unsigned long sentinel; // will be our magicKey if memory already set otherwise don't assume it's correct
unsigned long IRCount;
char message[20]; // enough space to store what you need. Careful don't overflow!
} parameters;
unsigned int eepromStartAddress = 0; //EEPROM address to start reading from
void storeParametersInEEPROM()
{
parameters.sentinel = magicKey; // ensure we have the magic key set up
EEPROM.put(eepromStartAddress, parameters); // save the current parameters
Serial.println("Saved Data"); // notify end user it's done
}
void setup() {
Serial.begin(115200);
// Read EEPROM memory to initialize our parameters
EEPROM.get(eepromStartAddress, parameters);
// verify if we read garbage or good data
if (parameters.sentinel == magicKey) {
// we fond the magic key, we can assume that the rest of the data is OK
Serial.println(F("Data OK in EEPROM: "));
} else {
// data in memory is likely not good, initialize our data structure with defaut value
// or ask the end user to enter a value from the keypad
Serial.println(F("Data in EEPROM was not OK, setting default Value :"));
// define the default values you want the first time
parameters.IRCount = 12345l;
strcpy(parameters.message, "Hello World"); // ensure it fits in the buffer, including the trailing '\0'
// and store it in memory for next time
storeParametersInEEPROM();
}
Serial.println(F("Parameters are: "));
Serial.print(F("IR Count = ")); Serial.println(parameters.IRCount);
Serial.print(F("Message = ")); Serial.println(parameters.message);
// -------------------------------------------
// uncomment those lines if you want to leave the memory in a state where the sentinel is wrong
// put garbage in EEPROM at start address for the sentinel and empty the rest
// -------------------------------------------
// -------------------------------------------
// parameters.sentinel = 0xDEADBEEF; // wrong sentinel
// parameters.IRCount = 0l;
// memset (parameters.message, 0, sizeof(parameters.message)); // http://www.cplusplus.com/reference/cstring/memset/?kw=memset
// EEPROM.put(eepromStartAddress, parameters);
// -------------------------------------------
// -------------------------------------------
}
void loop() {}