Skip to content

Instantly share code, notes, and snippets.

@bha159
Created May 22, 2017 06:14
Show Gist options
  • Save bha159/29a19fac5a062b19f8c82521f7576b29 to your computer and use it in GitHub Desktop.
Save bha159/29a19fac5a062b19f8c82521f7576b29 to your computer and use it in GitHub Desktop.
For calibrating a line follower bot, which uses three IR sensors.
int sensorPin[]={A0,A1,A2}; /*Array of analog pins for sensor*/
int totalpin=3; /*Total No. of pins*/
const int ledpin=9;
int sensorMin[]={1023,1023,1023}; /*Array to store minimum values*/
int sensorMax[]={0,0,0}; /*Array to store maximum values*/
float sensorAvg[]={0.0,0.0,0.0}; /*Array to store average values*/
void calib()
{
int i,l,temp;
for(l=0;l<1000;l++)
{
for(i=0;i<totalpin;i++)
{
temp=analogRead(sensorPin[i]); /*Read the value from sensor*/
if(temp>sensorMax[i]) /*Find the maximum value*/
{
sensorMax[i]=temp;
}
if(temp<sensorMin[i]) /*Find the minimum value*/
{
sensorMin[i]=temp;
}
}
delay(10);
}
for(i=0;i<totalpin;i++)
{
sensorAvg[i]=(sensorMax[i]+sensorMin[i])/2; /*Find the average value*/
}
}
void setup()
{
Serial.begin(9600);
int i;
for(i=0;i<totalpin;i++)
{
pinMode(sensorPin[i],INPUT); /*To declare analog pins to input mode*/
}
calib(); /* call function for calibration*/
}
int a,b,c,x,y,z;
void loop()
{
a=analogRead(sensorPin[0]);
if(a<sensorAvg[0])
{
Serial.print("black");
x=0; /*x=0 on black surface for sensor connected to A0 */
}
else
{
Serial.print("White");
x=1; /*x=1 on white surface for sensor connected to A0*/
}
Serial.print(" ");
b=analogRead(sensorPin[1]);
if(b<sensorAvg[1])
{
Serial.print("black");
y=0; /*y=0 on black surface for sensor connected to A1*/
}
else
{
Serial.print("white");
y=1; /*y=1 on white surface for sensor connected to A1*/
}
Serial.print(" ");
c=analogRead(sensorPin[2]);
if(c>sensorAvg[2])
{
Serial.print("white");
z=1; /*z=1 on white surface for sensor connected to A2*/
}
else
{
Serial.print("black");
z=0; /*z=0 on black surface for sensor connected to A2*/
}
Serial.print(" ");
Serial.print("\n");
if(x==0 && y==1 && z==0)
{
//Go Straight
}
if(x==1&&y==0&&z==0)
{
//Turn Left
}
if(x==0&&y==0&&z==1)
{
//Turn Right
}
if(x==1&&y==1&&z==0)
{
//Turn Left
}
if(x==0&&y==1&&z==1)
{
//Turn Right
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment