Wednesday, 24 June 2015

Line Follower Robot

int last_direction; // variable declaration

#define forward 0 //flag define

#define left 1    //flag define

#define right 2   //flag define



void robot_move(int direc)//define a function named robot_move which will return direction

{

if(direc==forward)        //if the direction is forward

{

 PORTD=0x05;              //IN1=1, In2=0, IN3=1, IN4=0 ,ie both motor will move forward

 Delay_ms(1);            //wait for 10 mili second

}


if(direc==left)          //if the direction is left

{

 PORTD=0x01;             //IN1=1,In2=0,IN3=0,IN4=0

 Delay_ms(1);           //wait for 10 mili second ie left motor will stop, right will move forward

}

if(direc==right)         //if the direction is right

{

 PORTD=0x04;             //IN1=0,IN2=0,IN3=1,IN4=0 ;ie right motor will stop, left will move forward

 Delay_ms(1);           //wait for 10 mili second

}

}




void main()

{

   TRISD=0x00;                  //PORTD is output

   TRISB1_bit=1;               //Declare RB1 as input

   TRISB2_bit=1;               //Declare RB2 as input

   while(1)

   {


   if(RB1_bit==1&& RB2_bit==1) // When both sensors are on black line

   {

    robot_move(forward);      //move forward

    last_direction=forward;   //save forward in the variable named last direction


    }

   else if(RB1_bit==1&& RB2_bit==0) // When left sensor is on black line

   {

    robot_move(left);               //move left

    last_direction=left;             //save left in the variable named last direction



    }

   else if(RB1_bit==0&&RB2_bit==1)// When right sensor is on black line

   {

    robot_move(right);            //move right

    last_direction=right;         //save right in the variable named last direction


   }


   else if (RB1_bit=0&&RB2_bit==0)// When none of the sensors is on black line

   {


    robot_move(last_direction);  //move to last direction


   }

   }


}

No comments:

Post a Comment