Hi people
I've recently started working with arduino. I think I got the hang of it but I ran into an issue I cannot get my head around.
I'm trying to do a line following car. I'm pretty sure the code is correct. However, I ran into a problem:
I've connected two motors to the L298N Controller, and coded each.But when I ask to run both, they don't. Only one runs at the time. In other words, only Motor 1 runs, but if I disable it, Motor 2 runs. They cannot run simultaneously. Am I doing something wrong? I'm using this code modified:
/------ Arduino Line Follower Code----- /
/-------definning Inputs------/
#define LS 2 // left sensor
#define RS 3 // right sensor
/-------definning Outputs------/
#define LM1 4 // left motor
#define LM2 5 // left motor
#define RM1 6 // right motor
#define RM2 7 // right motor
int enA = A0;
int enB = A1;
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
}
void loop()
{
analogWrite(enA,200);
analogWrite(enb,200);
if(digitalRead(LS) && digitalRead(RS)) // Move Forward
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(!(digitalRead(LS)) && digitalRead(RS)) // Turn right
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
delay(3000);
}
if(digitalRead(LS) && !(digitalRead(RS))) // turn left
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
delay(3000);
}
if(!(digitalRead(LS)) && !(digitalRead(RS))) // stop
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
delay (3000);
}
}