Toggle Button Activated Program

Hi all,

I'm having issues coding a toggle button activated program such that when I hit the button, it'll initiate the programs, and when I hit it again, it'll stop the program. With my current code, I have to hold down the button a half a second, and then it'll run the program once and then stop. Any suggestions/fixes to this issue? I have attached the fritzing diagram.

Thank you.

int speedPin1=4;
int speedPin2=5;
int dir1=3;
int dir2=2;
int dir3=6;
int dir4=7;
int speed;
//For DC Motors 

int trigPin = 8;
int echoPin = 9;
long distance;
// For Ultrasonic Sensor 

int buttonPin=13;
int buttonNew;
int buttonOld=1;
int State=0;

// For Button





void setup() {
pinMode(speedPin1,OUTPUT);
pinMode(speedPin2,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(dir3,OUTPUT);
pinMode(dir4,OUTPUT);
//For DC Motors 
//3 seconds for 360 turn 

pinMode(trigPin, OUTPUT);
digitalWrite (trigPin, LOW);
pinMode(echoPin, INPUT);
//For Ultrasonic Sensor 

pinMode(buttonPin, INPUT);
//For button 

Serial.begin(9600);

}




void Forward() {
Serial.println("Forward");
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
digitalWrite(dir3,HIGH);
digitalWrite(dir4,LOW);
}
//Robot will go Forward




void Reverse() {
Serial.println("Reverse");
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
digitalWrite(dir3,LOW);
digitalWrite(dir4,HIGH);
}
//Robot will Reverse 




void TurnLeft() {
Serial.println("TurnLeft");
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
digitalWrite(dir3,HIGH);
digitalWrite(dir4,LOW);
}
//Robot will turn left 




void TurnRight() {
Serial.println("TurnRight");
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
digitalWrite(dir3,LOW);
digitalWrite(dir4,HIGH);
}
//Robot will turn right 




void Stop() {
Serial.println("Stop");
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
digitalWrite(dir3,LOW);
digitalWrite(dir4,LOW);

}
//Robot will stop





void Distance (){
digitalWrite(trigPin, LOW); 
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); 
delayMicroseconds(10);
Serial.print ("Distance: ");
distance = pulseIn(echoPin, HIGH) * 0.034 / 2.0;
delay(200);

if (distance >= 150){
  Serial.println("Out of Range");
  Stop();
  delay(500);
  TurnLeft();
  delay(random(500,2100));
  TurnRight();
  delay(random(100,1500));
  Stop();
  delay(1000);
  Forward();
  }
  
else {
  Serial.print (distance);
  Serial.println ("cm"); 
//Reads distance. If/else statement sets maximum distance
}
}



void Avoid (){
  if (distance < 15)
// distance can be between 45 or 60 to avoid obstacle correctly at an angle
  { 
Stop();  
delay(10000);   
Reverse();
delay(random(1000,3000));
TurnRight();
  }
}
//avoidance program




void loop() {

buttonNew=digitalRead(buttonPin);


if(buttonOld==0 && buttonNew==1){
 
if (State==0){
Serial.println("ON");
delay(1000);
Distance();
Avoid ();
Forward();
State=1;
  }
}

else {
Serial.println("OFF");
delay(2000);
Stop();
State=0;
}

buttonOld=buttonNew;
delay(100);
}

test_2.ino (2.54 KB)

If you post your code as described in the forum guidelines more members will see your code.

Delays block execution and make the program unresponsive. Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Your button has no pullup resistor so its input pin is floating when the button is not pressed. Enable the internal pullup with pinMode(pin, INPUT_PULLUP).

The state change detection example may help with the button toggling a state. See also state change with active low button.

You can take a look to How to use button to start/stop program