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 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
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
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
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 &&
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