My arduino's EEPROM loses data when I reset or when I turn off the board

here is my problem, at the beginning of the code I defined default values in the EEPROM (0).
During the execution of the program with the help of a push button I increase or I decrease these values.
When the board is on and I press the pushbutton it works, the value changes and I can see it in the serial monitor.
as soon as I reset or when I turn off the card,
it is the default value (0) at the start defined which is in the EEPROM.
please help me i leave the code below.

#include <EEPROM.h>
#include <Wire.h>

int upButton = 10;
int downButton = 11;
int selectButton = 12;
int i=1;

void setup() {

Serial.begin(9600);
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);

// janvier
for(int i=0;i<=31;i++){ int jan = 0;
EEPROM.put(i,jan); }

}
void loop() {

int valeur = EEPROM.read(10);
Serial.print(valeur);
if (!digitalRead(downButton)){
Serial.println(“buttom is push”);
EEPROM.update(10,i);
i++;
while (!digitalRead(downButton));
delay(10);
}

Serial.println(" EEPROM.read");
delay(300);

}

How do you determine this? The very first thing your program will do after a rest or a power cycle is to put 0 in the first 31 addresses of EEPROM. You write before you ever read.

Hello
in the setup() you overwrite the contents of the EEPROM.

1 Like

Also be aware that EEPROM.update only writes a single byte. In loop(), the variable i is an int; use EEPROM.put instead.

Please edit your post, select all code and click the </> button above the reply text box. This will put your code in a scrollable window which is easier to read and easier to copy for analysis on a computer.

good evening thank you for your intervention.
you are right, I completely forgot that the setup is executed at each reset or power up.
I will review the code to write once to the EEPROM and see the result.
Thank you

ok i will correct that to see the result.
thank you

good evening
ok i will try taking into account what you said.
and for the form of my message sorry this is my first time. i didnt know how to do it so thanks for the tip.
thank you

hi, i made some modification in the code and it works now, but
I noticed a small problem; on my nano board which I use for the tests, after switching off and on again, the last saved value is increased by 1 or sometimes by 2.
i tried on a uno and it works fine.
the problem comes from nano board or my code?

#include <EEPROM.h>
#include <Wire.h> 

int upButton = 10;
int downButton = 11;
int selectButton = 12;
int i=1;

void setup() {

  Serial.begin(9600);
  pinMode(upButton, INPUT_PULLUP);
  pinMode(downButton, INPUT_PULLUP);
  pinMode(selectButton, INPUT_PULLUP);

   // janvier
// for(int i=0;i<=31;i++){ int jan = 0;
//   EEPROM.put(i,jan); }
  
}
void loop() {
  
   int valeur = EEPROM.read(0); 
   Serial.println(valeur);
   if (!digitalRead(downButton)){ 
     Serial.println(" buttom is push");
     i = EEPROM.read(0)+1; 
     EEPROM.put(0,i);
     i++;
    while (!digitalRead(downButton));
    delay(10);
  } 
 
  
    Serial.println(" EEPROM.read");
    delay(200);

}

Are you saying that without pushing any buttons valeur is different on the first pass of loop that what it was when the power was cut?

int valeur = EEPROM.read(0); 
   Serial.println(valeur);

What happens if you add a delay(1000) at the end of setup()?

1 Like

Yes that's it. but by adding delay(1000) at the end of setup as you suggested, everything is correct
Thanks for your help !