My friend Michael and I are making a line following robot as a home project. but when it is running it wont follow the line it will just keep going. i have the sensors on a test board and connected to the arduino using a arduino motor shield with the sensor reads in analogs 0-2 and the leds on digital pin 7. then the motors on digital pins 3,11,12,13.
this is the code we are using:
//Home project 1 : Line following robot
#define QUARTER_SPEED 64 //defining speeds
#define HALF_SPEED 128
#define FULL_SPEED 255
#define BACKWORD -128
int LDRleft, LDRcenter, LDRright; //sensor values
int rotate = 60;
int leftOffset = 0, centerOffset = 0, rightOffset = 0;
int speedleft = 3; //speed for left side of robot
int speedright = 11; //speed for right side of robot
int directionleft = 12; //left motor(s) forword,reverse
int directionright = 13; //right motor(s) forword,reverse
int threshhold = 5; //sensor threshold
// sensor calibration routine
void calibrate()
{
for (int x=0; x<10; x++) // runs loop 10 times
{
digitalWrite(7, HIGH);
delay(100);
LDRleft = analogRead(A0); //reads the 3 sensors
LDRcenter = analogRead(A1);
LDRright = analogRead(A2);
leftOffset = leftOffset + LDRleft;
centerOffset = centerOffset + LDRcenter;
rightOffset = rightOffset + LDRright;
delay(100);
digitalWrite(7, LOW);
delay(100);
}
//obtain average
leftOffset = leftOffset / 10;
centerOffset = centerOffset / 10;
rightOffset = rightOffset / 10;
//calculate offsets for left and right sensors
leftOffset = centerOffset - leftOffset;
rightOffset = centerOffset - rightOffset;
}
void setup()
{
pinMode(7, OUTPUT); //has to be pin 7, pin 9 is a break for motors
pinMode(speedleft, OUTPUT);
pinMode(speedright, OUTPUT);
pinMode(directionleft, OUTPUT);
pinMode(directionright, OUTPUT);
// calibrate the sensors
calibrate();
delay(4000);
digitalWrite(7, HIGH); //lights on
delay(100);
//setting motors to forward
digitalWrite(directionleft, HIGH);
digitalWrite(directionright, LOW);
//setting speeds of motors
analogWrite(speedleft, HALF_SPEED);
analogWrite(speedright, HALF_SPEED);
delay(100);
}
void loop()
{
//make motors go forword
digitalWrite(directionleft, HIGH);
digitalWrite(directionright, LOW);
analogWrite(speedleft, HALF_SPEED);
analogWrite(speedright, HALF_SPEED);
//read the sensors and add the offsets
LDRleft = analogRead(A0) + leftOffset;
LDRcenter = analogRead(A1);
LDRright = analogRead(A2) + rightOffset;
// if LDRleft is greater than the center sensor + threshold turn right
if (LDRleft > (LDRcenter+threshhold)) {
speedleft = HALF_SPEED + rotate;
speedright = HALF_SPEED - rotate;
}
//if LDRright is greater than the center sensor+threshold turn left
if (LDRright > (LDRcenter+threshhold)) {
speedleft = HALF_SPEED - rotate;
speedright = HALF_SPEED + rotate;
}
}
and when i run this the robot doesn't work. can anyone help me with what may be wrong and if you can please show us the errors in it so we can learn off of it. and if you can message me what the code should look like can you please message it to me to try on the robot. so is my problem in my wiring or is there an apparent problem in my code.