Monday 6 January 2020

Two sensor code for line follower using feedback


This post is about two sensor version of line follower bot with feedback idea. Go below for main post link.
With two sensors you will find that bot has oscillating behaviour around the line. Which is not good as sometimes bot will never come back to the line. The idea is worth trying. Just modify your three sensor code and see the results.

//code is

int rightmotor1=12;
int leftmotor1=10;
int rightmotor2=13;
int leftmotor2=11;


int sensorleft=14;
int sensorright=16;


int l1=1;
int l2=1;

int f1=0;
int f2=0;

//////////////////////////////////////

void setup()
{
 pinMode(rightmotor1,OUTPUT);
 pinMode(leftmotor1,OUTPUT);
 pinMode(rightmotor2,OUTPUT);
 pinMode(leftmotor2,OUTPUT);

 pinMode(sensorleft,INPUT);
 pinMode(sensorright,INPUT);

}
///////////////////////////////////////////////////

void loop()
{
l1=digitalRead(sensorleft);
l2=digitalRead(sensorright);

/////////////////////////////////////////////////////////////////////
if(l1==1&&l2==1)   //feedback when comes all sensor on white
{
l1=f1;
l2=f2;
}

////////////////////////////////////////////////////////////////////////////
if(l1==0&&l2==1)   //left turn
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,0);
  digitalWrite(leftmotor2,0);
}
else
if(l1==1&&l2==0)  //right turn
{
  digitalWrite(rightmotor1,0);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
else
if(l1==0&&l2==0)  //go forward straight
{
  digitalWrite(rightmotor1,1);
  digitalWrite(rightmotor2,0);
  digitalWrite(leftmotor1,1);
  digitalWrite(leftmotor2,0);
}
///////////////////////////////////////////////////////////////////
f1=l1;
f2=l2;        //memory variables
////////////////////////////////////////////////////////////////////
}




Here f1 and f2 are our feedback variable or we can say memory variables.

This idea best works with three sensors. Checkout three sensor post here https://crackeconcept.blogspot.com/2014/03/3-sensor-line-follower-ardiuno.html

Hope this post will help you. Thank you.