Main

Topics

Legal

# Robotics Competition | Odd & Even Count | Line Following Robot

In this blog we have discussed about our 2nd stage autonomous robotics competition held at BIT Patna. So, before that please read our previous blog about 1st stage competition.

## Odd & Even Count

The task given was to make the bot choose a path depending on the no of strips. If there is even no of side strips, the bot will choose path1 and if there is odd no of strips the bot will choose path2.

## Path Choosing

Let me give you a demo. Now, let us assume that this is a line following robot and as you can see, here the no of strips are 3 the sensors at the extreme end will give a reading as odd, therefore the bot will choose path1.

And now if the no of strips would be 2,4,6,.. etc, the extreme sensors will give the reading as even and hence the bot will choose path2.

There is also a sensor at the middle to detect obstacles. If any, the bot will stop for 3 secs(all autonomously).

## Programming Part

##### Void Setup()

Here only Pin 3 (Proximity Sensor) is added for obstacle detection. For know about other pins read our previous blog.

``````void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT); //BUZZER

pinMode(A0, INPUT);//EXTRM LEFT
pinMode(A1, INPUT);//LEFT
pinMode(A2, INPUT);//RIGHT
pinMode(A3, INPUT);//EXTRM RIGHT

pinMode(5, OUTPUT);  //left wheel positive
pinMode(6, OUTPUT);  //left wheel negative
pinMode(10, OUTPUT); //right wheel positive
pinMode(11, OUTPUT); //right wheel negative

pinMode(3, INPUT); //PRXIMITY

pinMode(8, INPUT); // LEFT STRIPE CUNT
pinMode(9, INPUT);  // RIGHT STRIPE CUNT

}``````
##### Functions()

We have already discussed most of the functions in the previous blog, this blog represents the continuous of the competition that held at BIT Patna.

Count variable is used for linefollowing. At first count variable set as 0, but when it reached to near strips it’s value changed to 1.

``````if(count==0)
{
{
rtturn1();
}
{
ltturn1();
}

{
slow();
n++;
digitalWrite(2,HIGH);
delay(10);
digitalWrite(2,LOW);

}
}``````

When counting strips if bot counts ODD number of strips according sensors, c used as a variable set as 1 and if bot counts EVEN number of strips, c set as 2.

``````if(count==1)
{
{
slow();
stop();
delay(100);

if(c==0)
c=1;
else if(c==1)
c=2;

digitalWrite(2,HIGH);
delay(10);
digitalWrite(2,LOW);

if(p==c)
{
Serial.print("    P====   ");
Serial.println(p);
Serial.print("    C====   ");
Serial.println(c);

rtturn2();
Serial.print("    turn====   ");
count=2;
Serial.println("    COUNT====   ");

Serial.print(count);

}
}
}``````

Here we use c as a variable, if c=1 then it means bot counts ODD number of strips and bot choose Path1.

``````if((count==2) && (c==1) && (a==0))
{
{
rtturn3();
}
{
ltturn3();
}
{
rtturn1();
a=1;
digitalWrite(2,HIGH);
delay(60);
digitalWrite(2,LOW);
}
}``````

Here we use c as a variable, if c=2 then it means bot counts EVEN number of strips and bot choose Path2.

``````if((count==2) && (c==2) && (a==0))
{
{
rtturn3();
}
{
ltturn1();
}

{
rtturn1();
a=1;
digitalWrite(2,HIGH);
delay(60);
digitalWrite(2,LOW);
}
}``````

## Whole Program

``````int z=0,n=0,c=0,count=0,a=0,p,b=0;
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT); //BUZZER

pinMode(A0, INPUT);//EXTRM LEFT
pinMode(A1, INPUT);//LEFT
pinMode(A2, INPUT);//RIGHT
pinMode(A3, INPUT);//EXTRM RIGHT

pinMode(5, OUTPUT);  //left wheel positive
pinMode(6, OUTPUT);  //left wheel negative
pinMode(10, OUTPUT); //right wheel positive
pinMode(11, OUTPUT); //right wheel negative

