Multiple boolean in while statement [solved]

hi everybody!

trying this:

void movestepper(int z) { // z is a distance in mm from the keypad
int calculatedmove = ((z*200)/8); // it's a leadscrew, from mm to steps
hBval2 = digitalRead(limitSw2); // initial status of the limit switch pin
stepper.moveTo(calculatedmove);
while (hBval2 == HIGH || stepper.distanceToGo()) {
stepper.run();

// just to display and check
lcd.clear();
lcd.print("remaining: ");
lcd.print(stepper.distanceToGo());

hBval2 = digitalRead(limitSw2);

}
delay(3000);
lcd.clear();
}

the WHILE should exit whatever happens first: the endpoint is reached, or the limit sensor is triggered.
i can't make the OR working. it works for each condition alone but running the above code, WHILE exits only when I trigger the limit sensor.

I can't see where the issue is so i'm sending this sos...thanks!

In this case you don't want the OR operator:
while (condition_1 || condition_2) will keep looping until both conditions evaluate to false.
If you want the loop to exit as soon as one of the conditions evaluates to false, you need AND:
while (condition_1 && condition_2)

See also: De Morgan's laws - Wikipedia, which state that !(A && B) == (!A) || (!B), in other words, the following two statements are equivalent

  • exit as soon as (limit switch triggered) OR (distance to go equals 0)”
  • repeat as long as (limit switch not triggered) AND (distance to go does not equal 0)”

omg you're right..I was fixating on the OR condition and didn't analyze the rest..

THANK YOU :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.