Trouble with using slide switch to turn on and off a bot

So I'm trying to use a slide switch to turn on and off a bot but. When I do not include the code for the switch:

while (digitalRead(switchpin) == HIGH)
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
}

Everything works as normal, my bot goes forward and when the ultrasonic sensor gets within a distance it will stop.
During this time the serial monitor continuously shows the distance being given to the computer.

But when I do include this piece of code, the switch in the HIGH position will keep the system off. When I put it to the other position, the bot will begin to move forward, but the ultrasonic sensor stops sending information to the bot and therefore does not stop after reaching a certain distance.

At the bottom I have include the full code. If there is an easier way to use the switch then that is fine. But basically I just need the switch to make the bot turn on or off. I have no idea why the switch makes the sensor not work.

#include <elapsedMillis.h>

int motor1forward = 12;
int motor1backward = 13;
int motor2forward = 2;
int motor2backward = 3;

const int trigPin = 4;
const int echoPin = 6;

long duration;
int distance;

int switchpin = 8;

elapsedMillis timeElapsed;

void setup() {
pinMode(motor1forward, OUTPUT);
pinMode(motor1backward, OUTPUT);
pinMode(motor2forward, OUTPUT);
pinMode(motor2backward, OUTPUT);
//pinMode(speed1Pin, OUTPUT);
//pinMode(speed2Pin, OUTPUT);

//digitalWrite(speed1Pin, HIGH);
//digitalWrite(speed2Pin, HIGH);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.begin(9600);
}

void loop() {

while (digitalRead(switchpin) == HIGH);
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
}

while (timeElapsed >= 60000)
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
}

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

Serial.print("Distance: ");
Serial.println(distance);

if ( distance > 10 )
{
digitalWrite(motor1forward, HIGH);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, HIGH);
digitalWrite(motor2backward, LOW);
}

/*else if ( distance <= 10 )
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, HIGH);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, HIGH);
delay(2000);

digitalWrite(motor1forward, HIGH);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
delay(1500);

}*/

else if ( distance <= 10 )
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
}


}
while (timeElapsed >= 60000)
{
digitalWrite(motor1forward, LOW);
digitalWrite(motor1backward, LOW);
digitalWrite(motor2forward, LOW);
digitalWrite(motor2backward, LOW);
}

timeElapsed is never updated in the while loop. What are you trying to do? Stop?

Hi,
Welcome to the forum.

How have you got your switch wired?
Do you have a pull down resistor fitted?
A 10K from gnd to the Arduino input pin your are switching will help.

Tom.... :slight_smile: