Using a button as a switch for a motor

Hello all, this is my first forum post. :slight_smile:
I have built a car with an arduino, a motor shield, and two motors. I have added two buttons to the front that have long extenders, with the intention of coding them so that the car will back up and change direction when it gets too close to an object.
Currently the buttons work as pushbuttons in that they change direction DURING the pressing. Here is that code: (My wheels are wired such that HIGH and LOW make the car go forword, HIGH and HIGH make it turn)

int speed1  = 3;
int speed2  = 11;
int direction1 = 12;
int direction2 = 13;
int leftbutton = 5;
int rightbutton = 6;
int buttonStateright = 0;
int buttonStateleft = 0;

void  stopMotor()  {
// turn both  motors  off 
analogWrite(speed1, 0);
analogWrite(speed2, 0);
}

void  setup() {
  Serial.begin(9600);
  pinMode(rightbutton, INPUT);
  pinMode(leftbutton, INPUT);
  pinMode(speed1, OUTPUT); 
  pinMode(speed2, OUTPUT); 
  pinMode(direction1, OUTPUT);
  pinMode(direction2, OUTPUT);
}

void  loop() {
  int buttonStateleft = digitalRead(leftbutton);
  int buttonStateright = digitalRead(rightbutton);
  Serial.println(buttonStateright);
  delay(1);

  if(buttonStateleft || buttonStateright == HIGH)
  {
    digitalWrite(direction1, HIGH); 
digitalWrite(direction2,HIGH); 
analogWrite(speed1,255);
analogWrite(speed2,255);
}
else
{
digitalWrite(direction1, HIGH); 
digitalWrite(direction2,LOW); 
analogWrite(speed1,255);
analogWrite(speed2,255);
}
  }

What I need to do is use the buttons as switches so that when it is pressed for just a split second, the car will switch into reverse mode and complete the backing up and turning motion, before continuing forward. Right now the car simply turns as it hits an object, and this is not what I want.

I understand how I would accomplish this change if my only output were an LED, there are plenty of examples for that, like this one: https://www.arduino.cc/en/Tutorial/Switch

What I don't understand is how I can synthesize that code with mine given that mine is using 4 pins for the motor shield instead of a simple single output pin. Any help would be greatly appreciated!
-Thank you, Sam

I think this will be much easier to conceptualize and to realize if you take a little time to reorganize your code into functions as illustrated in Planning and Implementing a Program

If you have a function to control the motor that takes a parameter (say 'F', 'B', 'R', 'L') for Forward, Backward, Right and Left and another function to read and save the button states. Then you can have a function to figure out what to do based on the button states. It can tell the motor to move in whatever direction. The code in loop() would be something like

void loop() {
   readButtons();
   analyzeStates();
}

and the code in analyzeStates() might be like this

void analyzeStates() {
    if (buttonStateA == HIGH && buttonStateB == LOW) {
       motorMove('F');
    }
    if (buttonStateA == HIGH && buttonStateB == HIGH) {
       motorMove('L');
    }
}

And don't use the delay() function anywhere. Use millis() to manage timing as illustrated in Several Things at a Time

It may also be necessary to keep track of the state of your car - for example is it turning, or moving forward etc so that you could allow it to continue in that state for a period of time or until something else happens. This is referred to as a "state machine" if you want to read more about the concept.

...R

Thank you for helping. I'm looking at your planning and implementing thread, and it looks very helpful and straightforward. But I'm still confused as to how your example would give me a switch in direction that wouldnt only be active while the button was pressed...
-Sam

stiktourgunz:
But I'm still confused as to how your example would give me a switch in direction that wouldnt only be active while the button was pressed...

I guess I don't understand what you don't understand.

My concept is that when a switch gets pressed a new "action" will result and it does not necessarily stop when the switch is released, unless you want it to stop.

Can you explain more clearly what you want to happen by writing down a series of steps, one on each line.

...R

'm still confused as to how your example would give me a switch in direction that wouldnt only be active while the button was pressed

You need to be clear whether you want something to happen when a button becomes pressed or when it is pressed. Maybe forward and back are activated by a button becoming pressed whilst left and right are only active when a button is pressed. You choose.

If I understand you correctly, you want to remember when button was pressed. So I suggest that you use a variable for that.

When the button is pressed, you set that variable to true (or high or 1 or whatever).

Something like

byte forward = 0;
byte reverse = 0;

void setup()
{
}

void loop()
{
    
    if(buttonForward == HIGH)
    {
        forward = 1;
        reverse = 0;
    }


    if(buttonReverse == HIGH)
    {
        forward = 0;
        reverse = 1;
    }
}

In your motor control function, you now use forward and reverse to decide.

Note: I did not look at your code; the above assumes that the button input is normally low and goes high when the button is pressed.

Okay, here's some clarification:
@Robin2: I think I'm missing something obvious. i just don't have enough experience with arduino to figure out all of the stuff you are saying in your other post. It looks very professional and helpful though.

Anyways, this is what my car does now: During the button being pressed (as in while i am actively holding it down) the car changes from forward to reverse.
This is what I want it to do: It should move forward until the button is pressed (not held) and then SWITCH (not yelling, just emphasis) to the reverse direction. If I get there it will be simple to add code for it to then turn and resume forward motion.

I am now trying to use the method from this sketch: https://www.arduino.cc/en/Tutorial/Debounce

/*
 Debounce

 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.


 created 21 November 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 modified 28 Dec 2012
 by Mike Walters

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

What I run into trouble with is the part directly below "// only toggle the LED if the new button state is HIGH." Thats where I dont know what to do for my code, since its not an LED, and I dont understand exactly what its saying in the actual code anyway.

Here is my current code up till that point.

int speed1  = 3;
int speed2  = 11;
int direction1 = 12;
int direction2 = 13;
int button = 5;
int ledState = HIGH
int buttonState;
int lastButtonState = LOW
long lastDebounceTime = 0;
long debounceDelay = 70;

void Back() {
digitalWrite(direction1, LOW); 
digitalWrite(direction2,HIGH); 
analogWrite(speed1,255);
analogWrite(speed2,255);}

void Forward() {
digitalWrite(direction1, HIGH); 
digitalWrite(direction2,LOW); 
analogWrite(speed1,255);
analogWrite(speed2,255);
}

void setup()  {
  // put your setup code here, to run once:
pinMode(button, INPUT);
pinMode(speed1, OUTPUT); 
pinMode(speed2, OUTPUT); 
pinMode(direction1, OUTPUT);
pinMode(direction2, OUTPUT);

digitalWrite(direction1, HIGH); 
digitalWrite(direction2,LOW); 
analogWrite(speed1,255);
analogWrite(speed2,255);

}

void loop() {
  // put your main code here, to run repeatedly:
int reading = digitalRead(button);

if(reading != lastButtonState) { 
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
  if (reading != buttonState) {
    buttonState = reading;
    if (buttonState == HIGH) {
   //What goes here for this particular case? 
    }
  }
}

If anyone could explain the if statement here that would be great. I understand the theory, I just dont quite see it in the code. Also, if you know what I would put there instead in MY code, please explain.

Again, thanks to everyone for their help. Im getting a lot closer to understanding this and getting it to work.

Okay, here's a very specific question that might really help my understanding. I understand how you can have a variable called "ledState", and set it to high. How can you make a variable that does the same thing for the motor, since it includes two different pins?

If Reply #9 has not answered your question I suggest you write a very short program that reads your buttons and displays the values of the button state variables on the serial monitor. That should help you to figure out the button process, and it will make it much easier for us to help you.

...R