I would be explaining about making a line follower, using 3 and 2 sensors. I would also be telling the advantage of using 3 sensors over two sensors.
1) Have a sensor circuit ready. You can check my post to know how to make a sensor ckt.
2) Have microcontroller (I would be using atmega8). and a motor driver ready.
Setup of sensors:
Have the sensors setup on the chassis in such a way that center sensor is atleast 2cm away from left and right sensor.( Why 2cm you will know later).
Now the LOGIC part: Assuming that arena is white in colour and line is black in colour.
L C R Direction
1 0 1 Forward [ both motors move in same direction]
0 0 1 Left [ only right motor moves]
1 0 0 right [only left motor moves]
0 0 0 Forward. ( we have encountered a junction over here)
Code to be written :
int L; //left sensor connected to 1st pin of Portc, which is PINC.0
int R;// right sensor connected to 2nd pin of Portc, Pinc.1
int C;// center sensor connected to 3rd pin of Portc, Pinc.2
Motors are connected to PORTD pins.
AA stands in hex as 10101010 [ Most significant bit to least significant bit]
So the motor pins are connected as follows: Consider only the first four bits/pins, bcz we wont be using the last four bits/pins.
When R+,L+ are high. Both the motors would rotate in one direction.
When R+ is high, rest ground. Only the right motor would rotate.
Same thing for rest all.
I hope you did understand the code . I will be posting the ckt of it shortly.
If any queries please ask me.