Line following robot - IR sensors do not work in co ordination with motors

I am trying to make a 4 wheel drive line follower robot. I am using Arduino Uno and an L293D motor shield with 4 BO motors and 2 IR sensors. After uploading the sketch, the motors work fine but the car doesn’t follow the black line as it is supposed to. Could anyone please guide me in this regard. My sketch is as follows.

#include<AFMotor.h> //Including Adafruit Motor Driver Shield Library
//creating motor obejects
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

#define LEFT_SENSOR A5// connecting the Left sensor with analog pin A5
#define RIGHT_SENSOR A4 // connecting the Right sensor with analog pin A4

void setup() {

pinMode(RIGHT_SENSOR, INPUT); // initialize Right sensor as an inut
pinMode(LEFT_SENSOR, INPUT); // initialize Left sensor as as input
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
Serial.begin(9600);

}

void loop() {
Serial.println(analogRead(LEFT_SENSOR));
Serial.println(analogRead(RIGHT_SENSOR));
if(analogRead(RIGHT_SENSOR)<=35 && analogRead(LEFT_SENSOR)<=35) //comparing both sensor value to set the directionc
{
motor1.run(FORWARD); // run motor1 clockwise
motor2.run(FORWARD); // run motor2 clockwise
motor3.run(FORWARD); // run motor3 clockwise
motor4.run(FORWARD); // run motor4 clockwise
}
else if(analogRead(RIGHT_SENSOR)<=35 && analogRead(LEFT_SENSOR)<=35) //comparing both sensor value to set the direction
{
motor1.run(FORWARD); // run motor1 clockwise
motor2.run(FORWARD); // run motor2 clockwise
motor3.run(BACKWARD); // run motor3 anti-clockwise
motor4.run(BACKWARD); // run motor4 anti-clockwise
}
else if(!analogRead(RIGHT_SENSOR)<=35 && !analogRead(LEFT_SENSOR)<=35) //comparing both sensor value to set the direction
{
motor1.run(BACKWARD); // run motor1 anti-clockwise
motor2.run(BACKWARD); // run motor2 anti-clockwise
motor3.run(FORWARD); // run motor3 clockwise
motor4.run(FORWARD); // run motor4 clockwise

}
else if(!analogRead(RIGHT_SENSOR)<=35 && !analogRead(LEFT_SENSOR)<=35) //comparing both sensor value to set the direction
{
//stop all the motors
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
}

Use Code Tags </> to present code.

Use local variables to hold analog values, instead of so many analogRead()s.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

A HINT; read the IR sensors at the start of the void loop() and store the value in a variable.
Then use the variables in your if statements.
Basically you take a snapshot of your inputs on each loop and use them through the code.

Have you written code to JUST test the IR sensors?

Thanks.. Tom.. :slight_smile: