How do I get line following robot to run for 10 seconds when button is pushed

const int rightmotorPin = 11;
const int leftmotorPin = 10;
const int centerirPin = 12;
const int rightirPin = 3;
const int leftirPin = 4;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
int distance;
int time;
int distrange = 30;
int centerVal;
int rightVal;
int leftVal;
int PWMspeed = 90;
void setup() {
  // put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
  pinMode(leftmotorPin, OUTPUT);
    pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
        pinMode(centerirPin, INPUT);
          pinMode(leftirPin, INPUT);
            pinMode(rightirPin, INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int centerVal = digitalRead(centerirPin); 
  int rightVal = digitalRead(rightirPin);  
  int leftVal = digitalRead(leftirPin); 

  if((rightVal == 0) && (leftVal == 0)) { //IDEAL CASE
    digitalWrite(rightmotorPin, PWMspeed);
    digitalWrite(leftmotorPin, PWMspeed);
  }

    if((rightVal == 1) && (leftVal == 0)) { //TURN RIGHT
      digitalWrite(rightmotorPin, LOW);
      digitalWrite(leftmotorPin, PWMspeed);
    }
      if((rightVal == 0) && (leftVal == 1)) { //TURN LEFT
        digitalWrite(leftmotorPin, LOW);
        digitalWrite(rightmotorPin, PWMspeed);
      }
        if((rightVal == 1) && (leftVal == 1)) { //STOP
          digitalWrite(leftmotorPin, LOW); 
          digitalWrite(rightmotorPin, LOW);
        }

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
  int distance = time * 0.0343 / 2; //343 speed of soundin air time in microseconds hence 0.0343

  if(distance < distrange) { //OBSTACLE AHEAD STOP
    digitalWrite(leftmotorPin, LOW);
    digitalWrite(rightmotorPin, LOW);
  }
Serial.println(distance);

My button is wired currently so that on bottom right pin i have resistor and ground connection and top left pin i have the digital pin connection

i have tried this connection as well and still the same problem occurs

is it a problem with the resistor? what is preffered ohm resistance i should use?

I do not see a button referenced in your code. Would you show that, and also a drawing of how your button is attached to your Arduino? Last post said your pushbutton is resetting the USB port.

A good way to wire a button is from the Arduino pin, to the pushbutton, then from the pushbutton on a Normally Open contact to ground.

Then you define a button pin in your code:

int buttonPin = 3; // for example

Then you set the function of the button pin:

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // buttonPin is normally HIGH with internal PULLUP resistor
.
.
}

Then, poll the button pin:

void loop() {
  if (!digitalRead(buttonPin)); // buttonPin is LOW when pressed
}

You should see the button pin go from HIGH (not pressed) to LOW (pressed).

300ohm (220R...330R..) seems good for a button. You have a pulldown resistor, so your code is waiting for a HIGH.

I think i was using a 220 ohm resistor and shouldnt my button value always be HIGH as i set it as input_pullup and it should wait for a LOW?

Yes.... I read your post wrong. Sorry.

No problem :slight_smile:

Also now what I want to do is once the button is pushed once the robot should run for 10 seconds and then stop for a certain amount of time (lets just say 5 seconds) and then run again for 10 seconds

similiar to how a train stops at different train stations is it possible to do that?

Using the timer your set up in Post #2, where you were counting 10 seconds, reset your timer and use a conditional "if()" to test for 5 seconds, and again for 10 seconds. Your "millis()" timeline can be imagined like this... all values are examples:

MILLIS
0000 - this is when you turned your Arduino on
0001 - 1 millisecond later
.
.
15377 - 15377 milliseconds later (15.377 seconds)
15378 - buttonPressed = millis() - this is when button was pressed and robot moves
.
.
25378 - millis() minus buttonPressed = 10000 which is your buttonInterval and stop robot moving
.
.
.
30378 - 5 seconds more (5000 millis()) - start the robot again (you can either start at buttonPressed and add 15000 or buttonInterval and add 5000). Maybe call it "stopDuration"
.
.
forever

You can see that you need to find the difference between "now" (millis(), a "zero" time) and when you started an event (buttonPressed for example) and compare that to any of your pre-defined intervals.

Understood, I shall write the code using this logic can i send it to you to make sure it is right

Yes, post your work here.

Note this is the code for the first part where i press the button once and the robot runs for 10 seconds i am still working on the second one where it runs, stops and runs again

Also i added more line following functions and changed the structure of the code to better incoorporate the distance sensor if condition

const int rightmotorPin = 10;
const int leftmotorPin = 11;
const int centerirPin = 12;
const int rightirPin = 5;
const int leftirPin = 6;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
const int buttonPin = 3;

int distance;
int time;
int centerVal;
int rightVal;
int leftVal;
int buttonState;

const byte PWMspeed = 50;

unsigned long trackTime = 10 * 1000 //10 seconds
unsigned long buttonPressed = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(rightmotorPin, OUTPUT);
  pinMode(leftmotorPin, OUTPUT);
    pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
        pinMode(centerirPin, INPUT);
          pinMode(leftirPin, INPUT);
            pinMode(rightirPin, INPUT);
              pinMode(buttonPin, INPUT_PULLUP); //ALWAYS HIGH
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  int time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
    int distance = time * 0.0343 / 2; //343 speed of sound in air time in microseconds hence 0.0343

  int centerVal = digitalRead(centerirPin); //1 white 0 black
    int rightVal = digitalRead(rightirPin);  //0 IS WHITE AND 1 IS BLACK
      int leftVal = digitalRead(leftirPin); //0 IS WHITE AND 1 IS BLACK 

  if(buttonState == 0) {
    buttonPressed = millis(); //NOTES DOWN THE TIMEEEEEEEEEEE WHEN THE BUTTON WAS PRESSED AND STORES IT IN THE BUTTONPRESSED VARIABLE
    delay(150); //debounce button press
  }

  if(millis() - buttonPressed < trackTime) { //IF THE TIME RIGHT NOW - TIME WHEN BUTTON WAS PRESSED IS LESS THAN 10 SECONDS THEN FOLLOW LINE

    if(distance < 20) { //OBSTACLE AHEAD STOP
      digitalWrite(rightmotorPin, 0);
      digitalWrite(leftmotorPin, 0);
      } else {

          if((leftVal == 0) && (centerVal == 0) && (rightVal == 0)) { //IDEAL CASE
            digitalWrite(rightmotorPin, PWMspeed);
            digitalWrite(leftmotorPin, PWMspeed);
          }     

            else if((leftVal == 0) && (centerVal == 1) && (rightVal == 1)) { //RIGHT SENSOR ON LINE
              digitalWrite(rightmotorPin, 0);
              digitalWrite(leftmotorPin, PWMspeed);
            }
              else if((leftVal == 1) && (centerVal == 1) && (rightVal == 0)) { //LEFT SENSOR ON LINE
                digitalWrite(leftmotorPin, 0);
                digitalWrite(rightmotorPin, PWMspeed);
              }
                else if((leftVal == 1) && (centerVal == 0) && (rightVal == 1)) { //STOP ALL BLACK DETECTED
                  digitalWrite(leftmotorPin, PWMspeed); 
                  digitalWrite(rightmotorPin, 0);
                }
                  else if((leftVal == 0) && (centerVal == 1) && (rightVal == 0)) { // ALL WHITE
                    digitalWrite(leftmotorPin, PWMspeed); 
                    digitalWrite(rightmotorPin, PWMspeed);
                  }
                    else if((leftVal == 1) && (centerVal == 0) && (rightVal == 0)) { //LEFT AND CENTER SENSOR SENSOR ON LINE
                      digitalWrite(rightmotorPin, PWMspeed);
                      digitalWrite(leftmotorPin, 0);
                    }
                      else if((leftVal == 0) && (centerVal == 0) && (rightVal == 1)) { //RIGHT AND CENTER SENSOR ON LINE
                        digitalWrite(rightmotorPin, 0);
                        digitalWrite(leftmotorPin, PWMspeed);
                      }
                        else if((leftVal == 1) && (centerVal == 1) && (rightVal == 1)) { //RIGHT AND LEFT SENSOR BLACK CENTER SENSOR WHITE
                          digitalWrite(rightmotorPin, PWMspeed);
                          digitalWrite(leftmotorPin, PWMspeed);
                        }
                          else { //IF NONE OF OTHER CONDITIONS
                            digitalWrite(rightmotorPin, 0);
                            digitalWrite(leftmotorPin, 0);
                          }
                            }
                              }
Serial.println(distance);
}

