EEProm question

Guys, I have a question regarding something about eeprom storage. I am sure someone has done this before.

Let say you have a new board, on your program loadup you do a get() at whatever address you would normally write your data to. What is a good way to check and make sure that you do have data there before wasting your time trying to pull your entire values down?

I am not really looking for a code example, just some thoughts. I am sitting here trying to figure out the best way to do something like this but can't come up with anything good.

The other day I was googling around and I saw someone do something similar (if data, length) I only remember those few words. I wasn't interested in that at the time, I guess the though stayed in my mind and I am wondering about that idea now but dont know what is a reliable way for fault tolerance.

Any ideas??

What is a good way to check and make sure that you do have data there before wasting your time trying to pull your entire values down?

Precede the data something that will not be there as a random value, perhaps a string (lowercase s) and read that. If the string is what you expect then you are good to go

JohnRandalls: What is a good way to check and make sure that you do have data there before wasting your time trying to pull your entire values down?

There could be anything from 00 to 255 (0x00 - 0xFF) in the target EEPROM location. Is it your data? You can't answer unless you have written on paper -- On 15 January 2019, I wrote 13 (0x0D) at address 0x0100 of the EEPROM of ATmega328PChip of my Arduino UNO-1.'

Thanks guys, I will wite out a char value and check for that.

Thanks again.

JohnRandalls: Thanks guys, I will wite out a char value and check for that.

Thanks again.

I hope that you mean a C style string (a zero terminated array of chars) rather than "a char value" and that will put() its value on the EEPROM rather than write()ing it

UKHeliBob: I hope that you mean a C style string (a zero terminated array of chars) rather than "a char value" and that will put() its value on the EEPROM rather than write()ing it

Thanks for correcting me Bob.

I often put a ‘version’ number out there as well. When I update the code with extra features etc, I can check if the EEPROM mapping of data is the same as expected by the current code, otherwise warn the user and reinitialise the EEPROM to default values.

If you get clever, you can migrate versions automatically since you’re having prior knowledge of the old footprint.

If i use an ESP i actually start out with the chipdID and use the chipID as a terminator as well.

lastchancename:
I often put a ‘version’ number out there as well.

Is this how you are doing the version number? I played around with this but the load() code somehow never compares the value properly. I tried with char then string but it seems like the comparison is off. Yet, whenever I display the info in the serial window I can see the correct value (strange).

struct myRec
{
 //char version_no[5]; 
  String version_no;
}

//#define VERSION_NO "1.01"
String VERSION_NO = "1.01";
#define NUM_RECS 4
myRec aRec[NUM_RECS];
int eeAddress = 0;

void writeData()
{
  for(int x = 0; x < 4; ++x)
  {
     aRec[x].version_no = VERSION_NO;
     EEPROM.put(eeAddress, aRec[x]);
     eeAddress += sizeof(myRec);
  }
}


void loadData()
{
  myRec xRec[4];
  for(int cnt = 0; cnt < sizeof(xRec) / sizeof(xRec[0]); cnt++)
  {
    EEPROM.get(eeAddress, xRec[cnt]);
    if(xRec[cnt].version_no == VERSION_NO)
    {        
      Serial.print("versionNo: "); Serial.println(xRec[cnt].version_no);
      Serial.println("");
      eeAddress += sizeof(myRec);
    }
}

You were on the right track with c-strings char arrays.
Compare c-strings with strcmp(), and

for(int cnt = 0; cnt < sizeof(xRec) / sizeof(xRec[0]); cnt++)
  {
    EEPROM.get(eeAddress, xRec[cnt]);

is unnecessary.
The put & get methods look after the whole struct/type, you don’t have to iterate through the sizeof() nonsense.

lastchancename:
You were on the right track with c-strings char arrays.
Compare c-strings with strcmp(), and

for(int cnt = 0; cnt < sizeof(xRec) / sizeof(xRec[0]); cnt++)

{
    EEPROM.get(eeAddress, xRec[cnt]);


is unnecessary.
The put & get methods look after the whole struct/type, you don’t have to iterate through the sizeof() nonsense.

Appreciate it…