Code won't exit while loop

I see no reason why while loop is not exiting when One or Two conditions are met. Can you please ?

/*
Sumo on Arduino Nano at 17/10/25
D2 motor 1
D3 motor 1
D4 motor 2
D5 motor 2
D6 sonic
D7 sonic
D8 IR
D9 LED blue
*/
const int A1A=2; //Motor 1
const int A1B=3;
const int B1A=4; //Motor 2
const int B1B=5;
const int trigPin = 6;
const int echoPin = 7; //blue wire
long duration;
int distance;
int gec = 1000;
void setup() 
{
            pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
            pinMode(echoPin, INPUT); // Sets the echoPin as an Input
            pinMode(A1A, OUTPUT);
            pinMode(A1B, OUTPUT);
            pinMode(B1A, OUTPUT);
            pinMode(B1B, OUTPUT);
            pinMode(8, INPUT);            
            pinMode(9, OUTPUT); //led           
            Serial.begin(9600);
}
 
void loop() 
{
            howfar();
            digitalWrite(9, LOW);
            while ((distance > 5) or (digitalRead(8)==  HIGH))
            {
                      Serial.print("Distance: ");
                      Serial.println(distance);
                      Serial.print("Hole: ");
                      Serial.print(digitalRead(8));
                      howfar();
                      moveAhead();
            }
            Serial.print("Out of loop ");
            digitalWrite(9, HIGH);
            moveStop();
            delay(500);
            moveRight();
            delay(500);
            moveStop();
            delay(500);            
}

void howfar() 
{
            digitalWrite(trigPin, LOW);
            delayMicroseconds(2);
            digitalWrite(trigPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(trigPin, LOW);
            duration = pulseIn(echoPin, HIGH);
            distance = duration * 0.034 / 2;
}

void moveRight()
{
    Serial.println("Right....");
    analogWrite(A1A,0);
    analogWrite(A1B,0); 
    analogWrite(B1A,80);
    analogWrite(B1B,0);       
delay(gec);
}  
void moveStop()
{
    Serial.println("Stop....");
    analogWrite(B1A,0);
    analogWrite(B1B,0);
    analogWrite(A1A,0);
    analogWrite(A1B,0);
delay(gec);
}  
void moveLeft()
{
    Serial.println("Left....");
    analogWrite(B1A,80);
    analogWrite(B1B,0);
    analogWrite(A1A,0);
    analogWrite(A1B,0);
delay(gec);
}

void moveAhead()
{
     // Serial.println("Ahead....");
      analogWrite(B1A,80);
      analogWrite(B1B,80); 
      analogWrite(A1A,80);
      analogWrite(A1B,80);
      delay(gec);       
} 

void moveReverse()
{
      Serial.println("Reverese....");
      analogWrite(B1A,0);
      analogWrite(B1B,80); 
      analogWrite(A1A,0);
      analogWrite(A1B,80);    
delay(gec);
}  

Perhaps because with a logical OR, the while loop will continue to be executed as long as one or both conditions are met?

Given what you've written, the only way the loop will be exited is when both conditions are not met.

I need it to exit the loop whenever any condition mentioned is met. Even when both conditions are not met it stays in the loop. which doesn’t make any sense.

That's not what is written in your code. It will stay in the loop if either condition is met.

How do you know? (That's a hint: Show. The. Output.)

Hole: 0Distance: 133

Hole: 1Distance: 147

Hole: 1Distance: 131

Hole: 1Distance: 132

Hole: 1Distance: 131

Hole: 1Distance: 77

Hole: 1Distance: 132

Hole: 1Distance: 132

Hole: 1Distance: 131

Hole: 1Distance: 132


assuming pin 8 is a button that can be pressed to exit the loop and is wired between the pin and ground, should the pin be configured as INPUT_PULLUP?

The value of Distance you're showing is greater than 5.

Therefore it doesn't matter what the value of digitalRead(8) is. It won't ever even be read as the boolean expression will short circuit at (distance > 5) because it's true.

Your code says to keep executing the loop while distance > 5.

Distance is greater than 5.

The loop keep executing. Just as you've instructed it to.

True but when I put my hand in front of the sonar it goes below 5. So its not exiting.

Nop

Not according to any output you've shown.

If you had a real problem, your output would show something like the following.

Distance: 4
Hole: 0

In that case, distance>5 would be false, AND digitalRead(8) would be not equal to HIGH.

And that would be an interesting problem, seeing as you your while condition would have detected that and exited the loop. I suppose it could be displayed once if you managed to have pin 8 go LOW between the test in the while and where it's printed out in the loop. But you'd have to be awfully lucky.

You haven't shown that to be the case.

And I'm said the same thing way too many times already. Someone else want to give it a try? I'm done.

I’m thinking your or should really be an and instead..

good luck.. ~q

van_der_decken: Thanks for you input.

Qubits-us: Those Two conditions represent a distance measuring mechanisms. So which-ever is too close it must stop the motors from going ahead. “And” is not the case here.

Sure, @van_der_decken , I'll give him one, and only one, try.
Look at this code, @vaxman .
It will only ever print, if at least one of the conditions is true. So, you will NEVER see Hole = 0 AND distance <5, because in that specific instance, you don't actually enter the loop.

Capiche?

I tried

 while ((digitalRead(8)==  HIGH))

and it exists the loop. Also tried

while ((distance > 5))

and it exists the loop too.

So the I fail to see the problem in

while ((distance > 5) or (digitalRead(8)==  HIGH))

I’ll just rey to implement Two inner while s and see what happens.

The output shows Two variables because they are instructed to do so WITHIN the loop. So not Capiche :slight_smile:

This worked for me

            while (distance > 5) 
            {
                      Serial.print("Distance: ");
                      Serial.println(distance);
                      Serial.print("Hole: ");
                      Serial.print(cukur);
                      howfar();
                      moveAhead();
                      if (cukur == LOW) {break;}
            }
void howfar() 
{
            digitalWrite(trigPin, LOW);
            delayMicroseconds(2);
            digitalWrite(trigPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(trigPin, LOW);
            duration = pulseIn(echoPin, HIGH);
            distance = duration * 0.034 / 2;

            cukur = digitalRead(8);

}

Thank you all for your input.

Hi, @vaxman
Have you got an external pullup resistor on pin8 if your button is between pin8 and gnd?
OR
Have you got an external pulldown resistor on pin8 if your button is between pin8 and Vcc?

Tom.... :smiley: :+1: :coffee: :australia:

if the button is wired between pin and ground and configured as INPUT_PULLUP, it will become LOW while pressed and the logic should be if the distance is far AND the button the is NOT pressed, continue in the loop
while ((distance > 5) && (digitalRead(8)== HIGH))
the loop should break if the button is pressed OR the distance becomes close