In this project the robot uses its sensors to follow around a figure eight course.
The notes on this page will help you to write a program to get your robot following a line.
This picture shows both of the sensors on the black line.
This means the robot can move forwards: both motors are running forwards at their normal speed.
This picture shows the left sensor off the black line. The robot must head to the right.
It can do this by slowing the right motor. The other motor runs at its normal speed.
This picture shows the right sensor off the black line. The robot must head to the left.
The notes on this page will help you to write a program to get your robot following a line.
This picture shows both of the sensors on the black line.
This means the robot can move forwards: both motors are running forwards at their normal speed.
This picture shows the left sensor off the black line. The robot must head to the right.
It can do this by stopping the right motor. The other motor runs at its normal speed.
This picture shows the right sensor off the black line. The robot must head to the left.
It can do this by stopping the left motor. The other motor runs at its normal speed.
Now, if you look at all three of these pictures you will notice something: the left sensor controls the right motor and the right sensor controls the left motor.
Like this:
If the left sensor goes on to the white, If the right sensor goes on to the white,
slow down the right motor. stop the left motor.
Otherwise right motor runs at its normal speed. Otherwise, left motor runs at normal speed.
We can use pseudo-code to help us understand this:
if(left sensor on white)
{
right motor stop
}
else
{
right motor goes at normal speed
}
The pseudo-code is written in English so we can see what we are doing. Once we have worked out the pseudo-code, we need to write it in the Arduino programming language. Here is the pseudo-code shown alongside the actual code:
if(left sensor on white) if(analogRead(A0) > 600)
{ {
right motor stops digitalWrite(8, LOW);
digitalWrite(9,LOW);
} }
else else
{ {
right motor goes at normal speed digitalWrite((8,HIGH); digitalWrite(9,LOW);
} }
The code above controls the right motor, Below that you need to write a very similar piece of code to control the left motor. Of course, you also need to set all the proper pins as OUTPUTs in the setup section of the program, and turn pins 10 and11 on also.
NOTE: The values you put in for your sensor reading will probably not be 600. You will need use the program AnalogReadSerial to check what your own robot's sensors read. On your robot, the left sensor is connected to A0 and the right sensor to A1.
Once your have got your line following program working this next video shows you how to use functions in your Arduino programs. This is in preparation for going up to the next level in programming.