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");
}
}
}