pinMode(3, INPUT); //PRXIMITY

pinMode(8, INPUT); // LEFT STRIPE CUNT
pinMode(9, INPUT);  // RIGHT STRIPE CUNT

}
void straight()
{
analogWrite(5,250);
analogWrite(6,0);
analogWrite(10,250);
analogWrite(11,0);
}

{
analogWrite(5,250);
analogWrite(6,0);

analogWrite(10,150);
analogWrite(11,0);
}

{
analogWrite(5,150);
analogWrite(6,0);

analogWrite(10,250);
analogWrite(11,0);
}

void rightturn()
{
analogWrite(5,180);
analogWrite(6,0);
analogWrite(10,0);
analogWrite(11,10);
}

void leftturn()
{
analogWrite(5,0);
analogWrite(6,10);

analogWrite(10,180);
analogWrite(11,0);
}
void stop()
{
analogWrite(5,0);
analogWrite(6,0);

analogWrite(10,0);
analogWrite(11,0);
}
void slow()
{
analogWrite(5,70);
analogWrite(6,0);

analogWrite(10,70);
analogWrite(11,0);
}

void rtturn1()
{
rightturn();
}

void rtturn2()
{
rightturn();

}

void rtturn3()
{
rightturn();
{
break;
}
}

void ltturn1()
{
leftturn();

}

void ltturn3()
{
leftturn();
{

break;
}
}

void loop()
{
if((a==0) || (a==1))
{
{
straight();
}

{
}

{
}
}

if(count==0)
{
{
rtturn1();
}
{
ltturn1();
}

{
slow();
n++;
digitalWrite(2,HIGH);
delay(10);
digitalWrite(2,LOW);

}
}

if(count==1)
{
{
slow();
stop();
delay(100);

if(c==0)
c=1;
else if(c==1)
c=2;

digitalWrite(2,HIGH);
delay(10);
digitalWrite(2,LOW);

if(p==c)
{
Serial.print("    P====   ");
Serial.println(p);
Serial.print("    C====   ");
Serial.println(c);

rtturn2();
Serial.print("    turn====   ");
count=2;
Serial.println("    COUNT====   ");

Serial.print(count);

}
}
}

if((count==2) && (c==1) && (a==0))
{
{
rtturn3();
}
{
ltturn3();
}
{
rtturn1();
a=1;
digitalWrite(2,HIGH);
delay(60);
digitalWrite(2,LOW);
}

}

if((count==2) && (c==2) && (a==0))
{
{
rtturn3();
}
{
ltturn1();
}

{
rtturn1();
a=1;
digitalWrite(2,HIGH);
delay(60);
digitalWrite(2,LOW);
}
}

{
if(((n%2)==0))
{
p=2;
digitalWrite(2,HIGH);
delay(100);
digitalWrite(2,LOW);
}

else
{
p=1;
}

count=1;
digitalWrite(2,HIGH);
delay(10);
digitalWrite(2,LOW);
stop();
delay(300);
}

}``````

Do you want to support our videos ?

Support Our Channel By Shopping parts from Amazon !

1. UNO R3 board, USB Cable : https://amzn.to/3V70iGC
2. L293d motor driver shield : https://amzn.to/41YTtZJ
3. Screws and nuts set : https://amzn.to/422jDL5
4. Plyboard chasis with motors and wheels : https://amzn.to/3Hg1phf
5. Only motor with wheels : https://amzn.to/44853Ug
6. Infrared Sensor x 2 : https://amzn.to/3Nigtz2
7. 4 cell AA Battery holder : https://amzn.to/3n1vdrk
8. 6 cell AA Battery holder : https://amzn.to/3NfS1hp
9. Duracell Ultra Alkaline AA Battery, 8 Pieces : https://amzn.to/422kU4P
10. Jumper wires : https://amzn.to/3oFrq3f
11. Black Tape : https://amzn.to/3oESH5L
12. L-Clamp x4 : https://amzn.to/3HgqhFS