Write/read data from EEPROM

Hey, I use an Arduino Mega and want to safe and read some float values into EEPROM. I never used it and saw that it’s not that trivial, may somebody help me? Cheers!

Actually it is trivial using EEPROM.put() and EEPROM.get(): https://www.arduino.cc/en/Reference/EEPROMPut https://www.arduino.cc/en/Reference/EEPROMPut The only tricky part is you need to remember that each float is 4 bytes so you need to allocate that much space in the EEPROM for each float you write. For example, if you write a float to EEPROM address 0 and then write another float to EEPROM address 1 you just overwrote 3 bytes of the first float and when you read it you'll find it's not the value you saved. Instead you need to save the second float to EEPROM address 4, leaving 4 bytes allocated for the first float.

Thanks man, it works!!!!

pert: Actually it is trivial using EEPROM.put() and EEPROM.get(): https://www.arduino.cc/en/Reference/EEPROMPut https://www.arduino.cc/en/Reference/EEPROMPut The only tricky part is you need to remember that each float is 4 bytes so you need to allocate that much space in the EEPROM for each float you write. For example, if you write a float to EEPROM address 0 and then write another float to EEPROM address 1 you just overwrote 3 bytes of the first float and when you read it you'll find it's not the value you saved. Instead you need to save the second float to EEPROM address 4, leaving 4 bytes allocated for the first float.

or you could just store a single struct of all your eeprom values and don't worry about the addresses:

struct StoredValues{
  float someValue;
  float someOtherValue;
  float yetAnotherValue;
}storedValues;
...

...
EEPROM.get(storedValues, 0);
storedValues.someValue = 3.14159;
EEPROM.put(0, storedValues);

You are likely to see configuration and startup settings done this way.