button controlled line follower

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

Check out the example "BlinkWithoutDelay" to see how to accomplish this. When you call delay() your program doesn't do anything else until the function is complete. That is not what you want.

well so what should i change in this program and where its almost midnight here and i have to submit this tomorrow and am very tired so thanks

Archut:
well so what should i change in this program and where its almost midnight here and i have to submit this tomorrow and am very tired so thanks

The whole concept must be changed and it seems like the lights are about to go out.

Paul

i even tried this but didn't work

int mot1=9;
int mot2=6;
int mot3=5;
int mot4=3;
#define sw1 A0



int a=1;
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 MOVE(void);
void setup()
{
  pinMode(mot1,OUTPUT);
  pinMode(mot2,OUTPUT);
  pinMode(mot3,OUTPUT);
  pinMode(mot4,OUTPUT);

  pinMode(left,INPUT);
  pinMode(right,INPUT);
 
  pinMode(sw1,INPUT);
  pinMode(buzzer,OUTPUT);

  Serial.begin(9600);
  
  
}

void loop() {
a=analogRead(sw1);
if(a == 0){
  MOVE();
}
 else{
  analogWrite(mot1,0);
analogWrite(mot2,0);
analogWrite(mot3,0);
analogWrite(mot4,0);
  
 }
}

void MOVE(void){
  analogWrite(mot1,255);
analogWrite(mot2,200);
analogWrite(mot3,255);
analogWrite(mot4,200);



 

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);

  
}

Seriously, check out the "BlinkWithoutDelay" example. Open the IDE, go to Examples -> 02.Digital ->BlinkWithoutDelay and look how you can do timing using millis() and a start time rather than using delay().