well i want to make a mini car which basically uses a the line following technique to follow a certain path for a couple of seconds which is determined by the button pressed which i am not able to achieve . I want if i press button 1 it should move for 5 seconds and if i press button 2 it should move for 10 secs which can be achieved using delay but it just keeps moving and doesn't obey the button press
please help asap
int mot1=9;
int mot2=6;
int mot3=5;
int mot4=3;
int sw1=A0;
int sw2=11;
int left=12;
int right=11;
int buzzer=2;
int Left=0;
int Right=0;
void LEFT (void);
void RIGHT (void);
void STOP (void);
void setup()
{
pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
pinMode(mot3,OUTPUT);
pinMode(mot4,OUTPUT);
pinMode(left,INPUT);
pinMode(right,INPUT);
pinMode(sw2,INPUT);
pinMode(sw1,INPUT);
pinMode(buzzer,OUTPUT);
digitalWrite(left,HIGH);
digitalWrite(right,HIGH);
}
void loop() {
if(digitalRead(sw1)==LOW){
analogWrite(mot1,255);
analogWrite(mot2,200);
analogWrite(mot3,255);
analogWrite(mot4,200);
delay(5000);
while(1)
{
Left=digitalRead(left);
Right=digitalRead(right);
if((Left==0 && Right==1)==1)
LEFT();
else if((Right==0 && Left==1)==1)
RIGHT();
}
}
}
void LEFT (void)
{
analogWrite(mot3,0);
analogWrite(mot4,30);
while(Left==0)
{
Left=digitalRead(left);
Right=digitalRead(right);
if(Right==0)
{
int lprev=Left;
int rprev=Right;
STOP();
while(((lprev==Left)&&(rprev==Right))==1)
{
Left=digitalRead(left);
Right=digitalRead(right);
}
}
analogWrite(mot1,255);
analogWrite(mot2,200);
}
analogWrite(mot3,255);
analogWrite(mot4,200);
}
void RIGHT (void)
{
analogWrite(mot1,0);
analogWrite(mot2,30);
while(Right==0)
{
Left=digitalRead(left);
Right=digitalRead(right);
if(Left==0)
{
int lprev=Left;
int rprev=Right;
STOP();
while(((lprev==Left)&&(rprev==Right))==1)
{
Left=digitalRead(left);
Right=digitalRead(right);
}
}
analogWrite(mot3,255);
analogWrite(mot4,200);
}
analogWrite(mot1,255);
analogWrite(mot2,200);
}
void STOP (void)
{
analogWrite(mot1,0);
analogWrite(mot2,0);
analogWrite(mot3,0);
analogWrite(mot4,0);
delay(2000);
analogWrite(mot1,255);
analogWrite(mot2,200);
analogWrite(mot3,255);
analogWrite(mot4,200);
}
the button is connected to 5v and Analog 0