EEPROM unexpected behaviour

I am storing 4 float values from an array in EEPROM

        EEPROM.put(1, hours[0]);
        EEPROM.put(2, hours[1]);
        EEPROM.put(3, hours[2]);
        EEPROM.put(4, hours[3]);

when I retrieve the values after restarts

        EEPROM.get(1, hours[0]);
        EEPROM.get(2, hours[1]);
        EEPROM.get(3, hours[2]);
        EEPROM.get(4, hours[3]);

I get unexpected values

e.g.
when saving values were
10.300000, 15.300000, 16.450001, 18.150000

when module restarts
I get following values from EEPROM
0.000000, 0.000000, -0.000000, 18.150000

it should be original values which I stored

You are overlapping your floats consequently corrupting the already stored data of 0, 1 & 2.
A float takes 4 bytes but you store them in 1 byte increments.

You can use sizeof(float) to store the individual values but Iā€™d rather store the entire array as one entity.

2 Likes

do you mean I should use something like this? non consecutive index

    EEPROM.put(1, hours[0]);
    EEPROM.put(5, hours[1]);
    EEPROM.put(9, hours[2]);
    EEPROM.put(13, hours[3]);

    EEPROM.get(1, hours[0]);
    EEPROM.get(5, hours[1]);
    EEPROM.get(9, hours[2]);
    EEPROM.get(13, hours[3]);

Exactly that. The first parameter is not an index but the byte offset from the EEPROM base address.

so if I store this way, it will work:

    EEPROM.put(sizeof(float)*1, hours[0]);
    EEPROM.put(sizeof(float)*2, hours[1]);
    EEPROM.put(sizeof(float)*3, hours[2]);
    EEPROM.put(sizeof(float)*4, hours[3]);

Yes, or just EEPROM.put(1, hours) which stores the entire array as one entity :wink:

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.