Running Two Motors seperately with 1 Push Putton each

Hi, I am trying to use Arduino rev 3 and the motor shield to control two motors on a woodworking project I am building. The motors need to turn on with the push of their respective button, and run for a few seconds then reverse direction and run in the opposite direction once the button is pressed again for the same amount of time. I'm struggling on how to code this inside the one loop so that both motors can operate independently.

const int MotorPinA = 12;
const int MotorSpeedPinA = 3;
const int MotorBrakePinA = 9;
const int PushButtonPinA = A0;

const int MotorPinB = 13;
const int MotorSpeedPinB = 11;
const int MotorBrakePinB = 8;
const int PushButtonPinB = A1;

const int CW = LOW; //Up direction for B
const int CWW = HIGH; // Down direction for B

int buttonstateB = 0;
int buttonstateA = 0;


void setup() {
  
//define pins
pinMode(MotorPinA, OUTPUT);
pinMode(MotorSpeedPinA, OUTPUT);
pinMode(MotorBrakePinA, OUTPUT);

pinMode(MotorPinB, OUTPUT);
pinMode(MotorSpeedPinB, OUTPUT);
pinMode(MotorBrakePinB, OUTPUT);
pinMode(PushButtonPinA, INPUT_PULLUP);
pinMode(PushButtonPinB, INPUT_PULLUP);


Serial.begin(9600); // serial monitor intialized
}

//Motor B
void loop() {
  //Motor B
  buttonstateB == digitalRead(PushButtonPinB);
  if(buttonstateB == HIGH){
    //start motor B at maximum speed
    digitalWrite(MotorPinB, CW); //Lift up
      Serial.println("Up Direction CW");
    analogWrite(MotorSpeedPinB,100); //set speed at maximum
      Serial.println("Speed 100");
    delay(2000); // run for 2 seconds
    digitalWrite(MotorBrakePinB, HIGH);//brake
        Serial.println("Brake Applied");
    delay(500);
    digitalWrite(MotorBrakePinB, LOW);//brake
        Serial.println("Brake Removed");
        if(buttonstateB == HIGH){
        //start motor B at maximum speed
            digitalWrite(MotorPinB, CWW); //Go down
              Serial.println("Down Direction CWW");
            analogWrite(MotorSpeedPinB,100); //set speed at maximum
              Serial.println("Speed 100");
            delay(2000); // run for 2 seconds
            digitalWrite(MotorBrakePinB, HIGH);//brake
                Serial.println("Brake Applied");
            delay(500);
            digitalWrite(MotorBrakePinB, LOW);//brake
                Serial.println("Brake Removed");
        }

  }  
  //Motor A
  buttonstateA = digitalRead(PushButtonPinA);

  if(buttonstateA == HIGH){
    //start motor B at maximum speed
    digitalWrite(MotorPinA, CW); //Lift up
      Serial.println("Up Direction CW");
    analogWrite(MotorSpeedPinA,100); //set speed at maximum
      Serial.println("Speed 100");
    delay(2000); // run for 2 seconds
    digitalWrite(MotorBrakePinA, HIGH);//brake
        Serial.println("Brake Applied");
    delay(500);
    digitalWrite(MotorBrakePinA, LOW);//brake
        Serial.println("Brake Removed");
        if(buttonstateA == HIGH){
        //start motor B at maximum speed
            digitalWrite(MotorPinA, CWW); //Go down
              Serial.println("Down Direction CWW");
            analogWrite(MotorSpeedPinA,100); //set speed at maximum
              Serial.println("Speed 100");
            delay(2000); // run for 2 seconds
            digitalWrite(MotorBrakePinA, HIGH);//brake
                Serial.println("Brake Applied");
            delay(500);
            digitalWrite(MotorBrakePinA, LOW);//brake
                Serial.println("Brake Removed");
        }

  }

}

what model motor shield are you using? i've never heard of one with a "brake" pin?

Welcome to the forums. Thanks for using code tags when posting your code.

As for your code, you need to detect when your buttons are pressed, not if they are pressed. Look at the State Change Detection example in the IDE (File->examples->02.digital->State Change Detection). You will have to do this for both your buttons.

You will also have to get rid of those delay() calls if you want both motors to run independently. Check out the Blink without Delay example.

Finally, you would be best to structure your code like a state machine. Upon startup, you are in the IDLE state. After a button press, you are in RUN state or whatever.... That way, based on what state you are in, you can measure elapsed time and/or a button press and then move to the next state.

As @blh64 pointed out, you will need to restructure your code. Here are a few tutorials to look at:

State Change Detection

BlinkWithoutDelay

Arduino Multiple Things

Several Things at a Time

State Machine

looks this over
(pins for my hardware)

// dual motor control

struct Motor {
    byte   PinBut;
    byte   PinDir;
    byte   PinEn;

    const char *desc;

    byte   butLst;
    int    state;
    int    dir;
    unsigned long msecLst;
};

Motor motors [] = {
//    But  Dir   En
    {  A1,  12,  10, "motor-A" },
    {  A2,  13,  11, "motor-B" },
};
const int Nmotor = sizeof(motors) / sizeof(Motor);

enum { CW, CCW };
enum { Off = LOW, On = HIGH };

const unsigned long MsecPeriod = 2000;

char s [80];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    Motor *m = motors;
    for (int n = 0; n < Nmotor; n++, m++)  {
        // check timer if motor running (CW/CCW)
        if (On == m->state && msec- m->msecLst > MsecPeriod)  {
            m->state = Off;
            digitalWrite (m->PinEn, Off);

            sprintf (s, " %s %s", "Stop", m->desc);
            Serial.println (s);
        }
        
        // check for button press
        byte but = digitalRead (m->PinBut);
        if (m->butLst != but)  {
            m->butLst = but;
            delay (20);         // debounce

            if (LOW == but)  {  // pressed
                // toggle direction
                if (CW == m->dir)
                    m->dir = CCW;
                else
                    m->dir = CW;
                m->msecLst = msec;
                m->state   = On;
                digitalWrite (m->PinDir, m->dir);
                digitalWrite (m->PinEn, On);

                sprintf (s, " %s %s", CW == m->dir ? "CW" : "CCW", m->desc);
                Serial.println (s);
            }
        }
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Motor *m = motors;
    for (int n = 0; n < Nmotor; n++, m++)  {
        pinMode (m->PinBut, INPUT_PULLUP);
        m->butLst = digitalRead (m->PinBut);

        pinMode (m->PinDir, OUTPUT);
        pinMode (m->PinEn,  OUTPUT);
        digitalWrite (m->PinEn, Off);
    }

    Serial.begin(9600); // serial monitor intialized
}

Maybe

It provides a brake pin.

a7

Yes that one

Would this work for my goal? or do I need to do more. I am a beginner when it comes to Arduino, and don't know much but have done other beginners level programming. Any more help is appreciated.

yes, i believe so. it incorporates the ideas @blh64 and @ToddL1962 suggested

i tested the code with buttons and LEDs i have (multi-function board). you need to change the values in motors for your hardware.

i suggest not worrying about the brake until you get on motor working the way you expect

do you understand the code i posted?

Somewhat, I will try it out and use the other resources as well. Appreciate your help. Not sure what you mean by values of the motors though.

i used pins on my hardware. you need to change the pins for you hardware

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