code
/*
line follower
a line follower having 3 sensors separated by 1.5 cm,to
follow black line of2 cm thickness on white background
a pair of motors connected for locomotion.
The circuit:
* sensors connected from pin ADC0,ADC1,ADC2 according to
sensor circuit diagram.
* +5v dc connected to VCC
* 0v dc connected to GND
* motor connected from pin PB0,PB1,PC0,PC1 to microcontroller
via motor driver accoding to motor driver circuit.
* pins OC0 AND 0C2 connected to motor driver accoding to
motor driver circuit.
created 2008
by a.k.nandan
modified Jun 2009
*/
//sample code for line follower using ATMEGA 16
#include<avr\io.h>
#include<util\delay.h>
void delay_ms(unsigned char time_ms)
{
unsigned short delay_count = F_CPU / 4000;
unsigned short cnt;
asm volatile ("\n"
"L_dl1%=:\n\t"
"mov %A0, %A2\n\t"
"mov %B0, %B2\n"
"L_dl2%=:\n\t"
"sbiw %A0, 1\n\t"
"brne L_dl2%=\n\t"
"dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
:"r"(time_ms), "r"((unsigned short) (delay_count))
);
}
void stop()
{
PORTC=0x00;
PORTB=0x00;
}
void turn_left()
{
PORTC=0x01;
PORTB=0x01;
}
void turn_right()
{
PORTC=0x02;
PORTB=0x02;
}
void move_backward()
{
PORTC=0x01;
PORTB=0x02;
}
void move_forward()
{
PORTC=0x02;
PORTB=0x01;
}
/*void delay(int a)
{
int i;
for(i=0;i<=a;i++)
delay_ms(100);
}*/
void adc_init0()
{
ADMUX=0xE0;
ADCSRA=0xA4;
DDRA=0x00;
}
void adc_init1()
{
ADMUX=0xE1;
ADCSRA=0xA4;
DDRA=0x00;
}
void adc_init2()
{
ADMUX=0xE2;
ADCSRA=0xA4;
DDRA=0x00;
}
void pwm_int1(int x)
{
DDRB=0xff;
TCCR0=0x6B;
OCR0=x;
}
void pwm_int2(int y)
{
DDRD=0xff;
TCCR2=0x6B;
OCR2=y;
}
int main()
{
DDRB=0xff;
DDRC=0xff;
unsigned char s0,s1,s2;
while(1)
{
adc_init0();
ADCSRA|=(1<<ADSC);
delay_ms(1);
s0=ADCH;
adc_init1();
ADCSRA|=(1<<ADSC);
delay_ms(1);
s1=ADCH;
adc_init2();
ADCSRA|=(1<<ADSC);
delay_ms(1);
s2=ADCH;
if((s1>min1)&&(s2<mid2)&&(s0<mid0))
{
pwm_int1(255);
pwm_int2(255);
move_forward();
}
if((s1>min1)&&(s2>mid2)&&(s0<mid0))
{ pwm_int1(sl);
pwm_int2(fst);
turn_left();
// pwm_int1(220);
// pwm_int2(220);
// move_forward();
//delay_ms(1);
}
if((s1>min1)&&(s2<mid2)&&(s0>mid0))
{
pwm_int1(fst);
pwm_int2(sl);
turn_right();
// pwm_int1(220);
// pwm_int2(220);
//move_forward();
//delay_ms(1);
}
if((s1<min1)&&(s2<mid2)&&(s0<mid0))
stop();
//move_forward();
}
return 0;
}