code

/*

obstacle detector

an autonomous vehicle having 3 sensors spaced at 60 degree

from the central sensor on either side and 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

*/

#include<avr\io.h>

#include<util\delay.h>

#define mid0 10

#define mid1 17

#define mid2 52

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 delay(int a)

{

int i;

for(i=0;i<=a;i++)

delay_ms(100);

}

void forward()

{

PORTB=0x01;

PORTD=0x01;

}

void stop()

{

PORTB=0x00;

PORTD=0x00;

}

void backward()

{

PORTB=0x02;

PORTD=0x02;

}

void left()

{

PORTB=0x01;

PORTD=0x00;

}

void right()

{

PORTB=0x00;

PORTD=0x01;

}

void left_turn()

{

PORTB=0x02;

PORTD=0x01;

}

void right_turn()

{

PORTB=0x01;

PORTD=0x02;

}

void open_grip()

{

PORTD=0x03;

}

void close_grip()

{

PORTD=0x04;

}

// control(left , right)

void control(unsigned char x,unsigned char y)

{

TCCR0=0x6B;

TCCR2=0x6B;

OCR0=x;

OCR2=y;

}

int main()

{

int s0;

int s1;

int s2;

/*

unsigned char s0;

unsigned char s1;

unsigned char s2;

unsigned char s3;

unsigned char s4;

unsigned char s5;

unsigned char s6;

unsigned char s7;

*/

DDRA=0x00;

DDRB=0xFF;

DDRC=0xFF;

DDRD=0xFF;

ADMUX=0xE0;

ADCSRA=0xA4;

//ADCSRA|=(1<<ADEN);

ADCSRA|=(1<<ADSC);

delay_ms(1);

while(1)

{

ADMUX=0xE3;

delay_ms(10);

s0=ADCH;

ADMUX=0xE4;

delay_ms(10);

s1=ADCH;

ADMUX=0xE5;

delay_ms(10);

s2=ADCH;

control(255,255);

forward();

if(s1>mid1)

{

backward();

delay(2);

left_turn();

delay(9);

}

if(s0>mid0)

{

backward();

delay(2);

left_turn();

delay(9);

}

if(s2>mid2)

{

backward();

delay(2);

right_turn();

delay(9);

}

if(s1<mid1&&s0<mid0&&s2<mid2)

{

forward();

}

}

return 0;

}//end of main