Uint16_t value incrementing by 2 instead of 1

Hey guys,

I'm essentially trying to take decrement a value that is stored in EEPROM. So first, I read in the value using the get function of the EEPROM library. Then decrement this value and store the new value back into the same position in EEPROM using the put function. Following is the code I am used.

void setup(){
  Serial.begin(9600);
  uint16_t tempvalue;
}

void loop() {
  // put your main code here, to run repeatedly: 
  while(!x){
    EEPROM.get(12, tempvalue);
    Serial.println(tempvalue);
    
    tempvalue = tempvalue - 1;
    
    EEPROM.put(12, tempvalue);
    Serial.println(tempvalue);
    Serial.println("-----------------------");
    x = 1;
  }
}

I ran the code multiple times and this is the output I am getting.

image

I would assume that at each iteration the last value in this iteration and the first value in the subsequent iteration should be the same, right? But they are not. Am I missing something here?

Any help would be greatly appreciated.

Thanks,

The sketch that you posted does not compile because x is not declared and the EEPROM library is not #included

If you want the code to only execute once then why not put it in setup() ?

Your sketch starts running the instant the upload finishes. About 180 milliseconds later, the Serial Monitor connects to your Arduino causing a reset. Since your Arduino decremented the value in EEPROM the first 180 milliseconds and again after the reset you see the value decremented by 2.

I put 'delay(200);' after 'Serial.begin();' to avoid executing code before Serial Monitor has connected.

Note: Declaring a local "uint16_t tempvalue;" in setup() does nothing. Did you want to make it global so loop() can use it?

Sorry I didn't copy paste it in the code, but the x is declared before the setup with a value of 0. And in terms of the code in setup, that's a good idea.

Hello John,

Thank you for your response. Could you just explain the Serial Monitor connection to Arduino causing a reset? I'm just trying to get a better understanding of this.

Thanks,

In order to automate the upload of sketches they connected one of the serial communication control lines to the Reset pin of the Arduino through a capacitor. When the USB-to-Serial chip establishes a connection, it pulls that line LOW and that causes the Arduino to reset. The Arduino bootloader gets control, sees that a hardware reset was performed and waits a fraction of a second to see if an upload starts arriving. If nothing arrives, it does on to start the previously loaded sketch.

Because only one program on your PC can control the serial port at any one time, the Serial Monitor has to disconnect from your Arduino when you do an upload. It then waits for the uploader to disconnect from the serial port and it re-connects. There is about 150 to 180 milliseconds between the uploader program releasing the port and the Serial Monitor connecting to the port again. In that time the Arduino can run execute about 2.4 million instructions, more than enough to read from EEPROM, modify the value, and write it back.

The re-connection of Serial Monitor looks just like the connection from an uploader so the bootloader waits a brief time to see if an upload is happening and then starts your sketch again.

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