As a school project we have been tasked with building a line following robot. We are having trouble with getting our robot to stay on the black tape. It keeps veering to the left and does not return. Im not the best with coding so any help would be greatly appreciated.
// Declaration
// LED + LDR
int LED1 = 6;
int LED2 = 5;
int LED3 = 3;
int LDRA = A0;
int LDRB = A1;
int LDRC = A5;
int x = 0;
int y = 0;
int z = 0;
unsigned int sensorValueA = 0; //value for right LDR
unsigned int sensorValueB = 0; //value for left LDR
unsigned int sensorValueC = 0; //value for left LDR
//Motor
#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2
#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
#define I3 12 // Control pin 1 for motor 2
#define I4 13 // Control pin 2 for motor 2
void setup() {
pinMode (LDRA, INPUT);
pinMode (LDRB, INPUT);
pinMode (LDRC, INPUT);
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
Serial.begin(9600);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
}
void LEDLDR () {
//set all LED on
digitalWrite (LED1, HIGH);
digitalWrite (LED2, HIGH);
digitalWrite (LED3, HIGH);
//getting sensor value
sensorValueA = analogRead(LDRA);
sensorValueB = analogRead(LDRB);
sensorValueC = analogRead(LDRC);
//testing purposes
Serial.print("Analog reading for A is = ");
Serial.print(sensorValueA); // the raw analog reading
Serial.print("Analog reading for B is = ");
Serial.print(sensorValueB);
Serial.print("Analog reading for C is = ");
Serial.print(sensorValueC);
//assigning the values of x, y, z
//assigning x
if (sensorValueA < 10) {
Serial.println(" - Dark");
x = 0;
} else if (sensorValueA < 770) {
Serial.println(" - Dim");
x = 1;
} else if (sensorValueA < 790) {
Serial.println(" - Light");
x = 2;
} else {
Serial.println(" - Bright");
x = 3;}
//assigning y
if (sensorValueB < 10) {
Serial.println(" - Dark");
y = 0;
} else if (sensorValueB < 680) {
Serial.println(" - Dim");
y = 1;
} else if (sensorValueB < 790) {
Serial.println(" - Light");
y = 2;
} else {
Serial.println(" - Bright");
y = 3;}
//assigning z
if (sensorValueC < 10) {
Serial.println(" - Dark");
z = 0;
} else if (sensorValueC < 300) {
Serial.println(" - Dim");
z = 1;
} else if (sensorValueC < 450) {
Serial.println(" - Light");
z = 2;
} else {
z = 3;
}
}
//Makes the car move forward
void forwardCar () {
analogWrite(E1, 250);
analogWrite(E2, 250);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void stopCar () {
analogWrite(E1, 0);
analogWrite(E2, 0);
digitalWrite(I1, LOW);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
}
void moveRight () {
analogWrite(E1, 150); //tbd
analogWrite(E2, 180);
digitalWrite(I1, LOW);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
void moveLeft () {
analogWrite (E1, 180);
analogWrite (E2, 150);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
}
void loop () {
LEDLDR (); //collect all values of LDRS
//nothing
//A - 830 (bright)
//B - 795 (bright)
//C - 453 (bright)
//b construction paper
//A - 793 (bright)
//B - 676 (light) | Dim
//C - 390 (light)
//c contrustrction paper
//A - 835 (bright)
//B - 784 (light)
//C - 285 (light) | Dim
//a construction paper
//A - 765 (light) | Dim
//B - 792 (Bright)
//C - 447 (light)
//Range for A
//Dark: 0-10
//Dim: 10-770
//Light: 770 - 790
//Bright: 790 +
//Range for B
//Dark: 0-10
//Dim: 10-680
//Light: 680-790
//Bright: 790+
//Range for C
//Dark: 0-10
//Dim: 10 -300
//Light: 300 - 450
//Bright: 450+
//For running forward, above should be true
if ((x==3) && (y==3) && (z==3)){ //b b b
forwardCar();
} else if ((x==1) && (y==3) && (z==2)){ //d b l
moveRight ();
} else if ((x==3) && (y==2) && (z==1)){ //b l d
moveLeft();
} else {
stopCar();
}
}