How to give U turn to your robot?

When a robot takes a U-turn, it looks very beautiful. This act appears to be quite intelligent. So if you want to implement this interagency in your robot like wheeled robot (BLFR category) you require at least three IR sensors in your robot. However, we shall recommend to use the 5 sensors array. This gives you more accuracy and control over your robot.

We also recommend to use simple IR sensors either TSOP type or plain photo diode type. We have tested the following source codes, on both types of sensors with same results.

U turns program for ATMega8

Here is the programming technique for ATMega8 MUC. Check the following program for the same.

	/*	
		************ IMPORTANT NOTICE *************
		This program is designed by Prof. Dattaraj Vidyasagar
		For YRC students of Vidyasagar Academy
		PROGRAM OF U TURN ROBOT FOR ATMega8 ONLY
		Created on: 19.10.2014, Time: 08.34.24

	*/

#define F_CPU 12000000UL
#include<avr/io.h>
#include <util/delay.h>
int main()
{
	DDRB=0X1E; // PB1-PB4 as output pins
	DDRC=0X00; // PC1, PC2 and PC3 as input pins
				// This program assumes that you are using
				// the 5-sensor array for your robot
				// with middle 3-sensors are used
	short Status=0;

void Forward();
	{
		PORTB |= (1<<1)|(1<<4);
		PORTB &= (~((1<<2)|(1<<3)));
	}

void PowerRight();
	{
		PORTB |= (1<<2)|(1<<4);
		PORTB &= (~((1<<1)|(1<<3)));
	}

void PowerLeft();
	{
		PORTB |= (1<<1)|(1<<3);
		PORTB &= (~((1<<2)|(1<<4)));
	}

void SoftLeft();
	{
		PORTB |= (1<<1);
		PORTB &= (~(1<<2));
	}

void SoftRight();
	{
		PORTB |= (1<<4);
		PORTB &= (~(1<<3));
	}


	while(1)
	{
	Status = PINC&0b0001110; // sensors connections and status defined
		
		{ // block of line following code

			if(Status==0) // 000
			{
				PORTB = 0x00; 
			}

			else if(Status==10) // 101
			{
				Forward();  
			}

			else if(Status==2) // 001
			{
				PowerLeft();   
				_delay_ms(3);
			}

			else if(Status==6) // 011
			{
				SoftLeft(); 
				_delay_ms(3);
			}

			else if(Status==8) // 100
			{
				PowerRight();  
				_delay_ms(3);
			}

			else if(Status==12) // 110
			{
				SoftRight();  
				_delay_ms(3);
			}

		} // end of line following code

		{ // block of U turn code

			if(Status==14) // 111
			{
				_delay_ms(20); // to move forward for a while
				PORTB=0x00;
					while(1)
					{
					PowerRight();   
					_delay_ms(7);
						if(Status==10) // 101
						PORTB=0x00;	
						break;	// break out of while loop
					}		// U turn completed!!!
			}
		} // end of U turn code
	}
	}

In the above program, at every ‘if’ condition, the binary code is given for the status of the three sensors. Here the three sensors are connected to PC1, PC2 and PC3.

U turns program for ATMega16

This program uses IR based 5 sensors array. This is better modification of the above given program. Check it as given below.

#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>
int main()
{
	DDRB=0b00011110; // PB1-PB4 as output pins
	DDRC=0b00000000; // PC0-PC4 as input pins
	short IRStat=0;
	/*
	* Left motor --> PB4(+) and PB3(-)
	* Right motor--> PB2(-) and PB1(+)

 	* Sensor 1 connected to PC0
 	* Sensor 2 connected to PC1
 	* Sensor 3 connected to PC2
 	* Sensor 4 connected to PC3
 	* Sensor 5 connected to PC4
 	*/

	while(1) // infinite loop
	{
	IRStat = PINC&0b00011111; // variable to record sensor status
		{ // block of line following code
			if(IRStat==0) // 00000
			{
				PORTB=0b00000000; // STOP
			}

			else if(IRStat==27) // 11011
			{
				PORTB |= (1<<1)|(1<<4);
				PORTB &= (~((1<<2)|(1<<3)));
			}

			else if(IRStat==15) // 01111
			{
				PORTB |= (1<<1)|(1<<3);
				PORTB &= (~((1<<2)|(1<<4)));
				_delay_ms(3);
				PORTB = 0x00;
				_delay_ms(7);
			}

			else if((IRStat==7)||(IRStat==3)||(IRStat==19)||(IRStat==23)) 
			// 00111 or 00011 or 10011 or 10111
			{
				PORTB |= (1<<1)|(1<<3);
				PORTB &= (~((1<<2)|(1<<4)));
				_delay_ms(10);
				PORTB = 0x00;
				_delay_us(1);
			}

			else if(IRStat==30) // 11110
			{
				PORTB |= (1<<2)|(1<<4);
				PORTB &= (~((1<<1)|(1<<3)));
				_delay_ms(3);
				PORTB = 0x00;
				_delay_ms(5);
			}

			else if((IRStat==25)||(IRStat==24)||(IRStat==28)||(IRStat==29)) 
			// 11001 or 11000 or 11100 or 11101
			{
				PORTB |= (1<<4)|(1<<2);
				PORTB &= (~((1<<3)|(1<<1)));
				_delay_ms(10);
				PORTB = 0x00;
				_delay_us(1);
			}
		} // end of line following code

		{ // block of U turn code

			if(IRStat==31) // 11111
			{
			_delay_ms(50);
			PORTB=0b00000000;
				while(1)
				{
				PORTB=0b00010100;
				_delay_ms(7);
					if(IRStat==27)
					PORTB=0x00;	
					break;	//break out of while loop
				} //left turn completed!!!
			}
		} // end of U turn code
	}
	}

Our senior students are also working on new models of US obstacle avoiding robots along with U-turn capability. So stay tuned!

About the author: Admin of Vidyasagar Academy to post all types of educational material required for our students. This material is equally helpful to our online students.

Comments on this entry are closed.

  • Sameer Hatekar

    September 3, 2015, 7:39 PM

    This code works on ATMega8.
    Please give it for 8051 n ATMega16 and higher