How to control LEDs using IR sensors in your robotic kit?

This is the FIFTH 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 LEDs with 2 sensors'
	Applicable to ATMega8/16/32/128
	Designed by: Vidyasagar Academy, Akola
	Website: www.vsagar.org

	*** CONNECTION DETAILS OF KIT ***
	1) The 4 LEDs in your kit, are internally connected to PB4-PB1 
	2) Connect two IR sensors: LS to PC3 and RS to PC0.
*/

#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 					

	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&0b0001001; // masking the variable 's' to PC0 & PC3 of PORTC
			 // so that the output status of sensors will be 
			// recorded in variable 's'

			// Note: PC3 is left sensor and PC0 is right sensor
	
	// When it is black surface below a sensor, its output=0
	// When it is white surface below it, its output=1

		if(s==0b0001001) // white surface below both sensors
		{
		PORTB=0b00011110; // all LEDs ON
		}

		if(s==0b0000001) // white surface below right sensor
		{
		PORTB=0b00000110; // LEDs connected to PB2&PB1 are ON only
		}

		if(s==0b0001000) // white surface below left sensor
		{
		PORTB=0b00011000; // LEDs connected to PB4&PB3 are ON only
		}

		else // black surface below both sensors
		{
		PORTB=0b00000000; // all LEDs will be 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) Keep a white paper below one IR sensor.
	7) First 2 LEDs will glow.
	8) Now keep white paper below the next sensor, the other 2 LEDs will glow.
	9) When you keep white paper below both sensors, all LEDs will glow.
	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.