简体   繁体   中英

Arduino EEPROM getting corrupt values

I'm using Arduino Uno to save several variables into the EEPROM. My solution to this is to create a custom struct in which to store all the variables needed then use EEPROM.put to store them into the built-in EEPROM.

My problem arises not when putting data, but when getting data. Whenever I would change something in my code and/or use the Arduino in a short amount of time, the data stored in the EEPROM gets corrupted.

I check the data through Serial.println() function. From time to time the String data get corrupted but not the other data types as far as I can remember. Maybe an issue with String data?

struct EEPROMDATA
{
    String customMessage, emergencyMessage;
    String emergencyContact[3];
    String ownerContact;
    String idleMessage;
    int travelThreshold;
    int idleThreshold;
    char password[6];
    location locationList[3];
};

EEPROMDATA eepromstruct;

void loadReset() 
{
    EEPROM.get(100, eepromstruct);
}

void saveReset()
{
    EEPROM.put(100, eepromstruct);
}

These are the functions I use to save (put) and load (get) the data from the EEPROM. I've read somewhere to avoid writing data at the 0th address that's why the address is set to 100.

Do remember that this is not a writing to EEPROM issue as my program currently doesn't have any references to EEPROM.put nor to saveReset(). Somehow during the course of the program the EEPROM data gets corrupted. I don't know if this is a hardware or a software problem.

EDIT: I forgot to note that when troubleshooting this, whenever a corruption occurs, I re-initialize then save the data in the EEPROM again. After that it works fine for a short while (turning on/off continuously then testing the data, resetting the device, etc.) until it corrupts again.

You need to use char[] instead of using String which is a class object when you declare at some point in your code. The object (an instance of the class) is supposed to refer the other area of the memory in case of dynamic memory allocation to store its data by its methods. Even though you're assuming that your data would be stored in EEPROM, but actually, it was stored in some area of RAM. That's why you have corrupted data only with String data.

First of all, if you change the definition of class EEPROMDATA, this will "corrupt" your eeprom data. Not in the sense that the data in the eeprom is somehow changed, but in the sense that you're trying to read data written in an old format into a new format.

Next, strings are variable size char arrays. Structs assume a static memory layout. The String class achieves variable length storage through indirection (pointers, essentially), which cannot be trivially translated from RAM to EEPROM.

You need to decide whether you want static layout in EEPROM (in which case you have to stop messing with the struct, and dedicate a specific number of chars to your strings) or dynamic (in which case you need to write an "intelligent" function to parse the data into/out of EEPROM)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM