EEPROM CRC value changing after reboot

Hello, intent for my sketch is to retrieve a stored crc value from EEPROM on boot-up and then check it against current crc value. I'm using Christopher Andrews' sketch as a base.

Issue is, when rebooting my Arduino pro mini, the current-state crc always differs from what was 'put' into EEPROM (savedcrc). The read crc data however does match the crc of the previous state crc.

Where am I going wrong?

#include <Arduino.h>
#include <EEPROM.h>

unsigned long CRC2;

struct MyObject {
  float field1;
  bool field2;
  char name[10];
  unsigned long CRC;
};

void setup() {

  Serial.begin(9600);

  
  // Display EEPROM crc
  Serial.print("\nCRC32 of EEPROM data: ");
  Serial.println(eeprom_crc(), HEX);

  
  // Get crc and display
  getEEPROM();

  
  // Check if current crc is the same as that stored in eeprom
  if(CRC2 == eeprom_crc()) Serial.println("\nYes");
  else Serial.println("\nNo");

  
  // Save updates to eeprom
  saveEEPROM();
}


void getEEPROM() {

  float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0; //EEPROM address to start reading from
  
  eeAddress = sizeof(float); //Move address to the next byte after float 'f'.

  MyObject customVar; //Variable to store custom object read from EEPROM.
  EEPROM.get(eeAddress, customVar);

  Serial.println("\nRead custom object from EEPROM: ");
  Serial.println(customVar.field1);
  Serial.println(customVar.field2);
  Serial.println(customVar.name);
  Serial.println(customVar.CRC, HEX);
}


void saveEEPROM() {

  float f = 0.00f;  //Variable to store in EEPROM.
  int eeAddress = 0;   //Location we want the data to be put.
  
  MyObject customVar = {
    123.86f,
    0,
    "TEST 888",
    eeprom_crc()
  };

  eeAddress += sizeof(float); //Move address to the next byte after float 'f'.

  EEPROM.put(eeAddress, customVar);
  Serial.print("\nData written: ");
  Serial.println(customVar.CRC, HEX);
}  


void loop() {
  /* Empty loop */
}


unsigned long eeprom_crc(void) {

  const unsigned long crc_table[16] = {
    0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  };

  unsigned long crc = ~0L;

  for (int index = 0 ; index < EEPROM.length()  ; ++index) {
    crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
    crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
    crc = ~crc;
  }
  return crc;
}

You calculate the CRC over the full eeprom. That includes the CRC field in your stuct as well. Let's assume that that CRC location was 0. After calculation, it's no longer 0 and that is what you save.

I would reserve the last 4 bytes of the eeprom for the CRC and calculate it over the eeprom length minus the size of the crc,

  unsigned long crc = ~0L;

  for (int index = 0 ; index < EEPROM.length() - sizeof(crc) ; ++index) {

And you calculate and save the CRC after you have saved the struct to eeprom.

Thank you for your prompt reply and clarifying the requirement to reserve some space for the eeprom.

If I understand your last comment correctly, does this mean I should:

  1. Remove the crc from the strut
  2. Calculate the crc after I've saved the strut
  3. Then save the crc to eeprom?

Yes to all three points.

Got it working. Thanks