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