Power button for obstacle detecting robot

The button should be able to turn off and on the arduino, but I don't know where to start. I'm using a L293D shield and planning to set the button to pin 10.
This is the program without the button:

#include <AFMotor.h> 
int trigPin1 = A5;
int echoPin1 = A4;
int trigPin2 = A3;
int echoPin2 = A2;
int trigPin3 = A1;
int echoPin3 = A0;

AF_DCMotor motor1(3);  
AF_DCMotor motor2(4);
long duration1;
long duration2;
long duration3;
int distanceleft;
int distancefront;
int distanceright;

void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin1, INPUT); 
  pinMode(echoPin2, INPUT);
  pinMode(echoPin3, INPUT);
  Serial.begin(9600); 
  
  motor1.setSpeed(255);   
  motor2.setSpeed(255);

}
void loop() {
  
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distanceleft = duration1 * 0.034 / 2;
  Serial.print("Distance1: ");
  Serial.println(distanceleft);
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distancefront = duration2 * 0.034 / 2;
  Serial.print("Distance2: ");
  Serial.println(distancefront);
  
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distanceright = duration3 * 0.034 / 2;
  Serial.print("Distance3: ");
  Serial.println(distanceright);
  
  if ((distanceleft <= 15 && distancefront > 15 && distanceright <= 15) || (distanceleft > 15 && distancefront > 15 && distanceright > 15))
  {
    motor1.run(FORWARD);
    motor2.run(FORWARD);
  }
  if ((distanceleft <= 15 && distancefront <= 15 && distanceright > 15) || (distanceleft <= 15 && distancefront > 15 && distanceright > 15))
  {
    motor1.run(RELEASE);         
    motor2.run(RELEASE);
    delay(1000);
    motor1.run(FORWARD);         
    motor2.run(BACKWARD);        
    delay(1000); 
  }
  if ((distanceleft > 15 && distancefront <= 15 && distanceright <= 15) || (distanceleft > 15 && distancefront > 15 && distanceright <= 15) ||  (distanceleft > 15 && distancefront <= 15 && distanceright > 15) )
  {
    motor1.run(RELEASE);         
    motor2.run(RELEASE);
    delay(1000);
    motor1.run(BACKWARD);         
    motor2.run(FORWARD);        
    delay(1000);
  } 
}

Do you really want to turn the board on and off, or simply disable the code from running?

I guess the easier option is to disable the code. But, do you need an eletrical configuration like wiring to make the board on/off?

Yes, to remove power from the board, you’d have to figure out some hardware,

Enabling or Disabling the code is quite straightforward.

Add your switch or button, (optionally a global variable)…
Toggle (that variable) with) the switch, and depending on the state execute your code within loop() or not.

As always, the question is why?

You are yet to explain this.

After reading the hyperlink, here's my answer:
My original plan was to have power button like appliances. But then [lastchancename] asked me if it's to turn on/off the board or disable the program. Since I have little experience in electrical wiring, I will go with disabling the program. Therefore, the button will stop the ultrasonic sensors and motors ' program and when pressed again the program will run until the button is pressed again. I hope this answers your question and makes sense

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.