How to control your robot using single IR sensor?

This is the NINTH PROGRAM in your course of FUNDAMENTALS OF ROBOTICS. To use this program, you must use our robotics kit already supplied to you.

If you do not have this kit, then you can purchase our distance learning programme in robotics or contact us to join our regular batches of robotics.

Now read the following program carefully and work as per the given instructions, to enjoy your first program in robotics.

To use this program directly for your robotic kit, copy it, create new project in AVR Studio and then paste it into the coding area. Then compile it.

/*
	Level II project of 'Controlling motors with single sensor'
	Applicable to ATMega8/16/32/128
	Designed by: Vidyasagar Academy, Akola
	Website: www.vsagar.org

	*** CONNECTION DETAILS OF KIT ***
	1) The 2 motors in your kit, are connected to PB4-PB1, as follows:
	   Left motor:  PB4 -> (+) and PB3 -> (-)
	   Right motor: PB1 -> (+) and PB1 -> (-)
	2) Connect one IR sensor to PC0 in your kit.
*/

#define F_CPU 12000000UL // defining the crystal frequency 12MHz 
			// given on your dev. board of ATMega8

#include <avr/io.h> // including the input-output 
		// to define the input output ports and pins
		// this file is inside the AVR folder 					

#include <util/delay.h> // including the delay file
			// this file is inside the 
			// utilities (util) folder

	int main() // starting the main function of program

	{ // main function brace opened

	DDRB=0b00011110; // PB4-PB1 of PORTB are defined as output pins
	DDRC=0b0000000;  // PC6-PC0 of PORTC are defined as input pins

	int s=0; // 's' is the variable to store the status value of sensor	
			 // when we write 'int s', it creates a location in memory of 
			 // microcontroller.
			 // initially '0' is stored into 's' memory location

	while(1) // starting the infinite loop to repeat the action infinitely
	
	{ // while loop brace opened

	s=PINC&0b0000001; // assigning the variable 's' to PC0 of PORTC
			// so that the output status of sensor will be 
			// stored into the variable 's'
			// since one sensor in our kit is connected 
			// to PC0 and other to PC3 
			// Note: PC3 sensor is not used in this program
			// only PC0 sensor is used

		if(s==0b0000001) // white surface below the sensor
		{
		PORTB=0b00010010; // both motors rotate in forward direction
		}

		else // black surface below the sensor
		{
		PORTB=0b00000000; // both motors are OFF
		}

	} // while loop closed

	} // main function closed

/* 
	=== HOW TO USE AND RUN THIS PROGRAM IN YOUR KIT? ===
	1) First read the program carefully. Understand the steps as taught to you.
	2) Connect your kit to USB port.
	3) Burn the 'hex' file into your kit.
	4) Connect both the IR sensors, to PC3&PC0 in your kit.
	5) Now connect battery and switch on the kit.
	6) First keep white paper below the sensor.
	7) The robot will move in forward direction.
	8) Now keep black surface below it.
	9) Both motors will be OFF and the robot stops.
	10) Is it working? Nice! You did it.
	11) Now don't forget to give your feedback.

*/ 
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.