Line Following Robot.



๐Ÿ”˜Components Required



1)Arduino Mega Board
Arduino Mega/Uno Board

2)Wheels


3)Geared Motors







4)Bread Board



5)Motor Driver


5) 5 IR sensors




6)Jumper Wires


7)Wheel






We use IR sensor for detecting robot path





Line sensors


IR sensor active =1(when white color)
IR sensor deactivate=0(when black color)


consider five IR sensors




01111      = line off to left
10111      = line little to left
11011      = middle of the line
11101      = line little to right
11110      = line off to right


๐Ÿ”˜intersection and turn handling


1) turning left and turning right










Get IR sensors read
if (read=11000)
     turn right
if (read=00011)
     turn left


2)
T junction or Four way



How to decide where it is "T" or "FOUR way" ๐Ÿ‘€???
➧when  all sensors are  become "11111"
➧then we can give one inch move to robot
➧after we can read IR sensors 

 if  ("00000")
    its "T" junction;
 else
     its "FOUR way";   

3)



How to recognize these Junctions๐Ÿ‘€??

➧when  all sensors are  become "11100"
➧then we can give one inch move to robot
➧after we can read IR sensors 

If (read="00100")
    Go Straight
else if (read ="00000")
    Turn left

๐Ÿ”˜Arduino Code 
****************************************
#define leftCenterSensor 52
#define leftNearSensor 51
#define leftFarSensor 53
#define rightCenterSensor 50

#define rightFarSensor 49
int leftCenterReading;
int leftNearReading;
int leftFarReading;
int rightCenterReading;
int rightNearReading;
int rightFarReading;
int leftNudge;
int replaystage;
int rightNudge;
#define leapTime 200
#include<AFMotor.h>
AF_DCMotor MR(1);
AF_DCMotor ML(2);

