T3W2: Robot Programming in MakeCode

Welcome to the Lowell STEMapalooza elective class, Term 3! For the second class we will start programming our Cutebot robots using MakeCode.

Cutebot Robot

The Cutebot Robot is a small two-wheeled robot that is designed to be controlled by a micro:bit board and has an extension pack that allows us to program it in MakeCode along with the other micro:bit functions.

Here is a top view of the robot:

From top to bottom, the robot has an ultrasonic distance sensor (that looks like two eyes), two small blue LEDs that show the status of the optical sensors, two RGB LED headlights, a socket for the micro:bit board, two motors with tires, an infrared sensor (under the battery), and an on-off switch, and some expansion pins. The bottom of the board has a free-rolling caster ball for balance, two optical sensors for detecting lines, two RGB ground-effect lights.

We will be programming our Cutebot robots with MakeCode using the extension they have provided. To load the extension, open the [Advanced] menu and select [Extensions] down at the bottom. Look for a picture of the cutebot and click on it to load the extension into MakeCode. This will add two new menu options, "CuteBot" and "Neopixel", with commands for controlling the robot:

One disadvantage of the extension is that statements won't run on the MakeCode simulator - we are going to have to get things working the old fashioned way, by trial and error! To make more space on the screen you can click the "<" tab to hide the simulator.

First Program - Forwards and Backwards

For our first program we will have the robot move forwards for a while and then back up the same amount. This sounds pretty simple but we will also put in some safety and status features that will help us now and in future projects.

MakeCode programs start running as soon as they are downloaded into the micro:bit board. This can be kind of dangerous when that board is hooked up to a robot on wheels! So, we can set up everything our program needs in the "on start" structure but we will wait for a button press before doing anything that makes the robot move. This can be done by putting the motion code in an "on button A pressed" structure instead of the "on start" block.

In the "on start" block, add a command from the CuteBot menu: [CuteBot]->Set LED headlights ALL color (Red). This will turn the headlights red (or whatever color you would like) when the code starts running, so we know the program has finished loading.

Then from [Input] add an "on button A pressed" structure. Add some motion commands inside it:

  1. [CuteBot]->Go [Forward] at speed (30)% for (2) seconds

  2. [Basic]->Pause 1000 ms

  3. [CuteBot]->Go [Backward] at speed (30)% for (2) seconds

Then at the end, add a command to change the headlight color so we know the program is done: [CuteBot]->Set LED headlights [All] color (Blue). At the beginning of the "on button A pressed" block, add another [CuteBot]->Set LED headlights [All] color (Green) so we know when the motion part is running.

When you are done the program should look like this:

Testing the Program

To test the program, plug the USB cable into the micro:bit board and make sure the board shows up as a drive in your file browser. Then click the purple "Download" button to save the .hex program on your Chromebook, then drag and drop it to the MICROBIT drive to upload the program. The headlights should turn red when the program is ready.

For the first try, hold the Cutebot in the air when you press the A button to start the motion. You can do this with the USB cable still attached to save time in case changes are needed. Look at the wheels and make sure they are turning how you would expect, and see when the headlights turn blue at the end.

When everything looks good, unplug your Cutebot and turn the power switch on - it's now on battery power. Set it on the floor and press button A to have it start moving. You can press the button multiple times to see the program work.

Adding Features

Adding variables

If you want to experiment with the motion or possibly add more segments, you will have to edit the movement speed and duration settings in every motion command to try a new setting. To save time and possible errors, use a Variable to hold the speed and time values, that way you only need to change it in one place and you know the values will be updated everywhere.

From the MakeCode [Variable] menu, click on "Make a Variable". Pick a name that says what the variable is, like SPEED (use all capital letters to show that it is a program constant rather than something that the program will update). Add a "set SPEED to (30)" block to the "on start" structure to set the speed value you want to use. Grab a (SPEED) variable from the [Variables] menu and insert it into the movement commands instead of the number 30.

Repeat this for a variable called RUNTIME and replace the number 2 in the motion commands. Now it is easy to experiment with different speeds and durations!

Adding Countdown function

It is not always safe to have the robot possibly start moving as soon as the button is pressed, with our finger still on the robot board. So we will have a "Countdown" process to give us time to put the robot down and clear out of the way before it starts. We can do this by creating a MakeCode Function. This allows us to run the countdown function from anywhere in the main program just by inserting a single block to call the function - you don't have to copy and paste a bunch of code each place you need it, and there is only one place the program code needs to be edited if you want to change it.

From the [Function] menu select "Make a Function.." and call the new function "Countdown". You will get an empty blue function structure. Inside the function structure, put code to change the headlight color to (orange), and wait five seconds. Then from the [Functions] menu, insert the "call Countdown" block into the beginning of your "on button A pressed" structure. This will call the countdown function each time the button is pressed.

You can make the countdown function more elaborate for fun - add a beep from the [Music] menu, or have it display the numbers 5..4..3..2..1 on the micro:bit LED matrix as it is counting down.

My version of the full program with variables and a coundown function looks like this: