Adding a push button to a dual motor control sketch

Hello again,

I apologize very much for my frequent and annoying questions, but I have not been successful with my many attempts and have become incredibly frustrated. So, I have resorted to asking yet another question.
Basically, I want to add a simple push button switch to power the Arduino and begin my sketch. I need to be able to hold down the button during the entire sketch. Once the sketch is finished, I will let go of the button and it will stop the sketch and power off the Arduino (I will know when the sketch ends).
I tried using a circuit to power up the Arduino and start the sketch without adding a code for the button and it worked, but it didn't start the sketch from the beginning.
I have tried everything to add the button into the circuit and the sketch, but nothing seemed to work right.

I'm sure if I was just using an LED, it wouldn't be so tough, but I'm using 2 motors. I don't know why I'm having such a hard time with this. All I want it to do is to begin the sketch when I hold it down and stop the sketch when I let go, and not start from a different part of the sketch when I press the button again.

Here is the code I'm using...without my attempts of adding a switch

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”Β  Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin =Β  13;Β  Β  // LED connected to digital pin 13

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Loop
void loop() {

drive_right_backward();
delay(200);
motor_stop();
Serial.println("1");

motor_stop();
delay(500);
motor_stop();
Serial.println("2");

drive_left_backward();
delay(200);
motor_stop();
Serial.println("3");

motor_stop();
delay(500);
motor_stop();
Serial.println("4");

drive_right_backward();
delay(200);
motor_stop();
Serial.println("5");

motor_stop();
delay(1500);
motor_stop();
Serial.println("6");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("7");

motor_stop();
delay(500);
motor_stop();
Serial.println("8");

drive_left_forward();
delay(200);
motor_stop();
Serial.println("9");

motor_stop();
delay(500);
motor_stop();
Serial.println("10");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("11");

motor_stop();
delay(500);
motor_stop();
Serial.println("12");

digitalWrite(ledPin, HIGH);Β   // set the LED on
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second
Β  digitalWrite(ledPin, LOW);Β  Β  // set the LED off
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void drive_left_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
}

void drive_left_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
}

void drive_right_forward(){
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_right_backward(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

Here is what I think the code partially looks like with the switch...I know its not much, but its all I'm sort of certain about.

// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”Β  Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin =Β  13;Β  Β  // LED connected to digital pin 13
int buttonPin = 4;
int buttonState = 0;

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Loop
void loop() {
Β  
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Β  digitalWrite(motor_left[], HIGH);
Β  digitalWrite(motor_right[], HIGH);
}
else {
Β  digitalWrite(motor_left[], LOW);
Β  digitalWrite(motor_right[], LOW);
}

drive_right_backward();
delay(200);
motor_stop();
Serial.println("1");

motor_stop();
delay(500);
motor_stop();
Serial.println("2");

drive_left_backward();
delay(200);
motor_stop();
Serial.println("3");

motor_stop();
delay(500);
motor_stop();
Serial.println("4");

drive_right_backward();
delay(200);
motor_stop();
Serial.println("5");

motor_stop();
delay(1500);
motor_stop();
Serial.println("6");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("7");

motor_stop();
delay(500);
motor_stop();
Serial.println("8");

drive_left_forward();
delay(200);
motor_stop();
Serial.println("9");

motor_stop();
delay(500);
motor_stop();
Serial.println("10");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("11");

motor_stop();
delay(500);
motor_stop();
Serial.println("12");

digitalWrite(ledPin, HIGH);Β   // set the LED on
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second
Β  digitalWrite(ledPin, LOW);Β  Β  // set the LED off
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void drive_left_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
}

void drive_left_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
}