char path[30] = {};
int pathLength;
int readLength;
void setup(){
pinMode(leftCenterSensor, INPUT);
pinMode(leftNearSensor, INPUT);
pinMode(leftFarSensor, INPUT);
pinMode(rightCenterSensor, INPUT);
pinMode(rightFarSensor, INPUT);
ML.setSpeed(255);
MR.setSpeed(255);

}
void loop(){
readSensors();
if(leftFarReading==1 && rightFarReading==1 &&
(leftCenterReading==0 || rightCenterReading==0) ){
straight();
}
else{
leftHandWall();
}
}
void readSensors(){
leftCenterReading = digitalRead(leftCenterSensor);
leftNearReading = digitalRead(leftNearSensor);
leftFarReading = digitalRead(leftFarSensor);
rightCenterReading = digitalRead(rightCenterSensor);
rightFarReading = digitalRead(rightFarSensor);
}
void leftHandWall(){
if( leftFarReading==0 && rightFarReading==0){
ML.run(FORWARD);
MR.run(FORWARD);
delay(leapTime);
readSensors();
if(leftFarReading==0 || rightFarReading==0){
done();
}
if(leftFarReading==1 && rightFarReading==1){
turnLeft();
}
}
if(leftFarReading==0){
ML.run(FORWARD);
MR.run(FORWARD);
delay(leapTime);
readSensors();
if(leftFarReading==1 && rightFarReading==1){
turnLeft();
}
else{
done();
}
}
if(rightFarReading==0){
ML.run(FORWARD);
MR.run(FORWARD);
delay(30);
readSensors();
if(leftFarReading==0){
delay(leapTime-30);
readSensors();
if(rightFarReading==0 && leftFarReading==0){
done();
}
else{
turnLeft();
return;
}
}
delay(leapTime-30);
readSensors();
if(leftFarReading==1 && leftCenterReading==1 &&
rightCenterReading==1 && rightFarReading==1){
turnRight();
return;
}
path[pathLength]='S';
pathLength++;
if(path[pathLength-2]=='B'){
shortPath();
}
straight();
}
readSensors();
if(leftFarReading==1 && leftCenterReading==1 && rightCenterReading==1
&& rightFarReading==1 && leftNearReading==1 ){
turnAround();
}
}
void done(){
ML.run(RELEASE);
MR.run(RELEASE);
replaystage=1;
path[pathLength]='D';
pathLength++;
delay(500);
replay();
}
void turnLeft(){
while(digitalRead(rightCenterSensor)==0||digitalRead(leftCenterSensor)==0){
MR.run(FORWARD);
ML.run(BACKWARD);
delay(20);
ML.run(RELEASE);
MR.run(RELEASE);
delay(10);
}
while(digitalRead(rightCenterSensor)==1){
MR.run(FORWARD);
ML.run(BACKWARD);
delay(20);
ML.run(RELEASE);
MR.run(RELEASE);
delay(10);
}
if(replaystage==0){
path[pathLength]='L';
pathLength++;
if(path[pathLength-2]=='B'){
shortPath();
}
}
}
void turnRight(){
while(digitalRead(rightCenterSensor)==0){
ML.run(FORWARD);
MR.run(BACKWARD);
delay(20);
ML.run(RELEASE);
MR.run(RELEASE);
delay(10);
}
while(digitalRead(rightCenterSensor)==1){
ML.run(FORWARD);
MR.run(BACKWARD);
delay(20);
ML.run(RELEASE);
MR.run(RELEASE);
delay(10);
}
while(digitalRead(leftCenterSensor)==1){
ML.run(FORWARD);
MR.run(BACKWARD);
delay(20);
ML.run(RELEASE);
MR.run(RELEASE);
delay(10);
}
if(replaystage==0){
path[pathLength]='R';
Serial.println("r");
pathLength++;
Serial.print("Path length: ");
Serial.println(pathLength);
if(path[pathLength-2]=='B'){
Serial.println("shortening path");
shortPath();
}
}
}
void straight(){
if( digitalRead(leftCenterSensor)==1){
ML.run(FORWARD);
MR.run(FORWARD);
delay(1);
ML.run(FORWARD);
MR.run(RELEASE);
delay(5);

return;
}
if(digitalRead(rightCenterSensor)==1){
ML.run(FORWARD);
MR.run(FORWARD);
delay(1);
 ML.run(RELEASE);
 MR.run(FORWARD);
 delay(5);

return;
}
ML.run(FORWARD);
MR.run(FORWARD);
delay(2);
ML.run(RELEASE);
MR.run(RELEASE);
delay(1);
}
void turnAround(){
ML.run(FORWARD);
MR.run(FORWARD);
delay(150);
while(digitalRead(leftCenterSensor)==1){
ML.run(BACKWARD);
MR.run(FORWARD);
delay(2);
ML.run(RELEASE);
MR.run(RELEASE);
delay(1);
}
path[pathLength]='B';
pathLength++;
straight();
}
void shortPath(){
int shortDone=0;
if(path[pathLength-3]=='L' && path[pathLength-1]=='R'){
pathLength-=3;
path[pathLength]='B';
//Serial.println("test1");
shortDone=1;
}
if(path[pathLength-3]=='L' && path[pathLength-1]=='S' && shortDone==0){
pathLength-=3;
path[pathLength]='R';
//Serial.println("test2");
shortDone=1;
}
if(path[pathLength-3]=='R' && path[pathLength-1]=='L' && shortDone==0){
pathLength-=3;
path[pathLength]='B';
//Serial.println("test3");
shortDone=1;
}
if(path[pathLength-3]=='S' && path[pathLength-1]=='L' && shortDone==0){
pathLength-=3;
path[pathLength]='R';
//Serial.println("test4");
shortDone=1;
}
if(path[pathLength-3]=='S' && path[pathLength-1]=='S' && shortDone==0){
pathLength-=3;
path[pathLength]='B';
//Serial.println("test5");
shortDone=1;
}
if(path[pathLength-3]=='L' && path[pathLength-1]=='L' && shortDone==0){
pathLength-=3;
path[pathLength]='S';
//Serial.println("test6");
shortDone=1;
}
path[pathLength+1]='D';
path[pathLength+2]='D';
pathLength++;
//Serial.print("Path length: ");
//Serial.println(pathLength);
//printPath();
}
void printPath(){
Serial.println("+++++++++++++++++");
int x;
while(x<=pathLength){
Serial.println(path[x]);
x++;
}
Serial.println("+++++++++++++++++");
}
void replay(){
readSensors();
if(leftFarReading==1 && rightFarReading==1){
straight();
}
else{
if(path[readLength]=='D'){
ML.run(RELEASE);
MR.run(FORWARD);
delay(100);
ML.run(RELEASE);
MR.run(RELEASE);

}
if(path[readLength]=='L'){
ML.run(FORWARD);
MR.run(FORWARD);
delay(leapTime);
turnLeft();
}
if(path[readLength]=='R'){
ML.run(FORWARD);
MR.run(FORWARD);
delay(leapTime);
turnRight();
}
if(path[readLength]=='S'){
ML.run(FORWARD);
MR.run(FORWARD);
delay(leapTime);
straight();
}
readLength++;
}
replay();
}

****************************************


๐Ÿ”˜FINALLY

Thank You...


Comments

visitors

Popular posts from this blog

Boyer Moore String Search Algorithm

String Matching with Finite Automata

Types Of Computer Networking