I am sure I have had this issue more than once and worked arround it , but it's bugging me... No mater what I try , I can't get a logical "or" "||" to work , adding extra () does not help. It compiles ok, uploads ok but when a clause is met, it does not seem to do anything, it never drops out of the loop and just keep decrementing.
the below is just a test code , I know my IDE is out of date but this seems fairly fundamental.
TIA..
Rob
int step1 = 100;
int step2 = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while ( step1 != 0 or step2 != 0 ) {
Serial.println(step1);
Serial.println(step2);
step1--;
step2--;
delay(300);
}
delay (5000);
Serial.println("zero");
step1 = 100;
}
The issue with your while loop condition is that you're using or (||) instead of and (&&). Currently, the loop will continue as long as either step1 or step2 is not zero. If you want the loop to stop when either variable reaches zero, you should use and (&&).
Here's the corrected version:
int step1 = 100;
int step2 = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while (step1 != 0 && step2 != 0) { // Changed 'or' to 'and'
Serial.println(step1);
Serial.println(step2);
step1--;
step2--;
delay(300);
}
delay(5000);
Serial.println("zero");
step1 = 100;
}
The loop will now stop when either step1 or step2 reaches zero