Eeprom wont update or change a simple value arduino atmega16

I am new to using EEPROM and I am having an issue getting a simple single digit value to change using momentary switches. I can get the value to change from the default empty eeprom to (2,1) in the first section and it changes the values at SS1 & SS2 in my program as it should, but it wont switch over to (2,2)in the second section to switch those values to something else.(the only thing that works in section 2 is that the led3 turns on and off when I push switch 3 & 4

I have added leds in the code so that I could see what is actually happening or not happening.
It is probably something simple, but I can't figure it out.
Thanks Fran

// THIS IS ONLY A SNIPPET OF A 15,000 BYTE PROGRAM and EEPROM is NOT /MENTIONED any where else in the code

int SS2 = 0; 
int SS1 = 0;
int val = (0);

void loop() 
{ 

  int val = EEPROM.read(2);

//FIRST SECTION
if (digitalRead (SW_1) == LOW && digitalRead (SW_3) == LOW) 
{
EEPROM.put(2,1); 
digitalWrite(LED_1,   HIGH);delay(500);
digitalWrite(LED_1,    LOW);
}
if (digitalRead (val) == (2,1))
{
digitalWrite(LED_2,   HIGH); SS1 = 50; SS2 = 500;
}

//SECOND SECTION
if (digitalRead (SW_2) == LOW && digitalRead (SW_4) == LOW) 
{
EEPROM.update(2,2); // ALSO tried write and put here
digitalWrite(LED_3,   HIGH);delay(500);
digitalWrite(LED_3,    LOW);
}
if (digitalRead (val) == (2,2)) 
{
digitalWrite(LED_4,   HIGH); SS1 = 50; SS2 = 1000;
} 

}
if (digitalRead (val) == (2,2)) 

Can you please explain what you think that this line does ?

I thought that it should read eeprom to see if address 2 has a 2 in it instead of 1 or another number
just like the other line in Section 1 does with

if (digitalRead (val) == (2,1))

If you want to compare a value in the EEPROM with another value then you need to explicitly read the value from EEPROM using either the EEPROM.read() or EEPROM.get() command, whichever is appropriate

1 Like

What have you got attached to pin 0 ?

isn't that what ```
int val = EEPROM.read(2);

AND
pin 0 is set as an output and has  led_4 attached to it
int val = EEPROM.read(2);

Yes, that reads a byte from address 2 in the EEPROM and assigns the value to the val variable but that does not explain what you do with

if (digitalRead (val) == (2,2))

That does not read the EEPROM

SAY WHAT?? if it does not read the EEPROM then how is it getting value 1 from address 2 in SECTION 1
and , it is supposed to change values on SS1 and SS2 as is written AND like is done in section 1

digitalRead() does not (cannot) read the EEPROM

It doesn't do anything of the sort

1 Like

Now I remember why I do not ask questions here very often !!

Think about this

Suppose that digitalRead(val) returns 1
How could that ever equal (2,1)

Hello,

you can also check the EEProm Library docu with examples
https://docs.arduino.cc/learn/built-in-libraries/eeprom

Actually, to be fair 1 does equal (2, 1) but not because a value has been read from the EEPROM but because of the use of the comma which causes the 2 to be ignored

However, 1 will also equal (9,8,7,6,5,4,3,2,1) for the same reason

I figured it out myself
Thanks anyway

I am glad that you got it working

For the benefit of others please describe what the problem was, how you corrected it and please post the working sketch

what screwed with my head is that it did actually write and read the eeprom and changed my values in my first section of my original sketch.
since that did not work beyond that.

SO!!
Since

int val = EEPROM.read(2);

was already reading address 2
I could have probably gotten away with removing the first 2(the address) & the comma in

if (digitalRead (val) == (2,1))
and
if (digitalRead (val) == (2,2))
and made it this
if (digitalRead (val) == (1))
and
if (digitalRead (val) == (2))
but, since I am not a fan of using an int like (val) to read one thing then having to go back again and read the digitalread (val)
I removed the (val) altogether and used

if (EEPROM.read(2) == (1))
and
if (EEPROM.read(2) == (2))
in place of
if (digitalRead (val) == (2,1))
and
if (digitalRead (val) == (2,1))

I then put it all in a switch case so that I could use the same two switches at once to switch from 2,1 to 2,2 instead of 4. But this has nothing to really do with the above issue.

As I mentioned , this was my first attempt at EEPROM and I do not code for a living ,nor was I formally taught how to.

Thanks for expanding on your solution, but I have some observations

int val = EEPROM.read(2);

Be careful when using EEPROM.read() and an int variable because the function only reads a single byte and not an int

if (EEPROM.read(2) == (1)); & if (EEPROM.read(2) == (2))

If this is what you really used and the semicolon in the middle of the expression is not just a typo here then this will not work because the semicolon ends the program statement. In fact I am not sure that it will even compile

To add to the problem, a single & does a bitwise comparison whereas what you need is a boolean comparison using &&

1 Like

re: Be careful when using EEPROM.read() and an int variable because the function only reads a single byte and not an int

isn't a single number like 2 -equal to one byte ?

the & is not part of the code. that is two separate statements
colon was an oops
I edited that area to separate lines

Yes, and your code will work even though you are reading a single byte and storing its value in an int, but the reason that I said "be careful" is that if the number that you were reading was greater than 255 then the read() function would not work and you might do this in a future project

In general it is better not to mix data types. Personally I would use EEPROM.get() to load values of any kind into a variable of the appropriate type even if it were only a byte

1 Like

thanks

OFF TOPIC question
I noticed your ID, so you must play with remotes
are you using RF or RF24 or other