This line needs a semi-colon after the 1000: (I put one in)

unsigned long trackTime = 10 * 1000; //10 seconds

Is your button is read as "pressed" all the time?

Here is your project in a simulation... you can see the sim keeps "rolling" at any SR04 distance. The "IR" devices are simple switches in the simulation. I put notes in the comments at the top that might help identify what needs to be addressed.

https://wokwi.com/projects/370699668648464385

Leaving files here for wokwi...

sketch.ino
// https://forum.arduino.cc/t/how-do-i-get-line-following-robot-to-run-for-10-seconds-when-button-is-pushed/1146946/
// https://wokwi.com/projects/370699668648464385

// 1. Motors stop at distance < 20 if motor is on or within 10 seconds of button press
// 2. If button is pressed with distancd < 20 motors will not run,  timer will count to 10
// 3. If distance > 20 and button is pressed, line follower motors run
// 4. After button is pressed, motors stop after 10 seconds
// 5. After button is pressed and Left/Center/Right IR is ON, corresponding motor stops

#define DEBUG 1 // 0 = packed debug string, 1 = long debug string

// pins
const int rightmotorPin = 10;
const int leftmotorPin = 11;
const int centerirPin = 12;
const int rightirPin = 5;
const int leftirPin = 6;
const int trigPin = 2; //digital pin here
const int echoPin = A5; //analog pin here
const int buttonPin = 3;

