How can I read the data stored in EEPROM although is powered OFF.

I need help regarding on EEPROM library. Based on the code below, for every 2 sec, I want to store the int for increase where it will keep increasing by 1. Then, I upload to my Arduino Nano and open Serial Monitor to let it run for about 20 seconds. The serial monitor shows that Increase Count:10
.
Then I unplug the Arduino Nano, closed the Serial Monitor, wait for 5 sec, then plug back to computer and turn on the Serial Monitor, then the Serial Monitor start to shows Increase Count:1
, which indicated it start to recount again from 1,2,3,4.....
I thought that EEPROM will store the last number it counted which is 10, then when I unplug and plug back it should continue to count from 10, 11, 12, .......
So I would like to ask community whether my execution code and the method is correct or not. If not, can you advice me on how I can execute it correctly. Thank you in advance!

#include <EEPROM.h>

int addr=5;
int storagedata;
int increase;

void setup()
{
storagedata= EEPROM.read(addr);
Serial.begin(9600);

timevalue=millis();
}

void loop()
{
if (millis()-timevalue>=2000)
{
increase=increase+1;

EEPROM.update(addr,increase);
storagedata= EEPROM.read(addr);

Serial.print("Increase Count:");
Serial.println(storagedata);

timevalue=millis();
}
}

The first obvious issue is, you read the value from EEPROM but do not copy it to your counter variable.

Another is, you write your EEPROM every two seconds. That is 30/min, 1800/h, 43000/day. This will destroy our EEPROM in a very short amount of time. Check the datasheet for the number of writes your EEPROM supports.

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