Pages: [1]   Go Down
Author Topic: Adding a push button to a dual motor control sketch  (Read 1133 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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.

Code:
// 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 :/ 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
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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}
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?...
Code:
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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

In what way did it not work?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3651
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No I ment this.

Code:
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);
}
« Last Edit: January 11, 2013, 07:21:15 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
smiley
Logged

Pages: [1]   Go Up
Jump to: