Reset EEPROM Not Working

I’m trying to create a sketch to store values in the range 0-100 to EEPROM, and while mucking about I think I used LONG variables which updated multiple EEPROM addresses. To get back to something useful, I want to reset the 12 addresses to something non-zero as follows:

#include <EEPROM.h>

#define NUM_PWM_CHNLS 12            // max 12 for TLC5971

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

  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 );
  }

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

  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() {
}

But it doesn’t work! Here’s the serial monitor output:

============================================================
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
============================================================
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

What am I doing wrong?

if you store a float (4 bytes) at address 0, you will end up occupying bytes at address 0,1,2 and 3. so the next read or write should be at address 4 (ie increment by sizeof(float))

use put() to minimize wear-out and handle any type (EEPROM.write(address, value) will only write 1 byte).

Assume that we have executed the following codes to write float type data into the inetrnal EEPROM of ATmega328P MCU of UNO.

float x = 3.70;
EEPROM.put(0, x);   //data storage begins from location 0 and ends up at location 3 (see Fig-1).

float y=11.75;
EEPROM.put(4, y); //data storage begins from location 4 and ends up at location 7 (see Fig-1).

Then, the storage pattern of the given data (x and y) in the EEPROM would be as depicted in Fig-1; where, each float type number has occupied four consecutive memory locations after being converted into 4-byte data (32-bit binray32/IEEE-754 formatted data as per template of Fig-2). Edit: Lower byte gets stored into lower location (the little endian)
float.png
Figure-1:


Figure-2:

float.png

@golam

Any change to take into account when you have a big endian Architecture versus a little endian architecture (since you want to be very detailed).

Thanks J-M-L & GolamMostafa - that explains the issue.

J-M-L - in all my EEPROM code I use get() & put() but tried using write() in this test sketch to see if there was any difference. The goal is to store unsigned values in the range 0-100 so I've switched to using uint8_t. It seems you need to read & write KNOWING what values are stored (or will be stored) in EEPROM - I had thought that by using float for the get() I could tell if the address had ever been updated (since originally I only wanted to save a value when the user changes it from an initial value). But if I read blank EEPROM addresses using uint8_t. I get mostly zeros which is a valid user setting, so instead I'm going to "seed" the 12 EEPROM addresses with the initial value of uint8_t 100.

Not sure what would change the address automatically for you, you are not dealing with typed pointers where adding one goes to the right next memory slot, you are dealing with integrals for your address so when you say +1, that’s what the compiler does