I'm currently trying to control 4 motors with 4 button switches using an L298N, Arduino UNO, and 7.2V 1500mA battery. However for some reason only tactPin 2 and 4 are working. Here's the program:
int IN1=8;
int IN2=7;
int ENA=10;
int ENB=11;
int IN3=12;
int IN4=13;
int tactPin1=3;
int tactPin2=5;
int tactPin3=6;
int tactPin4=9;
int ledState1 = HIGH;
int ledState2 = HIGH;
int ledState3 = HIGH;
int ledState4 = HIGH;
void setup()
{
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(tactPin1, INPUT); // Right Side
pinMode(tactPin2, INPUT); // Right Side
pinMode(tactPin3, INPUT); // Left side
pinMode(tactPin4, INPUT); // Left side
}
void loop()
{
if (digitalRead(tactPin1) == HIGH)
{
analogWrite(ENB, 255); // motor speed
digitalWrite(IN3, HIGH); // rotate forward Right side
digitalWrite(IN4, LOW);
}
else if (digitalRead(tactPin1) == LOW)
{
analogWrite(ENB, 0);
digitalWrite(IN3, LOW); // stop Right side
digitalWrite(IN4, LOW);
}
if (digitalRead(tactPin2) == HIGH)
{
analogWrite(ENB, 255);// motor speed
digitalWrite(IN3,LOW);// rotate backward Right side
digitalWrite(IN4,HIGH);
}
else if (digitalRead(tactPin2) == LOW)
{
analogWrite(ENB, 0);
digitalWrite(IN3,LOW); // stop Right side
digitalWrite(IN4,LOW);
}
if (digitalRead(tactPin3) == HIGH)
{
analogWrite(ENA, 255);// motor speed
digitalWrite(IN1,HIGH); // Left Side backward
digitalWrite(IN2,LOW);
}
else if (digitalRead(tactPin3) == LOW)
{
analogWrite(ENA, 0);// motor speed
digitalWrite(IN1,LOW); // Left Side stop
digitalWrite(IN2,LOW);
}
if (digitalRead(tactPin4) == HIGH)
{
analogWrite(ENA, 255);// motor speed
digitalWrite(IN1,LOW); //Moves backward
digitalWrite(IN2,HIGH);
}
else if (digitalRead(tactPin4) == LOW)
{
analogWrite(ENA, 0);// motor speed
digitalWrite(IN1,LOW); // Left Side stop
digitalWrite(IN2,LOW);
}
}