Arduino EEPROM write() not working

I need to store a value of 0-100 into 12 EEPROM addresses but while working on the code I think a variable I was using was a LONG and it updated multiple addresses. I want to reset the EEPROM values to start again but for some reason I cannot get this to work.

#include <EEPROM.h>

#define NUM_PWM_CHNLS 12            // max 12 for TLC5971

void setup() {
  Serial.begin(9600);
  Serial.println("============================================================");
  Serial.println("====================== BEFORE UPDATE =======================");

  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    float f = 0.00f;   // Variable to store data read from EEPROM
    EEPROM.get( i, f );

    Serial.print("EEPROM f val read from address ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println( f, 3 );         // This may print 'ovf' or 'nan' if the data inside the EEPROM is not a valid float.
  }

  // Reset all EEPROM addresses to initial values
  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    EEPROM.write( i, 5.0 );
  }

  Serial.println("============================================================");
  Serial.println("======================= AFTER UPDATE =======================");

  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    float f = 0.00f;   // Variable to store data read from EEPROM
    EEPROM.get( i, f );

    Serial.print("EEPROM f val read from address ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println( f, 3 );         // This may print 'ovf' or 'nan' if the data inside the EEPROM is not a valid float.
  }
}

void loop() {
}
============================================================
====================== BEFORE UPDATE =======================
EEPROM f val read from address 0: 0.000
EEPROM f val read from address 1: 0.000
EEPROM f val read from address 2: 0.000
EEPROM f val read from address 3: 0.000
EEPROM f val read from address 4: 0.000
EEPROM f val read from address 5: 0.000
EEPROM f val read from address 6: 0.000
EEPROM f val read from address 7: 0.000
EEPROM f val read from address 8: 0.000
EEPROM f val read from address 9: 0.000
EEPROM f val read from address 10: ovf
EEPROM f val read from address 11: nan
============================================================
======================= AFTER UPDATE =======================
EEPROM f val read from address 0: 0.000
EEPROM f val read from address 1: 0.000
EEPROM f val read from address 2: 0.000
EEPROM f val read from address 3: 0.000
EEPROM f val read from address 4: 0.000
EEPROM f val read from address 5: 0.000
EEPROM f val read from address 6: 0.000
EEPROM f val read from address 7: 0.000
EEPROM f val read from address 8: 0.000
EEPROM f val read from address 9: 0.000
EEPROM f val read from address 10: ovf
EEPROM f val read from address 11: nan

I’m using a Pololu A-Star 328PB Micro, programming via the serial TTL.

Any ideas?

EEPROM.write() writes a byte to the specified address.

Use EEPROM.put() to store other types of variables.

Thanks Jim. I tried casting the variable as byte and that didn’t work. I then switched to using EEPROM.put() but it only changed one address, not all 12.

Please post the revised code.

#include <EEPROM.h>

#define NUM_PWM_CHNLS 12            // max 12 for TLC5971

void setup() {
  Serial.begin(9600);
  Serial.println("============================================================");
  Serial.println("====================== BEFORE UPDATE =======================");

  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    float f = 0.00f;   // Variable to store data read from EEPROM
    EEPROM.get( i, f );

    Serial.print("EEPROM f val read from address ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println( f, 3 );         // This may print 'ovf' or 'nan' if the data inside the EEPROM is not a valid float.
  }

  // Reset all EEPROM addresses to initial values
  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    EEPROM.put( i, 5 );
    delay(5);
  }

  Serial.println("============================================================");
  Serial.println("======================= AFTER UPDATE =======================");

  for (int i = 0; i < NUM_PWM_CHNLS; i++) {
    float f = 0.00f;   // Variable to store data read from EEPROM
    EEPROM.get( i, f );

    Serial.print("EEPROM f val read from address ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println( f, 3 );         // This may print 'ovf' or 'nan' if the data inside the EEPROM is not a valid float.
  }
}

void loop() {
}
============================================================
====================== BEFORE UPDATE =======================
EEPROM f val read from address 0: 0.000
EEPROM f val read from address 1: 0.000
EEPROM f val read from address 2: 0.000
EEPROM f val read from address 3: 0.000
EEPROM f val read from address 4: 0.000
EEPROM f val read from address 5: 0.000
EEPROM f val read from address 6: 0.000
EEPROM f val read from address 7: 0.000
EEPROM f val read from address 8: 0.000
EEPROM f val read from address 9: 0.000
EEPROM f val read from address 10: -0.000
EEPROM f val read from address 11: 5.000
============================================================
======================= AFTER UPDATE =======================
EEPROM f val read from address 0: 0.000
EEPROM f val read from address 1: 0.000
EEPROM f val read from address 2: 0.000
EEPROM f val read from address 3: 0.000
EEPROM f val read from address 4: 0.000
EEPROM f val read from address 5: 0.000
EEPROM f val read from address 6: 0.000
EEPROM f val read from address 7: 0.000
EEPROM f val read from address 8: 0.000
EEPROM f val read from address 9: 0.000
EEPROM f val read from address 10: -0.000
EEPROM f val read from address 11: 5.000

You are using put() to store the two byte integer “5” and expecting to read back a four byte float.

You really need to start paying attention.

That’s a bit harsh. The code I’m using for the get() portion is what I found when needling to read EEPROM which may or may not have a value, hence the checks for ovf and nan. And it works just fine on another project when needing to store a single value to address 0.

I’m very much open to suggestions, but not unhelpful comments like yours.

Please read, understand and follow the documentation of the functions you are using. It can be helpful to study the provided example(s).

EEPROM.put()

Specifically with put(), if you wish to store a float value, declare and store a float value.

Hi, RossW.

If I understand you correctly, you’re trying to “erase” the EEPROM by writing 5 to each address but then surprised that your program is still reading a float value of 0.000 from each address. Is that correct?

I’m not sure why you chose the value of 5, but I think you’re effectively filling the first few bytes of memory with 0x05, and then each float that you read (except for the last few) has a hexadecimal value of 0x05050505. That would be the IEEE 754 representation of the decimal 6.254552E-36, which the Arduino would round to 0.000 when printing it.

If you want to fill the EEPROM with a value that is not a valid float (NaN), you would probably be better off writing bytes of 0xFF instead (this also corresponds to a blank EEPROM, which typically has all bits set to 1).

Keep in mind that floats are 32 bits, and note that your reads overlap; if you want to store 12 floats, you would need 48 bytes of EEPROM.

Kevin

Thanks very much, Kevin. This is my first time working with EEPROM on an Arduino so your explanation goes a long way to clearing things up.

I initially wanted to use floats as it seemed easy to check for NaN which I could then interpret as not having been set yet. I missed the fact that a float takes up more than 1 byte which means I can’t iterate through memory addresses 0-12 to get the info. Ultimately, I think I want to store uint8_t in the 0-100 range but if I also read the addresses as uint8_t they return “0”, which is a valid stored value. Hence, my current plan is to seed the 0-12 memory address with “100” instead an update them as the user changes the value.