const byte PWMspeed = 50;

// ultrasonic sensor (sonar)
int distance;
int time;

// IR line follower
int centerVal;
int rightVal;
int leftVal;

// button detection
int buttonState = 0; // set after button is pressed to make motors run. reset to 0 after trackTime
unsigned long buttonPressed = 0; // holds time button was pressed
int startMotorButton;

// timer
unsigned long trackTime = 10 * 1000; //10 seconds << this line needed a semicolon ;
unsigned long previousMillis = 0; // campare with millis() for event interval

void setup() {
  // put your setup code here, to run once:
  pinMode(rightmotorPin, OUTPUT);
  pinMode(leftmotorPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(centerirPin, INPUT);
  pinMode(leftirPin, INPUT);
  pinMode(rightirPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); //ALWAYS HIGH
  Serial.begin(115200); // 115200 is much faster than 9600
}

void loop() {

  pingSONAR(); // HCSR04 distance finder
  time = pulseIn(echoPin, HIGH); //measure how much time echoPin was HIGH
  distance = time * 0.0343 / 2; //343 speed of sound in air time in microseconds hence 0.0343

  lineSense(); // IR line sensors
  checkButton(); // for motor start
  if (buttonState) { // only run the motors after a button press
    lineFollow();
  }

  if (DEBUG)
    debugLong  (buttonState, distance, leftVal, centerVal, rightVal, buttonPressed);
  else
    debugShort (buttonState, distance, leftVal, centerVal, rightVal, buttonPressed);
}

//******************************************************************************
// User Defined Functions
//******************************************************************************

void checkButton() {
  startMotorButton = digitalRead(buttonPin); // need to read the button pin...
  if (startMotorButton == 0) { // 0 (button pressed), 1 (not pressed)
    buttonPressed = millis(); //NOTES DOWN THE TIMEEEEEEEEEEE WHEN THE BUTTON WAS PRESSED AND STORES IT IN THE buttonPressed VARIABLE
    buttonState = 1;  // button was pressed... need to clear buttonState after 10 seconds
  }
}

void lineSense() {
  centerVal = digitalRead(centerirPin); // 1 white 0 black
  rightVal = digitalRead(rightirPin);   // 0 IS WHITE AND 1 IS BLACK
  leftVal = digitalRead(leftirPin);     // 0 IS WHITE AND 1 IS BLACK
}

void pingSONAR() {
  digitalWrite(trigPin, LOW); // force trigger low for clean high
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW); // force trigger low for clean echo
}

