Control DC Motor w. 3 pushbuttons

The following code is originally from "elmar"'s post "Control DC Motor w. Limit switches"
http://forum.arduino.cc/index.php?topic=433556.0

Function:

"Hy @ all

like the title says i want to control an dc motor with limit switches.
Overall i´m planning to use 3 pushbuttons:

Button 1: Should start the motor

button 2: should stop the motor after it reaches a specific position

button 3: should do the same like button 2 only on the other end of the rotation

It should work like this:

Button 1 is pressed: the motor turns clockwise until it reaches the position of the 2nd button.
By pressing the second button the motor stops.

After hitting button 1 again: the motor spins counterclockwise until it reaches button 3, then the motor stops, a counter for button 1 gets a reset so that the loop is finished and can be restarted....

I already tried to build a sketch for that purpose but i´m not sure if it will work or if i´m missing something:

I just tested this code, when press button#1, DC motor ran CW, when press button(limit Switch)#2, or #3, motor didn't stop, press button#1 again, motor ran CCW.

Can somebody check this code error? This code function is exactly what I need.

Thanks a lot in advance!

int enableA = 6;              // Motor A, PWM-Pin
int out1A = 7;                // Motor A, control line 1
int out2A = 2;                // Motor A, control line 2
int buttonPin1 = 8;
int buttonPin2 = 9;
int buttonPin3 = 10;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonPushCounter = 0;      // counter for the number of button presses
int lastButtonState = 0;        // previous state of the button



void setup() {
 
Serial.begin(9600);

pinMode(enableA, OUTPUT);
pinMode (out1A, OUTPUT);
pinMode (out2A, OUTPUT);
pinMode(buttonPin1,INPUT); 
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);                       
digitalWrite(enableA, HIGH);
   
}

void loop()
{
buttonState = digitalRead(buttonPin1);                  // read the pushbutton input pin
  if (buttonState != lastButtonState) {                 // compare buttonState to previous state
lastButtonState = buttonState;                          // if the current state is 1 then the button
  if (buttonState == 1) {                               // went from off to on:
                                                       
    buttonPushCounter++;                                // buttoncounter +1
    Serial.println("on");
    Serial.print("  ");
    Serial.println(buttonPushCounter, DEC);

      switch (buttonPushCounter) {



  case 1:                                                 // If the button count is at 1
  Serial.println("1");                                    // print 1
  buttonState2 = digitalRead(buttonPin2);                 // check limit switch1
  if (buttonState2 == HIGH) {                             // if limit switch1 is pressed
  digitalWrite(out1A, LOW);                               // set pin 2 on L293D low
  digitalWrite(out2A, LOW); }                             // set pin 7 on L293D low
  else {                                                  // else
  digitalWrite(out1A, HIGH);                              // set pin 2 on L293D high
  digitalWrite(out2A, LOW);                               // set pin 7 on L293D low
  }
 
        break;
       

case 2:                                                     // If the button count is at 2
  Serial.println("2");                                      // print 1
  buttonState3 = digitalRead(buttonPin3);                   // check limit switch2
  if (buttonState3 == HIGH) {                               // if limit switch2 is pressed
  digitalWrite(out1A, LOW);                                 // set pin 2 on L293D low
  digitalWrite(out2A, LOW); }                               // set pin 7 on L293D low
  else {                                                    // else
  digitalWrite(out1A, LOW);                                 // set pin 2 on L293D low
  digitalWrite(out2A, HIGH);     }                          // set pin 7 on L293D high
  buttonPushCounter = 0;
 
        break;
  }     
  }      }
  }

If you use the AutoFormat tool to lay out your code better I think you will see that it is a bit of a mess. It looks like different parts of the logic are all mixed up.

Try this version. I have checked that it compiles but I have not tested it so I may have the directions mixed up . If it is not working properly tell me what it actually does.

