A line follower robot which can follow a dark line on a bright surface.
https://www.youtube.com/watch?v=MXt_QuZyAjU
//My line follower car
int SensorLED = 3; //Define the LED of the Line Sensor
int button = 2;
int LeftMotorPlus = 8;
int LeftMotorMinus = 9;
int LeftMotorEnable = 10;
int RightMotorPlus = 12;
int RightMotorMinus = 13;
int RightMotorEnable = 11;
int sensor1 = 0;
int sensor2 = 0;
int sensor3 = 0;
int sensor4 = 0;
int sensor5 = 0;
int sensor6 = 0;
int sensor7 = 0;
void setup()
{
Serial.begin(9600);
pinMode(LeftMotorPlus, OUTPUT);
pinMode(LeftMotorMinus, OUTPUT);
pinMode(LeftMotorEnable, OUTPUT);
pinMode(RightMotorEnable, OUTPUT);
pinMode(RightMotorMinus , OUTPUT);
pinMode(RightMotorPlus , OUTPUT);
pinMode(SensorLED, OUTPUT);
}
void move_forward() {
digitalWrite(LeftMotorEnable, HIGH);
digitalWrite(RightMotorEnable, HIGH);
digitalWrite(LeftMotorPlus, HIGH);
digitalWrite(LeftMotorMinus, LOW);
digitalWrite(RightMotorPlus, LOW);
digitalWrite(RightMotorMinus, HIGH);
}
void move_backward() {
digitalWrite(LeftMotorEnable, HIGH);
digitalWrite(RightMotorEnable, HIGH);
digitalWrite(LeftMotorPlus, LOW);
digitalWrite(LeftMotorMinus, HIGH);
digitalWrite(RightMotorPlus, HIGH);
digitalWrite(RightMotorMinus, LOW);
}
void turn_left()
{
analogWrite(LeftMotorEnable, 210);
analogWrite(RightMotorEnable, 210);
digitalWrite(LeftMotorPlus, HIGH);
digitalWrite(LeftMotorMinus, LOW);
digitalWrite(RightMotorPlus, HIGH);
digitalWrite(RightMotorMinus, LOW);
}
void turn_right() {
analogWrite(LeftMotorEnable, 210);
analogWrite(RightMotorEnable, 210);
digitalWrite(LeftMotorPlus, LOW);
digitalWrite(LeftMotorMinus, HIGH);
digitalWrite(RightMotorPlus, LOW);
digitalWrite(RightMotorMinus, HIGH);
}
void stop_moving() {
digitalWrite(LeftMotorEnable, LOW);
digitalWrite(RightMotorEnable, LOW);
digitalWrite(LeftMotorPlus, LOW);
digitalWrite(LeftMotorMinus, LOW);
digitalWrite(RightMotorPlus, LOW);
digitalWrite(RightMotorMinus, LOW);
}
int find_line_position() {
digitalWrite(SensorLED, HIGH);//Turn on Sensor LED
delay(1);
//Read the sensors and display Serially
int sensorValue1 = analogRead(A1);
delay(1);
sensorValue1 = analogRead(A1);
if (sensorValue1 < 600 && sensorValue1 > 320) {
sensor1 = 1;
}
else {
sensor1 = 0;
}
Serial.println(sensorValue1);
delay(1);
int sensorValue2 = analogRead(A2);
delay(1);
sensorValue2 = analogRead(A2);
if (sensorValue2 < 600 && sensorValue2 > 320) {
sensor2 = 1;
}
else {
sensor2 = 0;
}
Serial.println(sensorValue2);
delay(1);
int sensorValue3 = analogRead(A3);
delay(1);
sensorValue3 = analogRead(A3);
if (sensorValue3 < 600 && sensorValue3 > 320) {
sensor3 = 1;
}
else {
sensor3 = 0;
}
Serial.println(sensorValue3);
delay(1);
int sensorValue4 = analogRead(A4);
delay(1);
sensorValue4 = analogRead(A4);
if (sensorValue4 < 600 && sensorValue4 > 320) {
sensor4 = 1;
}
else {
sensor4 = 0;
}
Serial.println(sensorValue4);
delay(1);
int sensorValue5 = analogRead(A5);
delay(1);
sensorValue5 = analogRead(A5);
if (sensorValue5 < 600 && sensorValue5 > 320) {
sensor5 = 1;
}
else {
sensor5 = 0;
}
Serial.println(sensorValue5);
delay(1);
int sensorValue6 = analogRead(A6);
delay(1);
sensorValue6 = analogRead(A6);
if (sensorValue6 < 600 && sensorValue6 > 320) {
sensor6 = 1;
}
else {
sensor6 = 0;
}
Serial.println(sensorValue6);
delay(1);
int sensorValue7 = analogRead(A7);
delay(1);
sensorValue7 = analogRead(A7);
if (sensorValue7 < 600 && sensorValue7 > 320) {
sensor7 = 1;
}
else {
sensor7 = 0;
}
Serial.println(sensorValue7);
Serial.println(" ");
//delay(3200);
//Calculate the Line Position
Serial.print(sensor1);
Serial.print(sensor2);
Serial.print(sensor3);
Serial.print(sensor4);
Serial.print(sensor5);
Serial.print(sensor6);
Serial.print(sensor7);
Serial.println("");
// Calculate the Decimal corresponding to the line position
int dec = 1 * sensor7 + 2 * sensor6 + 4 * sensor5 + 8 * sensor4 + 16 * sensor3 + 32 * sensor2 + 64 * sensor1;
Serial.print ("Detected line position is");
Serial.println(dec);
return dec;
}
int count = 0;
void loop()
{
int line_position = find_line_position();
if (line_position > 0 && line_position < 7) //i.e. the line is on the right
{
turn_right();
Serial.println("Line found on the right");
}
if (line_position > 7 && line_position < 31)//i.e line is on the center
{
move_forward();
Serial.println("Line is in The Middle");
}
if (line_position > 31 && line_position < 127) //i.e. line is on the left
{
turn_left();
Serial.println("Line is on the Left Side");
}
if (line_position == 127 || line_position == 0) {
count = count + 1;
Serial.print(" Current No line Timeout is");
Serial.println(count);
if (count > 250)
{
Serial.println("Stopping");
stop_moving();
}
}
}