Line Follower Robot not moving forward

Hello guys. I am new to arduino. I made line follower robot that should be following the black line. I wrote some code, but my robot does not move forward. I wonder if I messed up somewhere in the code I would be very thankful for some tips and answers. Cheers. Here is my code:

#define LS 2 // left sensor
#define RS 3 // right sensor
#define LM1 5 // left motor M1a
#define LM2 4 // left motor M2a
#define RM1 7 // right motor M2a
#define RM2 6 // right motor M2b
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
}
void loop()
{
if(digitalRead(LS) && digitalRead(RS)) // Move Forward on line
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(digitalRead(LS) && !(digitalRead(RS))) // turn left by rotationg left motors in forward and right ones in backward direction
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(!(digitalRead(LS)) && digitalRead(RS)) // Turn right by rotating right motors in forward and left ones in backward direction
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, HIGH);
}

if(!(digitalRead(LS)) && !(digitalRead(RS))) // Finish line, stop both the motors
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}

It may be a wiring problem: have you tested your motors before coding the sensors? Try a simple code as:

digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);

and see if you wheels work as expected.

You should store the sensors readings in variables to do the if tests, such as:

bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);

if(leftSensor  && rightSensor) // Move Forward on line
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

But you haven't told us about your sensors and Arduino board: any references? Are you sure these sensors provide digital information?

Yes I did test the motors and the wheels work like they should.
So maybe the problem is I didn't store the sensors readings into the variables? Should definitely try that.

Oh sorry I'm new to the forum so yeah...
I use arduino uno board and I use 2 IR Infrared Obstacle Avoidance Sensors I bought off ebay.

Thank you for your answer!

And have you checked you sensors alone?
Use a simple sketch

void loop() { Serial.println(digitalRead(LS)); }

and move your sensor on and off the line you want to follow to see what the sensor tells you.

Yes the sensors work like they should.

I changed the code and this is how it looks now.. but now the robot is just driving and not stopping when it doesn't get any light..

#define LS 2 // left sensor
#define RS 3 // right sensor
#define LM1 5 // left motor M1a
#define LM2 4 // left motor M2a
#define RM1 7 // right motor M2a
#define RM2 6 // right motor M2b
bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
}
void loop()
{
if(leftSensor && rightSensor) // Move Forward on line
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(leftSensor && !(rightSensor)) // turn left by rotationg left motors in forward and right ones in backward direction
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, HIGH);
}
if(!(leftSensor) && rightSensor) // Turn right by rotating right motors in forward and left ones in backward direction
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if(!(leftSensor) && !(rightSensor)) // Finish line, stop both the motors
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}

bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);

Needs to be in loop ().

Please remember to use code tags when posting code.

I did put them in the loop but I get this error: expected initializer before 'bool'

And sorry about the tags!

Have we seen the code that gives you that error?

Here si the code

#define LS 2 // left sensor
#define RS 3 // right sensor
#define LM1 5 // left motor M1a
#define LM2 4 // left motor M2a
#define RM1 7 // right motor M2a
#define RM2 6 // right motor M2b
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
}
void loop()

bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);

{
if(leftSensor && rightSensor) // Move Forward on line
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
if(leftSensor && !(rightSensor)) // turn left by rotationg left motors in forward and right ones in backward direction
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, HIGH);
}
if(!(leftSensor) && rightSensor) // Turn right by rotating right motors in forward and left ones in backward direction
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if(!(leftSensor) && !(rightSensor)) // Finish line, stop both the motors
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}
void loop()

Something missing here

Something like this?

void loop()
{
  bool leftSensor = digitalRead(LS);
  bool rightSensor = digitalRead(RS);
}

Instead of

void loop()

bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);

{
if(leftSensor && rightSensor) // Move Forward on line

Do this

void loop() { // <-- here !

bool leftSensor = digitalRead(LS);
bool rightSensor = digitalRead(RS);

if(leftSensor && rightSensor) // Move Forward on line

Thank you guys for answers this is the code now

#define LS 2 // left sensor
#define RS 3 // right sensor
#define LM1 5 // left motor M1a
#define LM2 4 // left motor M2a
#define RM1 7 // right motor M2a
#define RM2 6 // right motor M2b

void setup(){
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
}

void loop(){
  bool leftSensor = digitalRead(LS);
  bool rightSensor = digitalRead(RS);

  if(leftSensor && rightSensor){
    // Move forward
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }
  if(leftSensor && !(rightSensor)){
    // turn left by rotating left motor forward and right motor backwards
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, HIGH);
  }
  if(!(leftSensor) && rightSensor){
    // turn right by rotating right motor forward and left motor backwards
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, HIGH);  
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }
  if(!(leftSensor) && !(rightSensor)){
    // stop both motors
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);
  }
}

And the problem is still there the robot does not move forward on the line… when i lift it in the air the wheels start spinning… as soon as I put it down on the line it does not move forward

1 Like