void lineFollow() {
  if (millis() - buttonPressed < trackTime) { //IF THE TIME RIGHT NOW MINUS TIME WHEN BUTTON WAS PRESSED IS LESS THAN 10 SECONDS THEN FOLLOW LINE

    if (distance < 20) { //OBSTACLE AHEAD STOP
      digitalWrite(rightmotorPin, 0);
      digitalWrite(leftmotorPin, 0);
    } else { // DISTANCE > 20, NO OBSTACLE,  CONTINUE LINE FOLLOWING

      if ((leftVal == 0) && (centerVal == 0) && (rightVal == 0)) { //IDEAL CASE
        digitalWrite(rightmotorPin, PWMspeed);
        digitalWrite(leftmotorPin, PWMspeed);
      }
      else if ((leftVal == 0) && (centerVal == 1) && (rightVal == 1)) { //RIGHT SENSOR ON LINE
        digitalWrite(rightmotorPin, 0);
        digitalWrite(leftmotorPin, PWMspeed);
      }
      else if ((leftVal == 1) && (centerVal == 1) && (rightVal == 0)) { //LEFT SENSOR ON LINE
        digitalWrite(leftmotorPin, 0);
        digitalWrite(rightmotorPin, PWMspeed);
      }
      else if ((leftVal == 1) && (centerVal == 0) && (rightVal == 1)) { //STOP ALL BLACK DETECTED
        digitalWrite(leftmotorPin, PWMspeed);
        digitalWrite(rightmotorPin, 0);
      }
      else if ((leftVal == 0) && (centerVal == 1) && (rightVal == 0)) { // ALL WHITE
        digitalWrite(leftmotorPin, PWMspeed);
        digitalWrite(rightmotorPin, PWMspeed);
      }
      else if ((leftVal == 1) && (centerVal == 0) && (rightVal == 0)) { //LEFT AND CENTER SENSOR SENSOR ON LINE
        digitalWrite(rightmotorPin, PWMspeed);
        digitalWrite(leftmotorPin, 0);
      }
      else if ((leftVal == 0) && (centerVal == 0) && (rightVal == 1)) { //RIGHT AND CENTER SENSOR ON LINE
        digitalWrite(rightmotorPin, 0);
        digitalWrite(leftmotorPin, PWMspeed);
      }
      else if ((leftVal == 1) && (centerVal == 1) && (rightVal == 1)) { //RIGHT AND LEFT SENSOR BLACK CENTER SENSOR WHITE
        digitalWrite(rightmotorPin, PWMspeed);
        digitalWrite(leftmotorPin, PWMspeed);
      }
      else { //IF NONE OF OTHER CONDITIONS
        digitalWrite(rightmotorPin, 0);
        digitalWrite(leftmotorPin, 0);
      }
    }
  }
  else { // after 10 seconds
    // clear buttonState to stop motors
    buttonState = 0;
    // stop motors
    digitalWrite(rightmotorPin, 0);
    digitalWrite(leftmotorPin, 0);
  }
}

void debugShort(int buttonState, int distance, int leftVal, int centerVal, int rightVal, unsigned long buttonPressed) {
  if (millis() - previousMillis > 1000 ) { // one second printout interval
    previousMillis = millis(); // set timer to current millis for next event

    Serial.print("Time:");
    if (((millis() - buttonPressed) / 1000) < 100) Serial.print("0"); // zero pad for Serial presentation
    if (((millis() - buttonPressed) / 1000) < 10) Serial.print("0"); // zero pad for Serial presentation
    Serial.print((millis() - buttonPressed) / 1000);

    if (distance < 20)  Serial.print("(distance:"); // lower case indicating LESS THAN
    else                Serial.print("(DISTANCE:"); // upper case indicating GREATER THAN
    if (distance < 100) Serial.print("0"); // zero pad for Serial presentation
    if (distance < 10)  Serial.print("0"); // zero pad for Serial presentation
    Serial.print(distance);

    Serial.print(" BLCRLR:");
    Serial.print(buttonState);
    Serial.print(leftVal);
    Serial.print(centerVal);
    Serial.print(rightVal);
    Serial.print(digitalRead(leftmotorPin));
    Serial.print(digitalRead(rightmotorPin));
    Serial.println();
  }
}

