2

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

1
  • Yup, true. That is why I need some guidance. Thanks – Peterhan123 Mar 4 at 2:40
8

increase is a global variable and will implicitly initialized with the value zero. Then you are reading the EEPROM data into the variable storagedata in setup(). And then in loop() you are increasing increase from zero to one and write that to EEPROM. This value then gets read back into storagedata. So now increase and storagedata have the same value: one. So the counting begins on every reset at 1.

It is unclear, why you even have 2 variables for this. One would be enough (unless you want to test the EEPROM for some reason). In setup() read the data into increase. Then the counting starts with the number in EEPROM.

5
  • strange what some people consider as useful answer – Juraj Mar 4 at 14:53
  • 2
    @Juraj I'm not sure, what you are criticizing here. Can you explain that a bit more? Is my answer not good in some way? Or is it about the fact, that such an easy answer gets more upvotes, as more complex ones? (Which I also wonder about) – chrisl Mar 4 at 15:56
  • I wonder how is the answer useful for anybody else then OP – Juraj Mar 4 at 16:47
  • @Juraj About that we could have a full discussion. Here we often have question, from whose answers others will most likely not profit. We have the possibility to dismiss those questions and close them, or answer them to help. Or - if you have an answer to the question, which would also help others - you can add your own answer. – chrisl Mar 4 at 16:56
  • Hey there, thank you for highlighting my error and I learned so much from your reply. Is fixed and it works fine. – Peterhan123 Mar 5 at 3:13
5

The problem is that you fail to initialize increase during setup(). The variable is undefined when the code enters the loop() function, even regardless of whether this is the first boot or not. You should change storagedata to a local variable in loop() and use increase to keep the value (or vice versa).

0

You never use the value you read in setup(). Go through your loop() function one line at a time:

// this does not use the value read in setup, variables are initialized to 0
// so the first time through this loop it will set 'increase' to 1
increase=increase+1;

// you are updating EEPROM with the value of 'increase' which is 1 the first
// time this is executed
EEPROM.update(addr,increase);

// you are reading the '1' that was just written into 'storagedata', you have
// never used the value you read in 'setup()' and have now overwritten it
storagedata=EEPROM.read(addr);

I'm not sure why you have two separate variables, but in setup() you just need to initialize increase as the value read from EEPROM:

storagedata=EEPROM.read(addr);
increase=storagedata; // start with stored value instead of default 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.