Cant stop a while loop & if's dont stop

I'm trying to create a robot that follows a line but only after you press the button, and when both sensors read stop then stop the while loop. The first problem, while loop doesn't stop at all, and the second is if it starts with a certain "if" that "if" doesn't change even if the sensor is reading something else.


#define in1 5 //Motor1  L293 Pin in1 
#define in2 6 //Motor1  L293 Pin in1 
#define in3 3 //Motor2  L293 Pin in1 
#define in4 7 //Motor2  L293 Pin in1 
#define stopLED 2
#define R_S 11//ir sensor Right
#define L_S 10 //ir sensor Left
int stop = 1;
void setup(){ 

pinMode(R_S, INPUT); 
pinMode(L_S, INPUT); 
pinMode(stopLED, OUTPUT);
pinMode(in1, OUTPUT); 
pinMode(in2, OUTPUT); 
pinMode(in3, OUTPUT); 
pinMode(in4, OUTPUT); 
Serial.begin(9600);
delay(1000);

}

void loop(){  
if((digitalRead(13) == HIGH)){
    delay(100);
    Serial.println("1");
        stop = 0;
        while(stop==0){
          
          if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){
              digitalWrite(stopLED, LOW);
              digitalWrite(in1, HIGH); //Right Motor forword Pin 
              digitalWrite(in2, LOW);  //Right Motor backword Pin 
              digitalWrite(in3, LOW);  //Left Motor backword Pin 
              digitalWrite(in4, HIGH); //Left Motor forword Pin 
              Serial.println("f");
              stop=0;
          }   
function


          if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){
              digitalWrite(stopLED, LOW);
              digitalWrite(in1, LOW);  //Right Motor forword Pin 
              digitalWrite(in2, HIGH); //Right Motor backword Pin  
              digitalWrite(in3, LOW);  //Left Motor backword Pin 
              digitalWrite(in4, HIGH); //Left Motor forword Pin 
              stop=0;
              Serial.println("l");
          } 



          if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){
              digitalWrite(stopLED, LOW);
              digitalWrite(in1, HIGH); //Right Motor forword Pin 
              digitalWrite(in2, LOW);  //Right Motor backword Pin 
              digitalWrite(in3, HIGH); //Left Motor backword Pin 
              digitalWrite(in4, LOW);  //Left Motor forword Pin 
              stop=0;
              Serial.println("r");
          }  



          if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){
              digitalWrite(in1, LOW); //Right Motor forword Pin 
              digitalWrite(in2, LOW); //Right Motor backword Pin 
              digitalWrite(in3, LOW); //Left Motor backword Pin 
              digitalWrite(in4, LOW); //Left Motor forword Pin 
              delay(1000);
              digitalWrite(stopLED, HIGH);
              stop=1;
              Serial.println("STOP");
          } 

        }
          
}
 




}


Welcome to the forum

function

What is this doing in the middle of the sketch that you posted ?

Is there anything keeping the inputs in a known state at all times ?

Do you have external pull down resistors on your input pins? If not, try using the internal pullups and invert your logic

pinMode(R_S, INPUT_PULLUP); 
pinMode(L_S, INPUT_PULLUP); 

Now the inputs are "active low". Change your logic accordingly, such as

if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){
              digitalWrite(stopLED, LOW);
              digitalWrite(in1, HIGH); //Right Motor forword Pin 
              digitalWrite(in2, LOW);  //Right Motor backword Pin 
              digitalWrite(in3, LOW);  //Left Motor backword Pin 
              digitalWrite(in4, HIGH); //Left Motor forword Pin 
              Serial.println("f");
              stop=0;

0s are now 1s and 1s are now 0s.
Correct pin 13 in the same fashion

Hi, @xsyll
Welcome to the forum.

Thanks for using code tags. :+1:

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Have you written you code in stages and tested that your sensors are communicating with your controller?

What model Arduino are you using?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

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