Line Follower won't turn right

I’m using an Arduino Redbot, with three line sensors. The car will follow the track, but won’t turn right. When it loses the track, it will just turn left until it picks something up, follows it, loses it, and repeat.

This is the script I have:

#include <RedBot.h>

RedBotSensor IRSensor1 = RedBotSensor(A3); // right sensor

RedBotSensor IRSensor2 = RedBotSensor(A6); // center sensor

RedBotSensor IRSensor3 = RedBotSensor(A7); // left sensor

RedBotMotors motors;

int leftSpeed; // variable used to store the leftMotor speed

int rightSpeed; // variable used to store the rightMotor speed

int SPEED = 60;

int LINE = 925;// level to detect if the sensor is on the line or not

void setup() {

Serial.begin(2000);

}

// if on the line drive left and right at the same speed (left is CCW/ right is CW)

void loop() {

if(IRSensor2.read() > LINE)

{
leftSpeed = -SPEED;

rightSpeed = SPEED;
}
// if the line is under the right sensor, adjust relative speeds to turn to the right

if(IRSensor3.read() > LINE)

{

leftSpeed = -(SPEED + 10);

rightSpeed = SPEED - 10;

}

if(IRSensor1.read() > LINE)

{

leftSpeed = -(SPEED - 10);

rightSpeed = SPEED + 10;

}

// if the line is under the left sensor, adjust relative speeds to turn to the left

if((IRSensor1.read() > 950) && (IRSensor2.read() > 950) && (IRSensor3.read() > 950)){

motors.brake();

motors.rightMotor(80);

motors.leftMotor(30);

delay(1000);

}

// if all sensors are on black or up in the air, stop the motors.

if((IRSensor1.read() < 950) && (IRSensor2.read() < 950) && (IRSensor3.read() < 950)){

motors.brake();

do{

motors.rightMotor(80);

motors.leftMotor(-30);

}
while((IRSensor2.read() < 950));

}

motors.leftMotor(leftSpeed);

motors.rightMotor(rightSpeed);

delay(10); // add a delay to decrease sensitivity.

}

Any idea what I could to do fix this?

RedBotSensor IRSensor1 = RedBotSensor(A3); // right sensor

RedBotSensor IRSensor2 = RedBotSensor(A6); // center sensor

RedBotSensor IRSensor3 = RedBotSensor(A7);  //  left sensor

The names are stupid. If they were RightSensor, CenterSensor, and LeftSensor, the comments would be unnecessary.

int LINE = 925;// level to detect if the sensor is on the line or not

Threshold values should be named appropriately. That value will not change while the program is running, so it should be const.

Comments should precede the block of code that they go with, not follow it.

I really can't follow your program logic, because the comments are so weirdly placed.

For everyone’s sake here is the code in code tags and Auto Formatted to make it easier to read

#include <RedBot.h>

RedBotSensor IRSensor1 = RedBotSensor(A3); // right sensor
RedBotSensor IRSensor2 = RedBotSensor(A6); // center sensor
RedBotSensor IRSensor3 = RedBotSensor(A7);  //  left sensor
RedBotMotors motors;

int leftSpeed;  // variable used to store the leftMotor speed
int rightSpeed; // variable used to store the rightMotor speed
int SPEED = 60;
int LINE = 925;// level to detect if the sensor is on the line or not

void setup()
{
  Serial.begin(2000);
}

// if on the line drive left and right at the same speed (left is CCW/ right is CW)

void loop()
{
  if (IRSensor2.read() > LINE)
  {
    leftSpeed = -SPEED;
    rightSpeed = SPEED;
  }
  // if the line is under the right sensor, adjust relative speeds to turn to the right
  if (IRSensor3.read() > LINE)
  {
    leftSpeed = -(SPEED + 10);
    rightSpeed = SPEED - 10;
  }
  if (IRSensor1.read() > LINE)
  {
    leftSpeed = -(SPEED - 10);
    rightSpeed = SPEED + 10;
  }
  // if the line is under the left sensor, adjust relative speeds to turn to the left
  if ((IRSensor1.read() > 950) && (IRSensor2.read() > 950) && (IRSensor3.read() > 950))
  {
    motors.brake();
    motors.rightMotor(80);
    motors.leftMotor(30);
    delay(1000);
  }
  // if all sensors are on black or up in the air, stop the motors.
  if ((IRSensor1.read() < 950) && (IRSensor2.read() < 950) && (IRSensor3.read() < 950))
  {
    motors.brake();
    do
    {
      motors.rightMotor(80);
      motors.leftMotor(-30);
    }
    while ((IRSensor2.read() < 950));
  }
  motors.leftMotor(leftSpeed);
  motors.rightMotor(rightSpeed);
  delay(10); // add a delay to decrease sensitivity.
}

OP - please take note