having trouble with code not sure whats wrong

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

Do you have pulldown resistors connected to the switch inputs?

Welcome to the forum. Please read the guidelines on the top of the page about how to properly post your code using tags so it makes it easier for people to help you.

At first glance, your logic is not correct. What happens if you only press button 1? Your first if() statement turns on your Right motor. Then, the very next if() statement tests button 2 which is not pressed and you turn OFF your Right motor. Probably not what you want.

I agree with @blh64. You need to do a bit more thinking about how you're processing the switches.

For example, priority: If button one is pressed ignore the state of button two.

If neither button 1 nor button 2 is pressed then turn off the motor.

Something like:

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 right motor on "forward":    
        digitalWrite(motorFowardRightPin, HIGH);
        digitalWrite(motorBackwardsRightPin, LOW);
        digitalWrite(enableMotor1And2Pin, HIGH);    
    }
    else if( buttonState2 == HIGH )
    {
        // turn right motor on "backward"
        digitalWrite(motorBackwardsRightPin, HIGH);
        digitalWrite(motorFowardRightPin, LOW);
        digitalWrite(enableMotor1And2Pin, HIGH);
    }
    else
        //turn right motor off
        digitalWrite(enableMotor1And2Pin, LOW);

.
.
.

thank you for the help the code very much the same except // check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if( buttonState1 == HIGH ) is this part of an array or Boolean or can you explain what was or is missing when verified the code there was no errors.

I am starting to see it more clearly. tell me if am wrong. if button1 is push motor 1 forward . Else if button2 is pressed motor1 backwards. else no button pressed turn off pin1 and2 is this correct. do i duplicate for button 3 and 4. sometimes c++ is learning curve

kmot2014:
I am starting to see it more clearly. tell me if am wrong. if button1 is push motor 1 forward . Else if button2 is pressed motor1 backwards. else no button pressed turn off pin1 and2 is this correct. do i duplicate for button 3 and 4. sometimes c++ is learning curve

Yes, this is essentially correct. In your original code, you looked at button 1 and acted on it; assume the button was pressed. You'd turn the motor on. But then you'd check button #2 and if it wasn't pressed then the motor was turned off. If things had been reversed and button 2 had been pressed and button 1 not, then when the loop came 'round the top the motor would be shut off.

In the code snippet I gave the button checks and actions are separated; button 2 is not checked as long as button 1 is pressed. If button 1 is not pressed but button 2 is, the motor is turned the other way. Only if neither button is pressed will the motor be turned off. In that example, button 1 is given "priority"; if pressed, it doesn't matter what state button 2 is in, only button 1 will be acted upon. You can change this if you like.

You can modify your code for the left motor (buttons 3 and 4) in the same way.

thank you for explaining I search all over a could not figure it out

Thanks for all the help Rewritten code works except for very last.

/*
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(buttonForwardLeftPin, 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 right motor on "forward":
digitalWrite(motorFowardRightPin, HIGH);
digitalWrite(motorBackwardsRightPin, LOW);
digitalWrite(enableMotor1And2Pin, HIGH);
}
else if( buttonState2 == HIGH )
{
// turn right motor on "backward"
digitalWrite(motorBackwardsRightPin, HIGH);
digitalWrite(motorFowardRightPin, LOW);
digitalWrite(enableMotor1And2Pin, HIGH);
}
else if( buttonState3 == HIGH )
{
// turn right motor on "backward"
digitalWrite(motorFowardLeftPin, HIGH);
digitalWrite(motorBackwardsLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, HIGH);
}
else if( buttonState4 == HIGH )
{
// turn right motor on "backward"
digitalWrite(motorBackwardsLeftPin, HIGH);
digitalWrite(motorFowardLeftPin, LOW);
digitalWrite(enableMotor3And4Pin, HIGH);
}

################################################

else

//turn right motor off
digitalWrite(enableMotor1And2Pin, LOW);

digitalWrite(enableMotor3And4Pin, LOW);
}

###############################################
this section If digitalWrite (enableMotor3And4, LOW); First Left Motor works

If digitalWrite(enableMotor1And@Pin, LOW); First right motor works Some how I need to make both state equal but that does not work ether. So close but yet so far

Please use code tags for posting code. Type [code] then paste your code, then type [/code]. This will make your code more readable, and will protect it from errors induced by the forum software.

Another tip: use const byte to declare the pin numbers instead of const int. It will work the same, but take up only half the memory.

Check your last ‘else’ {}

You seldom want to operate on a level read from an input.
Usually you only want to see ‘when’ that input changes state.
See the change in state example in the IDE.

ChrisTenone:
Another tip: use const byte to declare the pin numbers instead of const int. It will work the same, but take up only half the memory.

I'm not sure that is correct. The compiler will most likely optimize away the actual variable in favor of using the value directly which doesn't result in any savings.

Oh, that clever compiler. You are correct.