Programming buttons to control motors

Using a Dual H-Bridge as the motor controller. I am trying to control 2 motors with buttons. 1 for forward M1, 2 for forward M2, 3 for backward M1, and 4 for backward M2. However, none of it is working. when I press a button it just makes noise. Any advice would be much appreciated.
Program:

int IN1=8;
int IN2=9;
int ENA=10;
int IN3=12;
int IN4=13;
int ENB=11;
int button1= 7;
int button2= 6;
int button3= 5;
int button4= 4;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop()
{
if (digitalRead(button1) == LOW)//If button isn't pressed then the Red motor won't go
{
analogWrite(ENA, 0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
if (digitalRead(button2) == LOW)//If button isn't pressed then the White motor won't go
{
analogWrite(ENB, 0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
if (digitalRead(button3) == LOW)//If button isn't pressed then the Red motor won't go
{
analogWrite(ENA, 0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
if (digitalRead(button4) == LOW)//If button isn't pressed then the White motor won't go
{
analogWrite(ENB, 0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}

if (digitalRead(button1) == HIGH)
{
analogWrite(ENA, 50);// motor speed
digitalWrite(IN1,HIGH);// rotate forward
digitalWrite(IN2,LOW);
}

if (digitalRead(button2) == HIGH)
{
analogWrite(ENB, 50);// motor speed
digitalWrite(IN3,LOW);// rotate reverse
digitalWrite(IN4,HIGH);
}
else if (digitalRead(button3) == HIGH)
{
analogWrite(ENA, 50);// motor speed
digitalWrite(IN1,LOW);// rotate forward
digitalWrite(IN2,HIGH);
}
else if (digitalRead(button4) == HIGH)
{
analogWrite(ENB, 50);// motor speed
digitalWrite(IN1,HIGH);// rotate Reverse
digitalWrite(IN2,LOW);
}
}

Try writing them full speed instead of only a PWM of 50/255. Some DC motors don't do well at certain PWM frequencies. If it will run at full speed, then you can play with getting it to run with PWM.

Why are you doing analogWrite(...) to ENA and ENB ?

I thought you needed to do analogWrite(ENA, ...) when programming H-bridges?

Blazingnite:
I thought you needed to do analogWrite(ENA, ...) when programming H-bridges?

You do. Or you can digitalWrite them if you want to run full speed. That's what I recommend you do as a test.

vaj4088:
Why are you doing analogWrite(...) to ENA and ENB ?

That's how you drive a motor PWM with an H-bridge.

Alright so did this. Button 1 and 4 work. 2 and 3 are still giving me issues
int IN1=8;
int IN2=9;
int ENA=10;
int IN3=12;
int IN4=13;
int ENB=11;
int button1= 7;
int button2= 6;
int button3= 5;
int button4= 4;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop()
{
if (digitalRead(button1) == LOW)//If button isn't pressed then the Red motor won't go
{
digitalWrite(ENA, 0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
if (digitalRead(button2) == LOW)//If button isn't pressed then the White motor won't go
{
analogWrite(ENB, 0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
if (digitalRead(button3) == LOW)//If button isn't pressed then the Red motor won't go
{
analogWrite(ENA, 0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
if (digitalRead(button4) == LOW)//If button isn't pressed then the White motor won't go
{
analogWrite(ENB, 0);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}

if (digitalRead(button1) == HIGH)
{
digitalWrite(ENA, 255);// motor speed
digitalWrite(IN1,HIGH);// rotate forward
digitalWrite(IN2,LOW);
}

if (digitalRead(button2) == HIGH)
{
digitalWrite(ENB, 255);// motor speed
digitalWrite(IN3,LOW);// rotate reverse
digitalWrite(IN4,HIGH);
}
if (digitalRead(button3) == HIGH)
{
digitalWrite(ENA, 255);// motor speed
digitalWrite(IN1,LOW);// rotate forward
digitalWrite(IN2,HIGH);
}
if (digitalRead(button4) == HIGH)
{
digitalWrite(ENB, 255);// motor speed
digitalWrite(IN1,HIGH);// rotate Reverse
digitalWrite(IN2,LOW);
}
}

Since you are testing for HIGH being a pressed button I have to ask, do you have pull-down resistors on the buttons? It's MUCH easier to simply connect the button between the pin and ground and use INPUT_PULLUP as the pinMode. In that case a pressed button reads LOW and unpressed reads HIGH.

Can you describe the problem a little better than "still giving me issues". When trying to find the problem in a piece of code it really helps to know what the symptoms of the problem are.

Also, please read the "How to Use This Forum" post at the top of the board. Pay particular attention to point 7 where it describes how to use code tags to post code to make it more readable. That will go a surprisingly long way in helping us to help you.

You should probably also set ENA and ENB as OUTPUT.

digitalWrite(ENA, 255);// motor speed

digitalWrite takes HIGH or LOW as the second parameter. The 255 is being interpreted as HIGH, but that is a gross abuse of the API. You should use HIGH or LOW for digitalWrite.

What are IN1 through IN4? Most motor controllers only have 1 direction pin and 1 enable pin per motor.

Can you draw us a quick diagram of your wiring?

Sorry for the vagueness in describing the problem that I'm facing and I am using a pull-down resistor.

I just took a picture of instead since it's quicker. And the buttons are 1-4 starting from left to right. The pull-down resistor for button 1 looks like it's in the wrong hole. But it isn't. I also thought that OUTPUT 1 on the Dual H-Bridge is for IN1, OUTPUT 2 for IN2, and so on.

Nvm. Image files are too big. Ill just Fritz it.

Blazingnite:
Nvm. Image files are too big. Ill just Fritz it.

Please don't just draw it or reduce the size of the image file so it will fit. Those fritzing things are so hard to look at.

Blazingnite:
I just took a picture of instead since it's quicker. And the buttons are 1-4 starting from left to right. The pull-down resistor for button 1 looks like it's in the wrong hole. But it isn't. I also thought that OUTPUT 1 on the Dual H-Bridge is for IN1, OUTPUT 2 for IN2, and so on.

The outputs on the H-bridge should go to the motor.

This is just a simple logic problem in the sketch. Nothing to do with H-bridge wiring, etc. Relax.

Looking at your sketch, when you press button3 (for instance), you want this:

digitalWrite(ENA, 255);// motor speed 
digitalWrite(IN1,LOW);// rotate forward
digitalWrite(IN2,HIGH);

Cool banannas. The problem is that if button1 isn't pressed, your loop will also do this:

digitalWrite(ENA, 0);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);

So when 3 is down and 1 isnt down, your sketch is, like: "Don't go! Go! Dont go! Go!"

To fix:

if(one is up and 3 is up) {
  turn off
}
else if(one is down) {
  go forwards
}
else {
  go backwards
}

And same again for the other pair of buttons.

I know that at this point my wiring is fine. But I keep making errors in the program

void loop()
{
 //First Motor: Button1 is Forward, Button2 is backward
 if(digitalRead(button1) == button2) 
 {
  digitalWrite(ENA, LOW); //turn off
  digitalWrite(IN1,LOW);     
  digitalWrite(IN2,LOW);
}
else if(digitalRead(button1) == HIGH)//is down
{
 digitalWrite(ENA, HIGH); //go forwards
 digitalWrite(IN1,HIGH); 
 digitalWrite(IN2,LOW); 
}  
else(digitalRead(button2) == HIGH)  //is down
 (
   digitalWrite(ENA,HIGH)  //go backwards
   digitalWrite(IN1,LOW); //I get an expected ')' before ' digitalWrite' here
   digitalWrite(IN2,HIGH);
 ) 
   
//Second Motor Button4 is Forward, Button3 is backward
 if(digitalRead(button3) == button4)
 {
   digitalWrite(ENB, LOW) //turn off
  digitalWrite(IN3,LOW);     
  digitalWrite(IN4,LOW);
}
else if(digitalRead(button4) == HIGH)//is down
{
 digitalWrite(ENB, HIGH) //go forwards
 digitalWrite(IN3,HIGH); 
 digitalWrite(IN4,LOW); 
}
else (digitalRead(button3) == HIGH)//is down
{
 digitalWrite(ENB, HIGH) //go backwards
 digitalWrite(IN3,LOW); 
 digitalWrite(IN4,HIGH); 
}

An else shouldn't have a condition.

 //First Motor: Button1 is Forward, Button2 is backward
 if(digitalRead(button1) == button2) 

...

//Second Motor Button4 is Forward, Button3 is backward
 if(digitalRead(button3) == button4)

These two conditions will never be true.

Thanks guys. I figured out it actually on the 2nd but have been without wifi for a while. For anybody who attempts to do the same, here's the program:

  //Red Motor: Button1 is Forward, Button2 is backward
  if(digitalRead(button1) == true) 
  {
  digitalWrite(ENA, HIGH); //go forwards
  digitalWrite(IN1,LOW); 
  digitalWrite(IN2,HIGH); 
  }
else if(digitalRead(button2) == true) //is down
{
  digitalWrite(ENA, HIGH); //go backwards
  digitalWrite(IN1,HIGH); 
  digitalWrite(IN2,LOW); 
} 
 else
 {
 digitalWrite(ENA, LOW); //Stop
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,LOW); 
 }
 //White Motor: Button4 is Forward, Button3 is backward
  if(digitalRead(button3) == true) 
  {
  digitalWrite(ENB, HIGH); //go backwards
  digitalWrite(IN3,LOW); 
  digitalWrite(IN4,HIGH); 
  }
else if(digitalRead(button4) == true) //is down
{
  digitalWrite(ENB, HIGH); //go Forwards
  digitalWrite(IN3,HIGH); 
  digitalWrite(IN4,LOW); 
} 
 else
 {
  digitalWrite(ENB,LOW); //Stop
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,LOW); 
 }
}