Hello, I am a little confused now because I'm not sure what am I doing wrong.
I want to control 2 DC motors with 4 buttons (Motor1 left, Motor1, right, Motor 2 left, Motor2 right). Everything works as should except one 'If' statement, where the motor keeps going instead of stop after a second, like in other cases. I've already switched motors, then buttons, then pins on board, and it is the same. When I comment out the problematic 'IF', the rest 3 works perfectly.
The issue I have is with Moto2down part.
Can someone have a look and help me here, please?
(for motor controls I'm using TB6612FNG)
//Moto 1
int motor1pin1 = 2;
int motor1pin2 = 3;
//Moto 2
int motor2pin1 = 4;
int motor2pin2 = 5;
//buttons
int moto1up = 6;
int moto1down = 7;
int moto2up = 8;
int moto2down = 11;
bool moto1up_state = 0;
bool moto1down_state = 0;
bool moto2up_state = 0;
bool moto2down_state = 0;
void setup() {
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(moto1up, INPUT_PULLUP);
pinMode(moto1down, INPUT_PULLUP);
pinMode(moto2up, INPUT_PULLUP);
pinMode(moto2down, INPUT_PULLUP);
//pwm
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
moto1up_state = digitalRead(moto1up);
moto1down_state = digitalRead(moto1down);
moto2up_state = digitalRead(moto2up);
moto2down_state = digitalRead(moto2down);
if (moto1up_state == LOW){ // Motor 1 up
analogWrite(9, 100); //ENB pin
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
delay(1000);
}
if (moto1down_state == LOW){ // Motor 1 down
analogWrite(9, 100); //ENA pin
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
delay(1000);
}
if (moto2down_state == LOW){ // Motor 2 down
analogWrite(10, 100);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(1000);
}
if (moto2up_state == LOW){ // Motor 2 up
analogWrite(10, 100);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(1000);
}
else {
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor1pin2, LOW);
}
}