Categories
Arduino Ask Engineering

How can I read the data stored in EEPROM although is powered OFF based on the simple code I wrote?

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

Leave a Reply

Your email address will not be published. Required fields are marked *