EEPROM.get &.put unexpected results

I have a simple program that demonstrates unexpected results while using the EEPROM.get and .put commands. I read a location from EEPROM and then write a second variable back to that location in EEPROM. What's weird is that the value read in with .get is not the value in EEPROM, but the value that will be written out with the .put in a later line. (UNO's are cool, but I didn't expect them to be capable of precognition!).
In the attached example, the initial EEPROM value is 99, as confirmed by the output from another program that does a .get and prints the value.
This program .get's the EEPROM value into variable "memory", but in this example the value of "memory" is 88 (the value in the second variable) not the expected 99. from EEPROM. I've tried multiple variation but get the same result.
Here is the code used:

#include <EEPROM.h>

int Loc = 0;           
long memory;
long output = 55; 
// Start setup
void setup() {

  Serial.begin(9600); 
  while (! Serial);         
  Serial.print("Test update ");
  Serial.println();           
  Serial.print("0.output: ");
  Serial.println(output);
  Serial.print("0.memory: ");
  Serial.println(memory);

  Serial.println("read EEPROM");
  EEPROM.get(Loc, memory);

  Serial.print("1.output: ");
  Serial.println(output);
  Serial.print("1.memory: ");
  Serial.println(memory);
  EEPROM.put(Loc,output);
  Serial.println("EEPROM Data updated ");

}   //end of setup

void loop() {
}  //  end void loop

here is the output on the Serial Monitor
from the program that prints the EEPROM value:

image

and the output from this program
image

If I remove the .put line, then "memory" accurately contains the value from EEPROM.
I suspect it's something I'm doing wrong, but after fighting with this for several hours, I need help figuring out what. I've done lots of coding, but am just learning the Arduino environment and C++, so any input would be welcomed.

When you put() the original value of 99 in the EEPROM what type of variable was the value held in ?

I simply ran this program with the variable 'output" set equal to 99 (instead of 55, as it is in the attached code)

I just notice that the "output" variable is set to 55 in the code, but in the print out sample, it shows as Output = 88. That was my mistake, I included the wrong print out sample. The print out sample was produced when "long output = 88" ; was used instead of "long output = 55;". I change the value of "output" and reran it, but "memory" always get set to equal "output"

Sorry for the confusion.

Do you get different results if you put a couple of second delay at the beginning of setup? The code may be running after upload and then again when the serial monitor reconnects.

You don't say which Arduino you are using but most will reset when the Serial monitor is opened

So, if you upload the code and then open the Serial monitor the code will run twice. Do you see how that might affect the value that you get() from the EEPROM on the second run ?

What happens if you upload the code with the Serial monitor open ?

I put a 1 second delay as the first step, and the code worked as expected, it it was a timing related issue. Thanks for the suggestion, I was really stumped!

Strangely, the issue occurred even when I reloaded the program without closing/reopening the Serial Monitor. I've seen where a program will restart if the monitor is opened after the program is running, but I assumed that wouldn't be happening in this case where the monitor was open already and only one stream of output was produced. I guess I don't understand exactly what happens on startup, but your suggestion definitely fixed the issue. I have a lot to learn about this environment, thankfully, there are lots of knowledgeable folks like you guys who are willing to help us newbies!

From memory there is a problem when using EEPROM functions early in a sketch because the hardware has not been initialised by then

@johnwasser I seem to remember you pointing this out somewhere

The problem was incrementing an EEPROM location in the 180 milliseconds between uploading and Serial Monitor resetting your Arduino. The increment is done a second time after the reset so it looks like the EEPROM is getting incremented by 2.

That's why I put "delay(200);" after Serial.begin() at the top of setup().

That's consistent with what I've been seeing, as this was originally in the setup in a larger program, and while it had several get's and put's throughout, they all worked as expected. Just this one, in the setup section, acted strangely. The delay fixed the problem, thanks.

Serial Monitor has to disconnect from the serial port when the uploader runs. Only one PC program can have the serial port open at a time. It takes about 150-180 milliseconds after your sketch starts before the Serial Monitor can re-open the serial port. In that time your Arduino UNO can run about 3000 instruction cycles. When the Serial Monitor opens the serial port, the Arduino gets reset. Anything that got done in the first 180-ish milliseconds your sketch was running will be done again.

I've seen code examples where there were delay's that didn't have any obvious (to me) purpose, thanks for the explanation.

Ok, that does make sense. That explains exactly what I was seeing. now I feel better, in the 50 years since I started programming, I've never met a computer with precognition, so I hoped there was a logical reason it could "know" the value of the EEPROM before it was written! :wink:

So, I got my problem solved and some education as as bonus. Thanks to all of you for your prompt and helpful replies.

1 Like

Thanks @johnwasser

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