Friday, 18 January 2013

SAMPLE CODE FOR THE LINE FOLLOWER (ROUND 1)


#define F_CPU 1000000UL            //Define Clock speed. This is currently set to 1Mhz(Which is default for a factory shipped AVR Micro). UL represents Hertz.
#include <avr/io.h>                //Header file for basic I/O Operations
#include<util/delay.h>          //Header file for the Delay Routine


void wait(float sec, int freq)             //Delay Routine (Leave it as it is)
{

int i;

if(freq==1)
for(i=0;i<(int)(1302*sec);i++)
_delay_loop_1(0);

if(freq==2)
for(i=0;i<(int)(2604*sec);i++)
_delay_loop_1(0);

if(freq==4)
for(i=0;i<(int)(15*sec);i++)
_delay_loop_2(0);

if(freq==8)
for(i=0;i<(int)(30*sec);i++)
_delay_loop_2(0);

if(freq==12)
for(i=0;i<(int)(46*sec);i++)
_delay_loop_2(0);

if(freq==16)
for(i=0;i<(int)(61*sec);i++)
_delay_loop_2(0);

}



int main()                    //Start of MAIN
{

DDRC=0x00;    //Set PORT C as input for the sensors.
/*Left sensor connected to bit 7 of PORT C (Change it a/c to your setup)
   Middle sensor connected to bit 6 of PORT C (Change it a/c to your setup)
   Right sensor connected to bit 5 of PORT C (Change it a/c to your setup)
*/

DDRD=0xFF;    //Set PORT D as output for connection with motor driver

/*A1-->PD0--> + --> +ve of the left motor
  A2-->PD1--> - --> -ve of the left motor
  B1-->PD2--> + --> +ve of the right motor
  B2-->PD3--> - --> -ve of the right motor
*/

int middle_sensor=0,left_sensor=0,right_sensor=0;

PORTD=0b00010101;       //Motor driver pulses along with enable active at PD4-->ENABLE

while(1)    //Infinite Loop
{
 left_sensor=PINC&0b10000000;  //Left sensor connected to bit 7 of PORT C (Change it a/c to your setup)
 middle_sensor=PINC&0b01000000;   //Middle sensor connected to bit 6 of PORT C (Change it a/c to your setup)
 right_sensor=PINC&0b00100000;    //Right sensor connected to bit 5 of PORT C (Change it a/c to your setup)

if(left_sensor==1 && middle_sensor==0 && right_sensor==0)     //For sensor state : 1 0 0 -> sharp left
{
PORTD=0b00010110;    //Motor driver bit combination for sharp left turn
wait(.2,8);      
wait(1,8);
PORTD=0b00010101;            //Move Forward
}

if(left_sensor==0 && middle_sensor==0 && right_sensor==1)     //For sensor state : 0 0 1 -> Sharp Right
{
PORTD=0b00011001;            //Motor driver bit combination for sharp right turn
wait(.2,8);
wait(1,8);
PORTD=0b00010101;            //Move Forward
}

if(left_sensor==1 && middle_sensor==1 && right_sensor==1)     //For sensor state : 1 1 1 -> All Sensor on horizontal strip(Move Forward and flash Led)
{

PORTD=0b00010101;            //Move Forward if you are on a horizontal strip.
PORTD=0b10000000;     //Glow an LED if on a horizontal strip.(LED connected to bit 7 of PORT D)
}

/*(Here in this code i have not written all the possible logic condtions (for three sensors!) which might arise on the track.

You will require to put all other logic condtions which you find is necessary.)*/

} //END of WHILE
 return 0;
} //END OF MAIN

No comments:

Post a Comment