Save Settings to EEProm not Working

I'm having a problem saving my setting to eeprom on a Arduino Mega 2560. My code is suppose to save the changes on exiting the menu. I've based my code off of the following example: replaced bad link; Arduino Playground - EEPROMLoadAndSaveSettings

Code Snippet:
#define CONFIG_VERSION "ls1"
#define CONFIG_START 32
struct StoreStruct {
char version[4];
int a, b, c, d;
}
storage = {
CONFIG_VERSION,
// The default values
2500, 1, 3, 60
};

void loadConfig() {
if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
for (unsigned int t=0; t<sizeof(storage); t++)
((char)&storage + t) = EEPROM.read(CONFIG_START + t);
}

void saveConfig() {
for (unsigned int t=0; t<sizeof(storage); t++)
EEPROM.write(CONFIG_START + t, ((char)&storage + t));
}

On a different tab I have my button commands & menu option. On exit of the menu if any of my values changed I initiate a save. I appears to work, the LCD displays "Saving Changes..." for 1.5 seconds but after a restart or power cycle they go back to the default values of (2500, 1, 3, 60). If I change my default values the changes are displayed in my menu options so the code is reading them.

Exit Menu Code:
else if (menuText1 == "Exit |GovDv + | -")
{ //On Exit Menu if vals changed save config
if (valChanged) {
lcdUpdate(0, 3, "Saving Changes...", true);
storage.a = maxrpm;
storage.b = lowAdj;
storage.c = highAdj;
storage.d = rpmDif;
saveConfig();
delay(1500);
}

I removed the link in the original post, it was the wrong one, sorry. Here's the correct link for the code example I've been working from: Arduino Playground - EEPROMLoadAndSaveSettings

I have tried the second example and added a LCD statement under the "//error writing to EEPROM" section and it is in fact throwing an error but I don't know why.

void saveConfig() {
for (unsigned int t=0; t<sizeof(settings); t++)
{ // writes to EEPROM
EEPROM.write(CONFIG_START + t, ((char)&settings + t));
// and verifies the data
if (EEPROM.read(CONFIG_START + t) != ((char)&settings + t))
{
// error writing to EEPROM
lcdUpdate(0, 3, "Error saving EEPROM!, true);
}
}
}

I have tried the second example and added a LCD statement under the "//error writing to EEPROM" section and it is in fact throwing an error but I don't know why.

"it is in fact..." - And "it" is?

"throwing an error" - I don't guess that the exact error is important...

"but I don't know why." - You are not the only one.

I have continued researching this issue and found an alternative that's working for the most part which I got from a AVR site. I'm still working on a way to set defaults if no values are found in eeprom but I'm able to read values, change them on my LCD menu and write them to eeprom with this method. Definitely more code writing for this solution but hey at least it works... :slight_smile:

#include <EEPROM.h> // LoadAndSaveSettings
#include <avr/eeprom.h>
struct settings_t
{
int a;
int b;
int c;
int d;
}
settings;

void setup(){
eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
maxrpm = settings.a;
lowAdj = settings.b;
highAdj = settings.c;
rpmDif = settings.d;
if (maxrpm == -1 || lowAdj == -1 || highAdj == -1 || rpmDif == -1){ // Check if settings are blank, don't know it this works yet.
maxrpm = 1500; // if settings are blank set and save default values
lowAdj = 1;
highAdj = 3;
rpmDif = 60;
settings.a = maxrpm;
settings.b = lowAdj;
settings.c = highAdj;
settings.d = rpmDif;
eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
}
}

// Function I can call to verify my values and post results to LCD
void verifySave(){
oldmaxrpm = maxrpm;
oldlowAdj = lowAdj;
oldhighAdj = highAdj;
oldrpmDif = rpmDif;
eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
maxrpm = settings.a;
lowAdj = settings.b;
highAdj = settings.c;
rpmDif = settings.d;
if (maxrpm != oldmaxrpm || lowAdj != oldlowAdj || highAdj != oldhighAdj || rpmDif != oldrpmDif){
// Error saving to EEPROM
saveResults = "Error Save Settings!";
}
else{
// Settings saved...
saveResults = "Settings Saved...";
}
}