EEPROM won't update in the setup

Hey,
I'm having trouble with getting the eeprom to update during the setup. I'm testing out reading and writing to the eeprom. I'm only looking to write a number at the specified address, print it out, write a new number and print that out. I only get the Serial display to spit out the same number each time. I'm sure its something Im missing very simple. Thanks here is my code



#include <EEPROM.h>

int Address_2 = 2; //variable for eeprom address 2
int Value_2 = 60; //variable to store value of 60
int Value_3 = 50; // variable to store value of 50


void setup() {
  Serial.begin(9600);
  
  int read_Value_3 = EEPROM.read(Address_2); //variable containing the value stored at address 2 of eeprom
  
 Serial.println(read_Value_3); //print value stored in address 3 of eeprom

 delay(1000);

 EEPROM.write(Address_2, Value_2); //write value "50" to eeprom address 2

 delay(1000);

 Serial.println(read_Value_3); // print value of eeprom address 2.  Should be "50".
 
 delay(1000);

 EEPROM.write(Address_2, Value_3);// write value of "60" to eeprom address 2

 Serial.println(read_Value_3); // print value  of eeprom address 2.  Should be "60".

}

void loop() {
  

}

PErhaps forgetting to read the value before printing it?

That is exactly what you programmed it to do. To print the updated EEPROM value, read it, then print.

I'm not following that. Seems like it should print "60, 50, 60"? Instead it prints 50,50,50

it's printing the variable that is the eeprom read. Maybe im missing something here

No, because read_Value_3 does not change, after the EEprom.read().

Are you actually reading the responses to your post?

I am.

This line is what I "think" I'm changing read_Value_3 with

 EEPROM.write(Address_2, Value_3);// write value of "60" to eeprom address 2

That line changes the contents of the EEPROM memory, not the contents of the variable read_Value_3.

the value of read_Value_3 is based on that memory location though, no?

No. The contents of read_Value_3 are stored in SRAM (random access memory).

This statement declares the variable read_Value_3 and defines its contents, once only:
int read_Value_3 = EEPROM.read(Address_2);

You have to read it, if you expect it to have changed.
You're writing 50, reading 50, writing 60, but never reading it again.
C

So I should just get rid of the variable int read_Value_3 and just use

Serial.println(EEPROM.read(Address_2));

or repeat the read???!

how would that look? I guess when I put that read_Value_3 variable I thought it did read from the eeprom again. I guess I dont have a good enough understanding of how it works. I've read through the arduino section on eeprom. I must be misunderstanding something. I assumed it read and printed what it returned with this statement

Read my comments, and compare them with yours.

#include <EEPROM.h>
int Address_2 = 2; //variable for eeprom address 2
int Value_2 = 60; //variable to store value of 60
int Value_3 = 50; // variable to store value of 50
void setup() {
  Serial.begin(9600);
  int read_Value_3 = EEPROM.read(Address_2); //variable containing the value stored at address 2 of eeprom
  Serial.println(read_Value_3); //print value stored in address 3 of eeprom
  delay(1000);
  EEPROM.write(Address_2, Value_2); //write value "50" to eeprom address 2
  // You just wrote "60" to address 2
  delay(1000);
  Serial.println(read_Value_3); // print value of eeprom address 2.  Should be "50".
  //You just printed the same value read earlier.  No change.
  delay(1000);
  EEPROM.write(Address_2, Value_3);// write value of "60" to eeprom address 2
  // You just wrote "50" to address 2
  Serial.println(read_Value_3); // print value  of eeprom address 2.  Should be "60".
  //You just printed the same value read earlier.  No change.
  // next time you press reset, yes, Address_2 will have the value you just wrote to it, no change.
}

void loop() {
}

Just a hint, not all "EEPROM" is actually written to the same way in all units. Some simulate the EEPROM using flash such as the ESP devices.

Perhaps you misunderstand the function of "=" in a C/C++ statement.

It actively transfers a value once, so this statement

int read_Value_3 = EEPROM.read(Address_2);

instructs the computer to create a new integer variable read_Value_3, read the contents of EEPROM location Address_2, and store them in the memory locations assigned to read_Value_3.

ok yeah it makes sense. I just thought the variable was dynamic with whatever was read at the time the variable was called.

The magic in "C" does not include that capability.

Could you please explain this statement? I can't even figure out what you think you meant.