Line Following Robot

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();
  }
}

Please post your sketch between code tags. They're the "</>" icon up top. Then your code will look like this:

This is my code

And we won't have to download your viruses.

Hi,

http://forum.arduino.cc/index.php?topic=446820.new#new

Same robot???????

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your complete circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
NOT a FRITZY picture that you have referenced to.

How are you powering your UNO and powering the motors?

Tom... :slight_smile:

Creating Robots using Arduino – H-bridgeHardware Fun | Hardware Fun. This is where we found the circuit that we are using. We are powering the Arduino Uno and the motors with four AA batteries. All the values that our LDRS are picking up while the car is moving are commented out in the code above.

If you change:

  Serial.begin(9600);

to

  Serial.begin (115200);

and shortened all those verbose debugging messages your control loop will be able to run a lot more frequently
and be more responsive.

helplessstudent:
Creating Robots using Arduino – H-bridgeHardware Fun | Hardware Fun. This is where we found the circuit that we are using. We are powering the Arduino Uno and the motors with four AA batteries. All the values that our LDRS are picking up while the car is moving are commented out in the code above.

That is not your circuit, where are the LDRs?
A picture, a circuit diagram and a sketch is worth a thousand words and can save a thousand ill informed posts.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?
Include how you are powering this project.

Not a Fritzy diagram that you linked us too.

Tom..... :slight_smile: