Hi Arduini,
I’m pretty new to programming but getting there. I have some code that provides conditional outcome on whether or not the number 0 or 1 are stored in EEPROM. I’ve tried to use the following variations or the “not equals” |= and “OR “||” operators as follows:
In this code the first time I run the code with a new ATMEGA 328pu processor in an Arduino pro-mini type configuration (running at 3.3v and 8mHz clock), I get, as expected print out, “no 0 or 1 detected” then “this is the eeprom contents” “255” (contents of an unused EEPROM memory slot). I then get the printout “should have written 1 in here” followed by a “1”.
#include <EEPROM.h>
int toggleState = 0;
int x= 1;
int y =0;
void setup() {
Serial.begin(115200);
toggleState = EEPROM.read(350);
Serial.println(“this is the eeprom contents”);
Serial.println (toggleState);
delay(10);
if ((toggleState != x) || (toggleState != y)){ // if first use - or EEPROM corrupted
//if (toggleState != x || toggleState != y){ // if first use - or EEPROM corrupted
//if ((toggleState != 0) || (toggleState != 1)){ // if first use - or EEPROM corrupted
//if (toggleState != 0 || toggleState != 1){ // if first use - or EEPROM corrupted
Serial.println(“no 0 or 1 detected”);
EEPROM.write(350,1);//write 1 (ON) to memory
delay(20);
int readToggle = EEPROM.read(350);
Serial.println(“should have written 1 in here”);
Serial.println (readToggle);
delay(10);
}
}
void loop() {
}
So far so good.
But in following attempts I get
“this is the eeprom contents” “1” followed by the unexpected
“no 0 or 1 detected” then “1”.
I tried the (commented out) variations following the if statement - but always the same result.
Clearly, the if statement IS being satisfied – so I must be doing something wrong with the “|=” and “||” operators. But I just can’t work out what it might be – can anyone help?
All the best,
Chris