T3W4: Using the Line Sensors

The Cutebot robot has a pair of optical sensors on its underside that it can use to steer along a linear path, with the help of some programming. This week we will write a short program to make the Cutebot follow a line. Next week we will combine the line following and distance sensing to make a program that will let us have multiple Cutebots on a robot freeway!

Line Sensor

The Cutebot robot's line sensors are two small black rectangles on the bottom of the robot's chassis. They have an infrared LED and detector and output a digital signal that depends on whether there is something directly in front of the sensor to reflect the light back. Because of this they can be fooled by a dark surface, and the black table tops in the FabLab are too dark to see a reflected signal. They could see and follow a white line on the black tabletop, but the logic of the program would have to be reversed!

Line Sensor Programming

Open the micro:bit MakeCode home page and start a new project. Then click on the [Advanced]->[Extensions] menu and load the Cutebot library.

We will put our startup code and initialize any variables in the "on start" block, and put the line following code in the "forever" block. Here is the startup code that waits for the A button to be pressed (same as last week):

The rest of our program can go in a "forever" block so it runs continuously after the A button is pressed. Our program will repeatedly check the reading from the line sensor and adjust the speed of the wheels separately to turn slightly if the robot starts to cross over the line.

Program Constants

Like last week, there are some number values that we will use in multiple places in the program - the robot speed and the slower speed for one wheel to turn. It is good programming practice to put numbers like that into constant variables: it gives us only one place in the program that we need to edit if we want to change the value, it prevents us from errors caused by missing a value when we do change it, and it gives us documentation on what the value means for the program.

Create two variables called "FULLSPEED" and "TURNSPEED" (or whatever makes sense to you) and put a "set [variable] to (value)" statement from the [Variables] menu at the beginning of the "on start" code block. We make the names all in capitals to remind us that they are constant values we set once instead of computing their values as part of the program. Then use those (FULLSPEED) and (TURNSPEED) constants in your program instead of entering numeric values in the code. This will make it easier for us to experiment with the speeds and get the robot following the line.

Following A Line

The "Tracking State is [o o]" function returns a Boolean (true or false) value corresponding to the state of the optical line sensors. If a sensor sees a reflection, the corresponding blue light on the top of the robot board turns OFF and this corresponds to an empty circle in the Tracking State function. The line is wide enough to turn off both sensors if the robot is centered on top of it, so a state of ON ON (two filled in dots) means go straight ahead. If one light turns on it means we have veered off the line so we will turn in the correct direction to get us back on. And if both lights are OFF (two hollow dots) we have lost the line all together, and we can slow down to make the robot easier to catch (or should we stop and wait for a button press? Try it both ways!).

Here is my version of the code:

Final program version

Adding Features

To help with debugging you could add block to change the color of the headlights depending on the state of the line sensors.

For fun you could add some animated pictures on the LED matrix that show the state of the robot - arrow keys or whatnot.

If the robot goes off the line you could add code to turn off the motors and wait for another button press to continue. Or you could have it wander around looking for the line again - but don't roll off the end of the table!