PDA

View Full Version : Line follower code in AVR!!! HELP!!!!!!!!!!!



newtoneins10
06-05-2012, 09:22 PM
wud u pls check da error in da code....any suggestion for improvement??


#include <avr/io.h>

#include <util/delay.h>


void initADC(){
DDRC|=0x00;

ADMUX|=(1<<REFS0)|(1<<ADLAR);
ADCSRA|=(1<<ADPS1)|(1<<ADPS2);


}

uint16_t readADC(uint8_t ch){
ADMUX|=(ADMUX & 0xF0)|(ch & 0x0F);
ADMUX|=ch;
ADCSRA|=(1<<ADEN);
ADCSRA|=(1<<ADSC);

while(!(ADCSRA & ADIF));

ADCSRA|=(1<<ADIF);

return ADC;
}

void pwm_init(){
TCCR1A|=(1<<COM1A1)|(1<<COM1A0)|(1<<WGM11)|(1<<WGM10);
TCCR1B|=(1<<CS12);
TCNT1=0;
}

void pwm(uint16_t left, uint16_t right){

OCR1A=left;//left motor
OCR1B=right;

}

void enable_motor(){
PORTB|=(1<<PORTB1)|(1<<PORTB2); //check

}

void disable_motor(){
PORTB &=~((1<<PORTB1)|(1<<PORTB2));

}

void motor(){

uint8_t ch[6];
int w,right,left,sum=0;
unsigned char i;
initADC();

for(i=0;i<6;i++){

w=(readADC(i)/1024)*5; //converwsion into ADC voltage
if(w>2.5) ch[i]=1; //inserting 1 or 0 in ch array
else ch[i]=0;
}

for(i=0;i<4;i++){
sum+=ch[i]; //summing left (0-3)array elements

}

left=sum;


for(;i<7;i++){
sum+=ch[i];

}

right=sum; //right array elements (4-6)

if(right>left)
{
_delay_ms(10);

if(right>1) { //hard right

pwm(2,1024);
//check

PORTD=0x09; //0b00001001 right motor in forward dir left reverse

}
else {



pwm(2,1024);

}
if(right<left){

_delay_ms(10);

if(left>1) {
pwm(1024,2);
PORTD=0x06; //0b00000110 right motor reverse left motor forward
}
else pwm(1024,2);
}

else {
_delay_ms(10);
pwm(1024,1024); //left=right

} //more modification when all da digits are 1 last direction history

}

void main()
{

DDRD=0xFF;
DDRC=0xFF;
PORTD=PORTC=0x00;


initADC();
pwm_init();

enable_motor();

while(1) {motor();}



}

everlearner2611
07-19-2012, 11:37 PM
i m not sure what microcontroller or board u r using.but i used this. i m not qualified enough to check the errors.lets see if my code helps u.
$regfile = "m16def.dat"
$crystal = 1000000

Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2

Config Adc = Single , Prescaler = Auto , Reference = Avcc

Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down

Config Porta = Input
Config Portb = Output
Config Portd = Output

Dim L As Integer , C As Integer , R As Integer , Lm As Integer , Cm As Integer , Rm As Integer,
Dim Rb As Integer , Rw As Integer , Cb As Integer , Cw As Integer , Lb As Integer , Lw As Integer

Start Adc
Cls
Start Timer1

Lcd "calib white values"
Waitms 5000
Lw = Getadc(4)
Cw = Getadc(0)
Rw = Getadc(2)
Waitms 1000
Cls



Lcd "calib black values"
Waitms 5000
Lb = Getadc(4)
Cb = Getadc(0)
Rb = Getadc(2)
Waitms 1000
Lm = Lb + Lw
Lm = Lm / 2
Cm = Cb + Cw
Cm = Cm / 2
Rm = Rb + Rw
Rm = Rm / 2
Cls

Lcd "start line"
Lowerline
Lcd "following"
Waitms 3000


Do
L = Getadc(4)
C = Getadc(0)
R = Getadc(2)
If L < Lm And C > Cm And R < Rm Then
Pwm1b = 200
Portd.3 = 0
Pwm1a = 200
Portd.6 = 0
Cls
Lcd "straight"

Elseif L > Lm And C < Cm And R < Rm Then
Pwm1b = 0
Portd.3 = 0
Pwm1a = 200
Portd.6 = 0
Cls
Lcd "left"

Elseif L > Lm And C > Cm And R < Rm Then
Pwm1b = 20
Portd.3 = 0
Pwm1a = 180
Portd.6 = 0
Cls
Lcd "left"

Elseif L < Lm And C < Cm And R > Rm Then
Pwm1b = 200
Portd.3 = 0
Pwm1a = 0
Portd.6 = 0
Cls
Lcd "right"

Elseif L < Lm And C > Cm And R > Rm Then
Pwm1b = 20
Portd.3 = 0
Pwm1a = 180
Portd.6 = 0
Cls
Lcd "left"


End If
Loop
End