trying to run 2 dc motors clockwise and counterclockwise 4 buttons 2 forward buttons 2 backwards buttons I have if else statements and several other try's but no success. if comment everything out except 1 motor and one button. it will not work. I know circuit is right using a Quad half h bridge SN754410 a few jumper wires off push button also proves circuit is right.
/*
Button Testing Motors Buttons 4 Buttons hooked to ardiuno which activates states for motors foward and backwards
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonFowardRightPin = 3; // the number of the pushbutton pin
const int motorFowardRightPin = 6; // the number of the Motor pin
const int buttonBackwardsRightPin = 2; // the number of the pushbutton pin
const int motorBackwardsRightPin = 5; // the number of the Motor pin
const int buttonForwardLeftPin = 9; // the number of the pushbutton pin
const int motorFowardLeftPin = 10; // the number of the Motor pin
const int buttonBackwardsLeftPin = 8; // the number of the pushbutton pin
const int motorBackwardsLeftPin = 11; // the number of the Motor pin
const int enableMotor1And2Pin = 7; // the number of the enable 1 & 2 motor
const int enableMotor3And4Pin = 12; // the number of the enable 3 & 4 motor
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the motor pin as an output:
pinMode(motorFowardRightPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonFowardRightPin, INPUT);
// initialize the motor pin as an output:
pinMode(motorBackwardsRightPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonBackwardsRightPin, INPUT);
// initialize the motor pin as an output:
pinMode(motorFowardLeftPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonBackwardsLeftPin, INPUT);
// initialize the motor pin as an output:
pinMode(motorBackwardsLeftPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonBackwardsLeftPin, INPUT);
// intialize motor 1 & 2 motor pin as an output:
pinMode(enableMotor1And2Pin, OUTPUT);
// intialize motor 3 & 4 motor pin as an output:
pinMode(enableMotor3And4Pin, OUTPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonFowardRightPin);
buttonState2 = digitalRead(buttonBackwardsRightPin);
buttonState3 = digitalRead(buttonForwardLeftPin);
buttonState4 = digitalRead(buttonBackwardsLeftPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn motor on:
digitalWrite(motorFowardRightPin, HIGH);
digitalWrite(motorBackwardsRightPin, LOW);
digitalWrite(enableMotor1And2Pin, HIGH);
}
else {
// turn motor off:
digitalWrite(motorFowardRightPin, LOW);
digitalWrite(motorBackwardsRightPin, LOW);
digitalWrite(enableMotor1And2Pin, LOW);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn motor on:
digitalWrite(motorBackwardsRightPin, HIGH);
digitalWrite(motorFowardRightPin, LOW);
digitalWrite(enableMotor1And2Pin, HIGH);
}
else {
// turn motor off:
digitalWrite(motorBackwardsRightPin, LOW);
digitalWrite(motorFowardRightPin, LOW);
digitalWrite(enableMotor1And2Pin, LOW);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState3 == HIGH) {
// turn motor on:
digitalWrite(motorFowardLeftPin, HIGH);
digitalWrite(motorBackwardsLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, HIGH);
}
else {
// turn motor off:
digitalWrite(motorFowardLeftPin, LOW);
digitalWrite(motorBackwardsLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, LOW);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState4 == HIGH) {
// turn motor on:
digitalWrite(motorBackwardsLeftPin, HIGH);
digitalWrite(motorFowardLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, HIGH);
}
else {
// turn motor off:
digitalWrite(motorBackwardsLeftPin, LOW);
digitalWrite(motorFowardLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, LOW);
}
}