void drive_right_forward(){
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_right_backward(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

I really need help :confused: I'm sorry to bother everyone.
I'm also not sure how to wire it anymore. I'm having major brain fart from all of the unsuccessful attempts I have had.

Thank you,
Matt

Hi
when you say "not start from a different part" do you mean releasing the button simply pauses the sketch where it is and then continues from where it left off when you let go?

That could be what happens, I honestly don't know. I probably should have watched the Serial Monitor. But that sounds more like what is happening. To me, It seemed as though it was continue the program even though the button was not being pressed.

Why are you not returning to your loop after you go forward, backwards an stop?

Void DoSomething(){
.
.
.
return; // goes back to loop
}

In fact if you just want to move while the button is pressed then make your motor calls in a completely different function and ONLY have if(button == HIGH){ go to that function} else {do nothing}

mfkubak:
I want to add a simple push button switch to power the Arduino and begin my sketch. I need to be able to hold down the button during the entire sketch. Once the sketch is finished, I will let go of the button and it will stop the sketch and power off the Arduino (I will know when the sketch ends).

It seems to me that a momentary switch in series with the power supply would do what you want. If you operate the switch the Arduino boots and runs your sketch. If you release the switch the Arduino is powered off immediately.

HazardsMind - I don't return to loop because after that longer stop, the motors drive in reverse. If I went back to loop, they would drive in the same direction as the first "set".
Do you mean this?...

int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int ledPin = Β 13; Β  Β // LED connected to digital pin 13
int buttonPin = 4;
int buttonState = 0;

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Loop
void loop() {
 Β 
if (buttonPin == HIGH); {

drive_right_backward();
delay(200);
motor_stop();
Serial.println("1");

motor_stop();
delay(500);
motor_stop();
Serial.println("2");

drive_left_backward();
delay(200);
motor_stop();
Serial.println("3");

motor_stop();
delay(500);
motor_stop();
Serial.println("4");

drive_right_backward();
delay(200);
motor_stop();
Serial.println("5");

motor_stop();
delay(1500);
motor_stop();
Serial.println("6");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("7");

motor_stop();
delay(500);
motor_stop();
Serial.println("8");

drive_left_forward();
delay(200);
motor_stop();
Serial.println("9");

motor_stop();
delay(500);
motor_stop();
Serial.println("10");

drive_right_forward();
delay(200);
motor_stop();
Serial.println("11");

motor_stop();
delay(500);
motor_stop();
Serial.println("12");

digitalWrite(ledPin, HIGH); Β  // set the LED on
 Β delay(1000); Β  Β  Β  Β  Β  Β  Β  Β  Β // wait for a second
 Β digitalWrite(ledPin, LOW); Β  Β // set the LED off
 Β delay(1000); Β  Β  Β  Β  Β  Β  Β  Β  Β // wait for a second

}
if else{do nothing};
}

PeterH - Thats what I thought! But it didn't seem to work?

mfkubak:
PeterH - Thats what I thought! But it didn't seem to work?

In what way did it not work?

No I ment this.

int motor_left[] = {
Β  2, 3};
int motor_right[] = {
Β  7, 8};
int ledPin =Β  13;Β  Β  // LED connected to digital pin 13
byte button; //ADDED
int buttonPin = 4;
int buttonState = 0;

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Setup
void setup() {
Β  Serial.begin(9600);

Β  // Setup motors
Β  int i;
Β  for(i = 0; i < 2; i++){
Β  Β  pinMode(motor_left[i], OUTPUT);
Β  Β  pinMode(motor_right[i], OUTPUT);
Β  Β  pinMode(ledPin, OUTPUT);
Β  Β  pinMode(buttonPin, INPUT);
Β  }

}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Loop
void loop() {
Β  button = digitalRead(buttonPin);

Β  if (button == HIGH) Drive();Β  
}

void Drive()
{
Β  drive_right_backward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("1");

Β  motor_stop();
Β  delay(500);
Β  motor_stop();
Β  Serial.println("2");

Β  drive_left_backward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("3");

Β  motor_stop();
Β  delay(500);
Β  motor_stop();
Β  Serial.println("4");

Β  drive_right_backward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("5");

Β  motor_stop();
Β  delay(1500);
Β  motor_stop();
Β  Serial.println("6");

Β  drive_right_forward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("7");

Β  motor_stop();
Β  delay(500);
Β  motor_stop();
Β  Serial.println("8");

Β  drive_left_forward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("9");

Β  motor_stop();
Β  delay(500);
Β  motor_stop();
Β  Serial.println("10");

Β  drive_right_forward();
Β  delay(200);
Β  motor_stop();
Β  Serial.println("11");

Β  motor_stop();
Β  delay(500);
Β  motor_stop();
Β  Serial.println("12");

Β  digitalWrite(ledPin, HIGH);Β   // set the LED on
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second
Β  digitalWrite(ledPin, LOW);Β  Β  // set the LED off
Β  delay(1000);Β  Β  Β  Β  Β  Β  Β  Β  Β  // wait for a second
}

// β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” Drive

void motor_stop(){
Β  digitalWrite(motor_left[0], LOW);
Β  digitalWrite(motor_left[1], LOW);

Β  digitalWrite(motor_right[0], LOW);
Β  digitalWrite(motor_right[1], LOW);
Β  delay(25);
}

void drive_forward(){
Β  digitalWrite(motor_left[0], HIGH);
Β  digitalWrite(motor_left[1], LOW);

Β  digitalWrite(motor_right[0], HIGH);
Β  digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
Β  digitalWrite(motor_left[0], LOW);
Β  digitalWrite(motor_left[1], HIGH);

Β  digitalWrite(motor_right[0], LOW);
Β  digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
Β  digitalWrite(motor_left[0], LOW);
Β  digitalWrite(motor_left[1], HIGH);

Β  digitalWrite(motor_right[0], HIGH);
Β  digitalWrite(motor_right[1], LOW);
}

void turn_right(){
Β  digitalWrite(motor_left[0], HIGH);
Β  digitalWrite(motor_left[1], LOW);

Β  digitalWrite(motor_right[0], LOW);
Β  digitalWrite(motor_right[1], HIGH);
}

void drive_left_forward(){
Β  digitalWrite(motor_left[0], HIGH);
Β  digitalWrite(motor_left[1], LOW);
}

void drive_left_backward(){
Β  digitalWrite(motor_left[0], LOW);
Β  digitalWrite(motor_left[1], HIGH);
}

void drive_right_forward(){
Β  digitalWrite(motor_right[0], HIGH);
Β  digitalWrite(motor_right[1], LOW);
}

void drive_right_backward(){
Β  digitalWrite(motor_right[0], LOW);
Β  digitalWrite(motor_right[1], HIGH);
}

PeterH - I was able to make it work. My problem was the LED and the way I wired it. I'm going to keep this wiring.
HazardsMind - I understand what you meant now. I didn't know you were able to do that with a sketch. I really appreciate the help.

Thank you to both of you! Forgive me for my noob questions.
:slight_smile: