Before we install the sensors on the robot, let’s get back to our diagram, where we shall mark that we have the line sensor that distinguishes white and black colors, as well as their
shades, which it sees as grey reading them as basic analog signals. We will also have a function that will inform us about the color of the background under the sensor. We are now going to realize the algorithm, which we devised
when we were talking about
our robot moving along a line. Let’s now interconnect these two functions. Okay, I am back. Now we know everything that would help us set our robot in
motion. We only need to improve our robot a bit by adding the path and the code. Speaking of the robot, I have installed 2 sensors as close to the borderlines as I could, because if they are too far from them, the robot will move until it hits the line with one of its sensors, and it will go off the
path. Do we need this? I don’t think so. After that, it will lean back and go in zigzags. No, we certainly don’t need that. That’s why I’ve placed the sensors next to the line. As for the path, I have drawn one in a simple graphic editor. I want to it to look like a race track with some straight sections, swift and sharp turns, and so on. You can find the sample in Additional materials or draw your own track, and then type “broadsheet print” in the search engine, find a printing office, call them and ask if they print on banner fabric. When they print it, you’ll have your own track. Now let’s deal with the code. I have taken one of our previous sketches as the basis, the one where we already
experimented with the robot’s movement. I have then corrected it a bit. Firstly, I have added macrodefinitions for the sensors’ pins, as well as several other macrodefinitions, which will help us make the code more readable. These parameters we shall be transmitting to the function of sensor readings. This marks the border between the black and the white, while these ones are the set speeds for the forward and backward
rotation of the motors. Remember, what we had in our diagram? I have also determined the function of sensor readings. We could have done without it, but with the further improvement of
the program, the code could become less readable, that’s why I have decided to create a
separate function for this. Here it is. It will be returning a boolean value, true or false, depending on the color of the background under the sensor. As parameters, it takes “right” or “left” to determine which sensor readings to perform. What happens is, if we receive “left”, we will read off the value from the pin, to which the left sensor is connected. If it’s higher than the threshold
value, the sensor will return us the “false” value, or “true” if that’s not the
case. The same happens if we receive “right”, but already for the right sensor. Now we have a function to control the motors, and a function to do
the reading of the sensors. We also know how to connect them, and we will do so in our code. According to our table, if both sensors show “white”, i.d. the lineSensor function returns the “true” value, the robot moves forward. Otherwise, if the right sensor shows “black”, i.e. lineSensor (right) returns the “false” value, the robot will turn
right, and the right wheel will turn backward, while the left wheel will turn forward. The third case we need to look at is when the right sensor shows “white”, and the left sensor shows “black”. In this case, the robot will turn left. Finally, if by any chance both sensors show us “black”, or any other unexpected situation happens, the robot will just stop. That’s it. Let’s see how it works, but before that, let’s read the sensor values
once again in order to determine the borderline between the black and the white. What do we see? In my visualizer program, we have these boxes with the max and the
min values, which are very
convenient, so I can see that the values change from 37 to 485. I am not even going to work with trimming resistors – I will just determine that the borderline between the black and the white is at the
value of 200, as mentioned in the code. If I had 400, I would change one of the macrodefinitions. Now let’s see if the robot is going to move in the way we
had described. Before uploading the program, don’t forget to start the Port Monitor or its visualizer, otherwise you will experience problems with the
upload. [MUSIC] Let’s see what we have here. [MUSIC] [MUSIC] [MUSIC] [MUSIC] [MUSIC] So, at a first glance, we have set our robot in motion
and it moves along the
line, so our main task
has been completed. However, there are a lot of drawbacks. Firstly, when the robot turns, it moves discontinuously. Secondly, it’s obvious that at different battery charge levels, the same strictly set values will lead to a different result, i.e. we will always need to change the invariables which determine the speed,
for instance. Anyway, we will be talking about an alternative way later, which has its own advantages. [END] [END] [END]