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?