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.

Please support us on YouTube also. Like Share and Subscribe to our channel : https://youtube.com/c/RatnasRoboLab

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)
{
  if((digitalRead(9)==LOW))
  {
  rtturn1();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn1();
  }

   if((digitalRead(A0)==LOW))
   {
      slow();
      while(!(digitalRead(A0)==HIGH));
      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)
{
      if((digitalRead(A3)==LOW))
      {
            slow();
            while(!(digitalRead(A3)==HIGH));
            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))
{
  if((digitalRead(9)==LOW))
  {
  rtturn3();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn3();
  }
    if((digitalRead(A3)==LOW))
     {
     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))
{
   if((digitalRead(9)==LOW))
  {
  rtturn3();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn1();
  }

  if((digitalRead(A3)==LOW))
  {
  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);
}

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

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

void leftadjust()
{
  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();
  while(!((digitalRead(A3)==HIGH) && (digitalRead(9)==HIGH)));
}

void rtturn2()
{
  rightturn();

      while(!(digitalRead(A3)==LOW));
   while(!((digitalRead(A3)==HIGH) && (digitalRead(9)==HIGH)));

}

void rtturn3()
{
  rightturn();
  while(!((digitalRead(A3)==HIGH) && (digitalRead(9)==HIGH)))
  {
      if((digitalRead(A3)==LOW))
         break;
  }
}


void ltturn1()
{
  leftturn();
  while(!((digitalRead(A0)==HIGH) && (digitalRead(8)==HIGH)));

}


void ltturn3()
{
  leftturn();
  while(!((digitalRead(A0)==HIGH) && (digitalRead(8)==HIGH)))
  {

      if((digitalRead(A3)==LOW))
         break;
  }
}


void loop()
{
if((a==0) || (a==1))
{
 if((digitalRead(A1)==LOW) && (digitalRead(A2)==LOW))//straight
  {
    straight();
  }

  if((digitalRead(A1)==HIGH) && (digitalRead(A2)==LOW) )//adjust straight 4 right
  {
    rightadjust();
  }

  if((digitalRead(A1)==LOW) && (digitalRead(A2)==HIGH))//adjust straight 4 left
  {
    leftadjust();
  }
}


  if(count==0)
{
  if((digitalRead(9)==LOW))
  {
  rtturn1();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn1();
  }

   if((digitalRead(A0)==LOW))
   {
      slow();
      while(!(digitalRead(A0)==HIGH));
      n++;
              digitalWrite(2,HIGH);
              delay(10);
              digitalWrite(2,LOW);

   }
}


 if(count==1)
{
      if((digitalRead(A3)==LOW))
      {
            slow();
            while(!(digitalRead(A3)==HIGH));
            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))
{
  if((digitalRead(9)==LOW))
  {
  rtturn3();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn3();
  }
    if((digitalRead(A3)==LOW))
     {
     rtturn1();
     a=1;
              digitalWrite(2,HIGH);
              delay(60);
              digitalWrite(2,LOW);
     }

}


  if((count==2) && (c==2) && (a==0))
{
   if((digitalRead(9)==LOW))
  {
  rtturn3();
  }
   if((digitalRead(8)==LOW))
  {
  ltturn1();
  }

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


 if((digitalRead(3)==HIGH) && (count==0))
 {
   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();
      while(!((digitalRead(3)==LOW)));
      delay(300);
  }

}

Do you want to support our videos ?
https://www.buymeacoffee.com/ratnasrobolab

Support Our Channel By Shopping parts from Amazon !

Product Links

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

Further Readings

Robotics Competition | Line Following Robot

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE

Related :

Follow :