int motorAenablePin = 6;              // Motor A, PWM-Pin
int motorAfwdPin = 7;                // Motor A, control line 1
int motorArevPin = 2;                // Motor A, control line 2
boolean motorRun = false;
char motorDirection = 'F';

int controlButtonPin = 8;
int controlButtonState = 0;
int buttonPushCounter = 0;      // counter for the number of button presses
int lastControlButtonState = 0;        // previous state of the button

int rightLimitSwitchPin = 9;
int leftLimitSwitchPin = 10;

int rightLimitswitchState = 0;
int leftLimitswitchState = 0;




void setup() {

    Serial.begin(9600);

    pinMode(motorAenablePin, OUTPUT);
    pinMode (motorAfwdPin, OUTPUT);
    pinMode (motorArevPin, OUTPUT);
    digitalWrite(motorAenablePin, HIGH);

    pinMode(controlButtonPin,INPUT);
    pinMode(rightLimitSwitchPin,INPUT);
    pinMode(leftLimitSwitchPin,INPUT);

}

//=========

void loop() {
    readSwitches();
    updateMotorControl();
    moveMotor();
}

//=========

void readSwitches() {
    rightLimitswitchState = digitalRead(rightLimitSwitchPin);
    leftLimitswitchState = digitalRead(leftLimitSwitchPin);

    controlButtonState = digitalRead(controlButtonPin);
    buttonPushCounter = 0;
    if (controlButtonState != lastControlButtonState) {
        lastControlButtonState = controlButtonState;
        if (controlButtonState == 1) {
            buttonPushCounter = 1;
            Serial.println("on");
        }
    }
}

//===========

void updateMotorControl() {
    if (rightLimitswitchState == HIGH) {
        motorRun = false;
        motorDirection = 'F';
    }
    if (leftLimitswitchState == HIGH) {
        motorRun = false;
        motorDirection = 'R';
    }
    if (buttonPushCounter == 1) {
        motorRun = true;
    }
}

//===========

void moveMotor() {
    if (motorRun == true) {
        if (motorDirection == 'F') {
            digitalWrite(motorArevPin, LOW);  // write LOW before HIGH
            digitalWrite(motorAfwdPin, HIGH);
        }
        else {
            digitalWrite(motorAfwdPin, LOW);
            digitalWrite(motorArevPin, HIGH);
        }
    }
    else {
        digitalWrite(motorAfwdPin, LOW);
        digitalWrite(motorArevPin, LOW);
    }
}

...R

Test Result:

  1. Press button#1, started DC motor(CW)
  2. Press button#2, should stop the motor, But actually NOT, Motor kept running same.
  3. Press button#3, should stop the motor, But actually NOT, Motor kept running same.
  4. Press button#1 again, Direction of DC motor changed(CCW).

I did check wiring, they are correct.
(All 3 push buttons are momentary switch)

Thanks!

Richard905:
I did check wiring, they are correct.

Because I don't have your hardware I may have things mixed up.

While the motor is moving left press the left end-stop with your finger and if the motor does not stop then press the right end-stop. I may have them mixed up. If neither finger press causes the motor to stop then let me know. But see the next bit first.

One thing I meant to ask is whether you have external pull-down resistors on all your switch pins to prevent the pins floating when the switches are open?

If you have not got them then a simpler (and better) option is to use the internal pull-up resistors pinMode(pin, INPUT_PULLUP), wire the switches so they pull the pin to GND when pressed and change the program logic to expect a LOW when the switch is pressed.

...R

I have external pull-down resistors, but no response from button#2, #3 at all.

Could somebody help me convert above code to State Machine Code?

Thanks in advance!

Richard905:
Could somebody help me convert above code to State Machine Code?

The code I suggested is a state machine.

Post the code you have uploaded to YOUR Arduino.

Post a photo of a drawing showing all your connections

Post a photo of your machine with arrows added to illustrate the directions of the motor and which switch is which.

...R