Ultrasonic obstacle avoider + edge avoider robot using ATMega16

Following is the fully tested code of Ultrasonic obstacle avoider + edge avoider robot using ATMega16. You can change the distance of obstacle sensing by editing the RED marked code in the program.

Ultrasonic obstacle avoider + edge avoider robot using ATMega16

Ultrasonic obstacle avoider + edge avoider robot using ATMega16

/*
 * Program of US obstacle avoider + edge avoider robot
 * Created: 12.06.2016 
 * Vidyasagar Academy, Akola; www.vsagar.org

	===== CONNECTION DETAILS =====
	
	Connections of US sensor
	Vcc --> Vcc of dev. board
	Trig --> PB7 of PORTB
	Echo --> PB6 of PORTB
	Gnd --> Gnd of dev board

	Connection of motors
	PB0-PB1 --> RIGHT MOTOR
	PB2-PB3 --> LEFT MOTOR

	Connections of 2 IR sensors
	Left IR sensor --> PA7
	Right IR sensor --> PA1
*/

#define F_CPU 12000000UL
#include 
#include 

void USTrigger()
	{
	//Send a 12uS pulse on trigger line
	
	PORTB|=(1< 60000) break; 
		else 
		continue;
	}
	else
		break;
	}

	if(i==600000)
		return -2;	//Indicates time out

	//Falling edge found

	result=TCNT1;

	//Stop Timer
	TCCR1B=0x00;

	if(result > 60000)
		return -2;	//No obstacle
	else
		return (result>>1);
	
	} // get pulse width completed

int main()

	{ // void main started

	DDRB=0b10011111; // PB7 --> trigger pin i.e. output pin for US sensor
					// PB4 --> buzzer output pin
					// PB3-PB0 --> motor control outputs
	DDRA=0b00000000;
	int sensors;

	uint16_t r;
	
	while(1)

	{ // while started

	sensors=PINA&0b10000010;

		//Send a trigger pulse
		USTrigger();

		//Measure the width of pulse
		r=GetPulseWidth();

		//Handle Errors

		if(r==-1)
		{
			PORTB=0b00010000;
			_delay_ms(10);
			PORTB=0b00000000;
			_delay_ms(1000);
		}

		else if(r==-2)
		{
			PORTB=0b00010000;
			_delay_ms(50);
			PORTB=0b00000000;
			_delay_ms(1000);
		}

		else

		{
			int d;
			d=(r/58.0);	//Convert to cm
			_delay_ms(5);

		if(d>6) 
		{
			PORTB=0b00001001; // forward
		}
	
		if(d<=6)
		{
		
		PORTB=0b00010000; // stop
		_delay_ms(500);
		PORTB=0b00000110; // backward
		_delay_ms(1000);
		PORTB=0b00000000; // stop
		_delay_ms(700);
		PORTB=0b00001010; // power right
		_delay_ms(300); // change this delay as required 
						// to turn the robot in 90 degrees
		}

		}

		// program of edge avoider started

		if(sensors==0b00000000) // both sensors are out of edge
		{
		PORTB=0b00000000; // stop
		_delay_ms(900);
		PORTB=0b00010000; // beep
		_delay_ms(100);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00001010; // power left turn
		_delay_ms(300);
		}

		if(sensors==0b10000000) // right sensor is out of edge
		{
		PORTB=0b00000000; // stop
		_delay_ms(900);
		PORTB=0b00010000; // beep
		_delay_ms(100);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00000101; // power left turn
		_delay_ms(300);
		}

		if(sensors==0b00000010) // left sensor is out of edge
		{
		PORTB=0b00000000; // stop
		_delay_ms(900);
		PORTB=0b00010000; // beep
		_delay_ms(100);
		PORTB=0b00000110; // backward
		_delay_ms(500);
		PORTB=0b00001010; // power right turn
		_delay_ms(300);
		}

	} // while closed

} // main closed

  1. Sunny Dhoke
    September 15, 2016, 4:00 pm

    Nice work Mr.Yash.
    Hope you will keep updating content and add some DIY.
    Thank you

    Reply

Leave a Reply

About the author: Hmm! At last you found me. I’m a student of science and technology doing my B.Sc. in Physics, Mathematics & Electronics. Throughout my studies I observe many things and share them with students which might help in a way or other…!