Adding EEPROM.update causes if statement condition to become false regardless?

Hi, I am working on a project that requires the use of EEPROMand so I need a way to initialize the data if it is not already present. To do this I planned to use a signature by writing 2 values into EEPROM and initializing the data if the result an AND statement between the two was false. However, everything works fine until I try to initialize the values using EEPROM.update() with code like this:

  EEPROM.get(1,y);
  Serial.println(y);
  if ( y!=6){
    EEPROM.update(1,6);
    Serial.println("updated the thing");
    Serial.println(EEPROM.read(0));
  }
  

I make sure to reset my eeprom (making everything zero) beforehand using this:

  Serial.begin(9600);
   for (int i = 0 ; i < 512 ; i++) {
    EEPROM.update(i, 0);
  }

the condition evaluates to false but after adding EEPROM.update(1,6); it evaluates to false even if I change the value at address 1 back to 0. (This code is not what I'll use in my program but is basically what I'm going to do, the problem still persists).

Any help is appreciated

EEPROM.update() writes a single byte. If 'y' is an 'int' then EEPROM.get() will read TWO bytes. It is best not to mix EEPROM.read()/.write()/.update() with EEPROM.put()/.get()

Try replacing the EEPROM.update(1,6) with this:

  y = 6;
  EEPROM.put(1, y);

Also note that your 'EEPROM.update()' is writing to address 1 but your 'EEPROM.read()' is reading from address 0.

1 Like

Hi, thakns for your replay I changed it to the following but still get the same issue :frowning: ;

void setup() {
  int y;
  Serial.begin(9600);
  EEPROM.get(1,y);
  Serial.println(y);
  if ( y!=6){
    y=6;
    EEPROM.put(1,y);
    Serial.println("updated the thing");
  }
  
}

After I added enough code to make it a valid sketch it worked fine for me on an Arduino UNO.

NOTE the 'delay(200);' after Serial.begin(). When your sketch is uploaded it will run for about 150 milliseconds BEFORE the Serial Monitor connects and resets the board. Since this is plenty of time to write to EEPROM you won't see the messages from the first update unless you put in a delay.

#include <EEPROM.h>

void setup()
{
  Serial.begin(115200);
  delay(200);

  int y = 0;

  EEPROM.get(1, y);
  Serial.println(y);
  if (y != 6)
  {
    y += 1;
    EEPROM.put(1, y);
    Serial.println("updated the thing");
  }
}

void loop() {}

The output I got with EEPROM initialized to 0:

0
updated the thing
1
updated the thing
2
updated the thing
3
updated the thing
4
updated the thing
5
updated the thing
6
6
6

Each digit is after a hardware reset. When y==6 it stops updating, as expected.

1 Like

THANK YOU!! this gave me such a headache over the past few days but it works now :slight_smile:

It seems you have had some success since I started typing.

The (signed) int datatype is either 2 or 4 bytes depending on the platform.
You might just get away with reading somewhere in the middle of a int for the initial get(), but it is not clean.

1 Like

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