You have already assembled the PrintBot Renacuajo. So now we are going to program it to follow a black line. Do you know how a line follower works? The image below will show you. The infrared sensor enables the robot to detect whether the ground is black or white, so it can also detect when it is on top of a line, enabling it to follow it.
Now we will see how to program it, step by step, on bitbloq
Let´s get started! Our PrintBot has two infrared sensors which help it to detect when it is on a line. So we will need to read their values. We will call the readings ir_derecho and ir_izquierdo (ir_right and Ir_left, “ir” is short for infrared). As you can see in the diagram above, the right servo is connected to digital pin 9 and the left to digital pin 6.
Notice that the left sensor is connected to pin 3 and the right sensor to pin 2
The infrared sensor gives us a value of 0 when black is detected and 1 otherwise. To remind us, we are going to declare the BLACK variable with the value 0. Variables are “names” that we assign a value. Have a look at how it´s done:
And now? Well, now we need to move the motors so that the robot will follow the line. Have you read the line follower explanation carefully?
1. When the two sensors detect the black line, this means that the robot is on the line, so it must go forward. This makes the two servos turn.
2. When the two sensors detect white, this means that the line has ended or the robot has gone off course, so it should stop.
3. When the left sensor detects black and the right sensor detects white, this means that the robot is veering off to the right, so it needs to turn to the left. To turn to the left, what I do is move only the right wheel (try this at home, you will see that I am right!).
4. When the right sensor detects black and the left sensor detects white, this means that the robot is veering off to the left, so it needs to turn to the right. To turn to the right, I only move the left wheel (do you believe me now?).
Well, that´s clear then! Now we will think about it together. Look closely at the examples and we will look for a simple rule.
Whenever the right sensor detects black, it will move the left wheel, and when the right sensor detects white, the left wheel will stop. Whenever the left sensor detects black, it will move the right wheel, and when the left sensor detects white, the right wheel will stop. Think about it! If you have understood, then making the program is super easy:
The 10 millisecond pause on each servo is because it´s always a good idea to wait a moment after moving it
So now you should now how to program it perfectly, but if you´re still not sure, our friend Berta will help explain it to you in this video. She´s a true robonaut! Are you brave enough to send us your own videos? Write to us at diy@bq.com and we will publish them for you.