T3W3: Using the Distance Sensor

Welcome back from Spring Break! For our third class we will try to use the robot's ultrasonic distance sensor to keep it from running into things.

Distance Sensor Programming

The Cutebot Robot's distance sensor is the two silver cylinders that look like eyes on the front of the robot. The eyes are ultrasonic transducers that send out a chirp of sound at a frequency that is too high to hear, and then measure the time it takes for the sound to reflect off an object in front of the robot and return to the ultrasonic receiver. The longer it takes to receive the reflected signal, the further away the object is.

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


Startup Routine

A program starts running as soon as the robot is turned on or it is loaded on the micro:bit, which can cause "runaway robot" problems if you are not ready. So the first thing we will do is make a program to pause until a button is pressed before running the rest of the program. We will use the headlights to show us what mode the robot is in so we know when the program is done loading.

The first program code we should run is to stop the motors in case the robot was running a previous program when it got reset up updated. Then use a "while" loop to wait for the button to be pressed.

Enter the following program and test it out by watching the color of the LED headlights - orange/yellow means it is waiting for a button press, and green means 'go' on to the rest of the program.

Avoid Obstacles Routine

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 distance reading from the sonar sensor. If there is no object in front of the robot, it will go forward, but if there is an object, it will turn the robot until there is nothing directly in front, and then it will continue straight ahead.

Program Constants

There are some number values that we will use in multiple places in the program - the robot speed and the distance to an obstacle that makes us stop. 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 "SPEED" and "MINDISTANCE" (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 (SPEED) and (MINDISTANCE) constants in your program instead of entering numeric values in the code.

Avoiding Obstacles

The "HC-SR04 Sonar unit [inches]" function in the [CuteBot] menu returns the distance to the nearest object in front of the sensor. You can pick the units of the distance between inches and cm - extra points for using metric like the rest of the world! We will save the distance in another variable called "SonarDistance" to avoid having to take redundant readings in the code logic. If SonarDistance < MINDISTANCE we want to stop the robot, change the headlight color, and turn the robot until there is nothing in the way.

Use a "while" loop to repeatedly turn for a while and take a new measurement until the coast is clear. We will turn by setting the right wheel speed to the negative of the left wheel speed, spinning the robot in place.

Once there is no obstacle in range, we will change the headlights to green and set the speed to both wheels in the same direction so the robot will resume moving forward for a while.

Here is my version of the code:

Random Turning Direction

To make things a little more interesting, we can pick a random direction to turn each time the robot senses something in front of it. We save the random direction in a variable outside the while loop and then use that variable inside to continue turning in the same direction until the obstacle is cleared:

Final program version

Adding Features

For fun you could add some animated pictures on the LED matrix that show the emotional state of the robot - it must be frustrating to run into so many obstacles!

If the robot is blocked from all directions it will continue to spin around until the power goes out. One feature you could add would be to check how long the robot has been turning in the while loop and stop the motors and play a warning sound if it takes too long to find an exit.

Our program has just one distance threshold, but you could have the robot slow down when it sees an obstacle off in the distance and speed up if the way ahead is clear.