Sunday, February 16, 2014

Embedded C code / program for Line follower robot using AVR micro-controller ATMega 16 - IR sensor based

 Are you making a line follower robot using a AVR micro controller - ATMega 16 / ATMega 16A  programmed using AVR Studio?  Here is the code for making a simple line follower robot.
This embedded C programming language Code is based on the following design. You can modify it based on your requirements :
No. of sensors  - 2
Signal from sensors- 1 for white and 0 for black
No. of motors - 2
About motors - Both the motors are DC motors and are connected to the micro-controller through a Motor driver IC.



This code / program is according to the syntax of the AVR studio compiler tool

  Code for Line follower Robot:

#include<avr/io.h>                //Header file
#define sensor1 PA0                //Left Sensor
#define sensor2 PA1                //Right sensor
void sw();
int main(void)                //main function
 {
DDRB=0xFF;                //output pin high
DDRA=0xFC;                //input pin low
while(1)
{
PINA=0x03;                //initialize input pin high in pin register
sw();
}
}
void sw1()
{
if(bit_is_clear(PINA,sensor2))        //checks whether right sensor is low       
PORTB=0b00000000;                //both left and right side motor low if true
else
PORTB=0b00000001;                //right side motor turns on and left motor off if false       
}
void sw2()
{
if(bit_is_clear(PINA,sensor2))        //checks whether right sensor is low   
PORTB=0b00000010;                //right side motor turns off and left motor on if true
else
PORTB=0b00000011;                //both left and right side motor high if false
}
void sw()
{
if(bit_is_clear(PINA,sensor1))    //Checks whether left sensor is low
sw1();            //terminate to this function if true
else
sw2();            //terminate to this function if false
}

Adjust your motors and sensors as per your requirement.


No comments:

Post a Comment

You might also like