void debugLong(int buttonState, int distance, int leftVal, int centerVal, int rightVal, unsigned long buttonPressed) {
  if (millis() - previousMillis > 1000 ) { // one second printout interval
    previousMillis = millis(); // set timer to current millis for next event

    Serial.print(" (Motor:");
    if (digitalRead(leftmotorPin)) Serial.print("LEFT.");
    else                           Serial.print("left.");
    Serial.print(digitalRead(leftmotorPin));

    if (digitalRead(rightmotorPin)) Serial.print(" RIGHT.");
    else                            Serial.print(" right.");
    Serial.print(digitalRead(rightmotorPin));
    if (!buttonState) Serial.print(" time."); // lower case means motor not on 
    else              Serial.print(" TIME."); // upper case means motor is on
    if (!buttonState) 
      Serial.print("000) ");
      else {
        if (((millis() - buttonPressed) / 1000) < 100) Serial.print("0"); // zero pad for Serial presentation
        if (((millis() - buttonPressed) / 1000) < 10) Serial.print("0"); // zero pad for Serial presentation
        Serial.print((millis() - buttonPressed) / 1000);
        Serial.print(") ");
      }

    if (distance < 20)  Serial.print("(distance:"); // lower case indicating LESS THAN
    else                Serial.print("(DISTANCE:"); // upper case indicating GREATER THAN
    if (distance < 100) Serial.print("0"); // zero pad for Serial presentation
    if (distance < 10)  Serial.print("0"); // zero pad for Serial presentation
    Serial.print(distance);
    Serial.print(")");

    if (!buttonState) Serial.print(" (buttonState:");
    else              Serial.print(" (BUTTONSTATE:");
    Serial.print(buttonState);

    Serial.print(") (IR:");
    if (leftVal) Serial.print("LEFT.");
    else         Serial.print("left.");
    Serial.print(leftVal);

    if (centerVal) Serial.print(" CENTER.");
    else           Serial.print(" center.");
    Serial.print(centerVal);

    if (rightVal) Serial.print(" RIGHT.");
    else          Serial.print(" right.");
    Serial.print(rightVal);
    Serial.print(")");
    Serial.println();
  }
}
diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -205.46,
      "left": 163,
      "attrs": { "color": "red", "label": "RIGHT MOTOR" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -198.53,
      "left": -56.07,
      "attrs": { "color": "red", "label": "LEFT MOTOR" }
    },
    {
      "type": "wokwi-text",
      "id": "text1",
      "top": -181.92,
      "left": -131.96,
      "attrs": { "text": "LEFT IR" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -320.97, "left": -162.13, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -20.41, "left": -154.72, "attrs": {} },
    {
      "type": "wokwi-text",
      "id": "text2",
      "top": -184.31,
      "left": 8.3,
      "attrs": { "text": "CENTER IR" }
    },
    {
      "type": "wokwi-text",
      "id": "text3",
      "top": -186.05,
      "left": 215.23,
      "attrs": { "text": "RIGHT IR" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -106.73,
      "left": -87.42,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -106.87,
      "left": 133.86,
      "rotate": 90,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic1",
      "top": -319.7,
      "left": -19.3,
      "attrs": { "distance": "63" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -287.93,
      "left": 182.8,
      "attrs": { "color": "green", "label": "START LINE FOLLOW" }
    },
    { "type": "wokwi-slide-switch", "id": "sw1", "top": -161.6, "left": -116.9, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw2", "top": -162.67, "left": 36.03, "attrs": {} },
    { "type": "wokwi-slide-switch", "id": "sw3", "top": -167.07, "left": 236.96, "attrs": {} }
  ],
  "connections": [
    [ "nano:10", "led1:A", "green", [ "v-15.57", "h169.43", "v-152.69" ] ],
    [ "led2:A", "nano:11", "green", [ "h17.4", "v148.43", "h72.3" ] ],
    [ "led2:C", "r1:1", "green", [ "h-16.3", "v26.06" ] ],
    [ "gnd1:GND", "r1:2", "black", [ "v-10.86", "h99.68" ] ],
    [ "led1:C", "r2:1", "green", [ "h-15.24", "v32.59" ] ],
    [ "gnd1:GND", "r2:2", "black", [ "v-10.67", "h320.14" ] ],
    [ "vcc1:VCC", "ultrasonic1:VCC", "red", [ "v78.33", "h201.03" ] ],
    [ "nano:2", "ultrasonic1:TRIG", "green", [ "v-208.73", "h-53.7" ] ],
    [
      "ultrasonic1:ECHO",
      "nano:A5",
      "green",
      [ "v15.27", "h62.5", "v190", "h48.67", "v94.67", "h-76" ]
    ],
    [ "gnd1:GND", "ultrasonic1:GND", "black", [ "v-10.86", "h289.02", "v-186", "h-62.67" ] ],
    [ "nano:3", "btn1:1.l", "green", [ "v-190.73", "h56.4", "v-93.33" ] ],
    [ "nano:GND.2", "btn1:2.l", "black", [ "v-203.4", "h47.2", "v-56" ] ],
    [ "nano:6", "sw1:2", "green", [ "v-79.2", "h-177.8" ] ],
    [ "nano:5", "sw3:2", "green", [ "v-77.2", "h166.6" ] ],
    [ "nano:12", "sw2:2", "green", [ "v-86.53", "h33.8" ] ],
    [ "gnd1:GND", "sw1:1", "black", [ "v-97.99", "h32.68" ] ],
    [ "gnd1:GND", "sw2:1", "black", [ "v-10.65", "h182.68" ] ],
    [ "gnd1:GND", "sw3:1", "black", [ "v-10.85", "h387.98" ] ],
    [ "vcc1:VCC", "sw1:3", "red", [ "v242.53", "h61.13" ] ],
    [ "sw1:3", "sw2:3", "red", [ "v78.54", "h152.93" ] ],
    [ "sw2:3", "sw3:3", "red", [ "v76.94", "h200.93" ] ]
  ],
  "dependencies": {}
}

Hey, credit to you for what you've done so far!

This doesn't change the code but might make it easier to read whic can save debug time;

  if(millis() - buttonPressed < trackTime) { //IF THE TIME RIGHT NOW - TIME WHEN BUTTON WAS PRESSED IS LESS THAN 10 SECONDS THEN FOLLOW LINE

    if (distance < 20)  //OBSTACLE AHEAD STOP
    { 
      digitalWrite(rightmotorPin, 0);
      digitalWrite(leftmotorPin, 0);
    } 
    else 
    {
      if ((leftVal == 0) && (centerVal == 0) && (rightVal == 0))  //IDEAL CASE
      {
        digitalWrite(rightmotorPin, PWMspeed);
        digitalWrite(leftmotorPin, PWMspeed);
      }     
      else if ((leftVal == 0) && (centerVal == 1) && (rightVal == 1))  //RIGHT SENSOR ON LINE
     {
       digitalWrite(rightmotorPin, 0);
      digitalWrite(leftmotorPin, PWMspeed);
    }
    else if ((leftVal == 1) && (centerVal == 1) && (rightVal == 0))  //LEFT SENSOR ON LINE
    {


Lining up braces vertically makes it way easier/faster to check and to see structures out of sa hill of text. It's more to do with how much attention you spend just finding braces in brick-code. I dunno about you but I have a limited supply of that and when it runs out (feel tired) then stubbornly continuing to work is how to write bugs.

Get the line follower to work first. I see nothing that will block the time part.
I would like to get you to add a button handler without trying to integrate that into the line-follower code. The follower will be fine with stop and go on a flag variable == 0 or 1 from code in void loop() that is not the follower but runs along with it.

It should be a big step towards how to write smaller, less complicated sketches.

Wow this simulator helps a ton with testing but for me the right and left led are turned off whenever the distance value goes below 20 yes and my button value is always high

However, when I press start in sim the code is autoatically running regardless of the button press I shall modify the code and upload it here on this forum reply please check if it is right.

The line follower works and thank you for the advice about the curly braces alignment I am adapted to using the before format with the braces aligned in a different way.

Adding an indent level every time.... put the braces where they work for you but you can ladder if-else logic straight down the page. I use white space to make structure easier to see. My eyes are old but I started doing these things when I wrote 1000's of lines of code (with libraries added, 20,000 to 35,000 where I wrote maybe 8000). Cutting out time to hunt through structures led me to write as open to view as possible.

I will drag out a button handler that runs in loop() with non-blocking code so you don't have to tackle button bouncing this week. The stop/go part will show how to use it.
Right now I'm winding down, messing with code like this is asking for bugs.

I put some notes in the top of the simulation... observations that might help... including the points you mentioned in post #35.

If you do not want the motors to run as soon as the simulation starts, putting the entire code inside the "if button" condition is your first step.

There may be some initializing when starting after a stop. Does the line follower save previous conditions/states or is it always "in the moment"?

Line follower as a function would make loop() easier to read.

I think it doesnt save the previous condition.