How to control your robot using two IR sensors?

This is the TENTH 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 TWO SENSORS'
	Applicable to ATMega8/16/32/128
	Designed by: Vidyasagar Academy, Akola

	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 the two IR sensors to PC3 & PC0 in your kit.
	   Connect LEFT SENSOR to PC3 and RIGHT SENSOR 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 					

#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 the 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; // 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: Left sensor is connected to PC3 and
			// right sensor to PC0

		if(s==0b0001001) // white surface below both sensors
		PORTB=0b00010010; // both motors rotate in forward direction
				// so robot moves forward

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

	} // while loop closed

	} // main function closed

	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 both the sensors.
	7) The robot will move in forward direction.
	8) Now keep black surface below the sensors.
	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.