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

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