Storing float data in EEPROM

Hi all,

I have been reading a lot of posts and trying many different ideas for hours but I have not managed.

I want to store several float values in the EEPROM and read from them when the arduino is restarted. These values are the result of a calibration. I have simplified my code to show where the problem is:

#include <EEPROM.h>

struct MyObject {
  float field1;
  byte field2;
  char name[10];
};

float addressEEPROM;
float Constantdry1;
float Constanttotalmoist1 = 1;

void setup() {
}


void loop() {

//Part where the values are read to use the last calibration data:
addressEEPROM = 0;
Constantdry1 = EEPROM.read(addressEEPROM);//Reading variable from position 0. Reading does not use up cycles
addressEEPROM = addressEEPROM + sizeof(float);//Increase the address of the EEPROM so that a float can fit before. This way you can store a float and will not be partially overwritten
Constanttotalmoist1 = EEPROM.read(addressEEPROM);//Reading variable from position 0+X


//Part where the data is updated in the EEPROM (just after the calibration):

Constantdry1 = 588.00;
Constanttotalmoist1 = -2.89;

addressEEPROM = 0;
    EEPROM.get(addressEEPROM, Constantdry1);//Saving in position 0
    addressEEPROM = addressEEPROM + sizeof(float);//Increase the address of the EEPROM so that a float can fit before. This way you can store a float and will not be partially overwritten
    EEPROM.get(addressEEPROM, Constanttotalmoist1);//Saving in position after the size of a float
}

After this chunk of code, I do a check to see the values of each value saved in the EEPROM. The results are the following:

EEPROMConstantdry1 = 0 (instead of 582.00)
EEPROMConstanttotalmoist1 = 195 (instead of -2.89)

For this, I printed all values and I can confirm that the calibration values are the ones I mention and the only error resides in the storage of data in the EEPROM. This means, that after loading the arduino, the values of the constants are 0 and 195. If I use EEPROM.update (I know it can read only till 255) I get 70 and 254 instead.

Any help in the right direction would be appreciated.

Use put() and get() and you won't have to worry about the data type. You can even use it with your struct.

get() does not save though, it reads the data... so not sure about your comment there

    EEPROM.get(addressEEPROM, Constantdry1);//Saving in position 0

Hi J-M-L,

thank you for your answer. I was confusing the use of get (which is to read data and not to write).

I was able to solve the issue. Thank you again. I will put the subject as solved

Glad it helped !