For a project I am doing, I have created a simple code using two push buttons and a potentiometer to control a DC motor, I wired the DC motor to an H-bridge and I know FOR SURE that my wiring is not the problem because I am using the same exact wiring diagram from project 10 of the Arduino uno start kit. My code should be very easy to use. Once uploaded, I should be able to hit "upPin" to turn the motor on in one direction for 3.5 seconds and when I hit the "downPin" it should go in the other direction for another 3.5 seconds. However, when I plug in the uno and upload the sketch, the loop IMMEDIATELY starts to run and doesn't wait for my pins, even though I set them up initially to LOW in the setup(). I have showed my professor, and he stated that my brackets were not in the correct spots for my IF statements. But when I move my IF brackets around any further, the motor doesn't run at all. He then suggested it has to do with the pins of the H-bridge but the circuit works perfectly fine for project 10 under FILES->EXAMPLES->STARTER KIT-> PROJECT TEN ZOETROPE so the way I am manipulating the pins in my own sketch shouldn't be what is causing my push buttons to not work. I just need help with understanding why my buttons are not being acknowledged and what I could do about it. The only reason I put the five second delays is because the motor will just keep running and keep switching directions if I didn't add some kind of pause in between the two.
Here is my code:
const int pin1 = 2; // to pin 7 on IC which is motor logic pin for terminal 2
const int pin2 = 3; // to pin 2 on IC, where current flows through the load
const int ePin = 9; // to pin 1 on IC, enables pin on H-bridge to control motor
const int downPin = 4; // switch to change direction downward
const int upPin = 5; // switch to start motor upward
const int potPin = A0; // potentiometer
int switchStateUp = 0; // state to move upward
int switchStateDown = 0; // state to go down or reverse direction
int motorEnabled = 0; // Turns the motor on/off
int motorSpeed = 0; // speed of the motor
void setup() {
// put your setup code here, to run once:
pinMode(downPin, INPUT);
pinMode(upPin, INPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(ePin, OUTPUT);
digitalWrite(ePin, LOW);
digitalWrite(upPin, LOW);
digitalWrite(downPin, LOW);
}
void loop() {
switchStateUp = digitalRead(upPin);
motorSpeed = analogRead(potPin) / 4 ;
analogWrite(ePin, motorSpeed);
if (upPin == HIGH);
{
digitalWrite(ePin, HIGH);
}
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay(3500);
if (upPin == LOW);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(ePin, LOW);
motorEnabled = digitalRead(ePin) ;
delay(5000);
switchStateDown = digitalRead(downPin);
if (downPin == HIGH) ;
{
digitalWrite(ePin, HIGH);
}
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delay(3500);
if (downPin == LOW);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(ePin, LOW);
motorEnabled = digitalRead(ePin) ;
delay(5000);
}