T3W6: Radio Control
This is our last week on the Cutebots, next week we will start our LED clock building project!
The micro:bit boards we have been programming to control our robots have built-in radio transmitters and receivers. This will allow us to use an extra micro:bit board as a remote control for our robot.
Remote Control Transmitter
There are two different programs this week - one for the transmitter board, and one for the receiver robot. For our transmitter micro:bit board, pressing buttons on the board will cause our program to transmit a numeric code to the receiver on the robot. Pressing the A button on the left side of the transmitter board will send a "1" code for LEFT. Pressing B sends a "2" code for RIGHT. And pressing A+B together sends a "3" code for FORWARD. If no buttons are pressed the transmitter will send a "0" code for STOP.
To get started, visit the MakeCode for Micro:bit website and open a new project. Call your new program "Remote Controller" or whatever makes sense to you.
The micro:bit radios have different logical channels to prevent interference between different boards. From the [Radio] menu, put a "radio set group ( )" command in the "on start" event structure. For the group number, pick a "1" or the number of your robot - it just needs to match between the transmitter and receiver.
Then in the "forever" loop, put an if/then/else structure to check the status of the push buttons. First check for "A+B" using the <button (A+B) is pressed> Boolean function from the [Input] menu. If pressed, execute a "radio send number (3)" statement from the [Radio] menu to send a FORWARD command. Then add cases for <button (A) is pressed> to send 1 for LEFT, and <button (B) is pressed> to send a 2 for RIGHT, and then send a 0 in the final else case for STOP. Here is the transmitter program:
Now in each if/then/else case, add a "show LEDs" block from the [Basic] menu and draw an arrow or other symbol that matches the command being sent to provide some visual feedback for the person running the transmitter.
Download the program to an extra micro:bit board that can serve as the transmitter and test out pressing the buttons and seeing the LEDs update. Save the program on your Chromebook using the floppy disk icon so you can come back to it later.
Receiver Robot Code
Now return to the MakeCode home page, create a new project called "Robot Receiver", and load the Cutebot extensions from the [Advanced]->[Extensions] menu.
In the "on start" event structure, add a [Radio]->radio set group command with a matching group number. Also create some program constants named FULL_SPEED = 50 and TURN_SPEED = 20 which we will use later. Finally, set the LED headlights to purple or whatever color you like so we can tell when the program starts running.
Then we can delete the "forever" structre and add a "on radio received (receivedNumber)" structure from the [Radio] menu. The code in this structure will be executed whenever the micro:bit board radio receives a transmission from the remote control, and the value of the "receivedNumber" variable will be set to the number received by the radio.
Insert an if/then/else structure from [Logic] and check for different values of (receivedNumber). If <receivedNumber = 0>, run a "Stop car immediately" command. If <receivedNumber = 1> for LEFT, add a "Set left wheel speed (TURN_SPEED)% right wheel speed (FULL_SPEED)%" command to steer towards the left. If <receivedNumber = 2>, add a copy of the wheel speed command with the speeds reversed to turn RIGHT. Finally, if <receivedNumber=3>, go straight ahead with both wheels at (FULL_SPEED).
Now for each of the different (receivedNumber) values, add a "Set LED headlights" command to show what the robot is doing. Make both headlights green for FORWARD mode, red for STOP, and make one headlight orange for LEFT and RIGHT to simulate a turn signal on a car.
Just in case the transmission gets scrambled, in the last "else" case if the big if/then/else structure, set the headlights to some other color like blue so you can see that there was a problem.
Here is my example code to follow:
Download your code to the micro:bit board in your robot, and push some buttons on the transmitter to see if the robot receives the commands.
Adding Features - Reverse
In an environment with lots of obstacles like chairs and tables, the little Cutebot robot can get stuck against something pretty easily. Let's add a REVERSE mode to get us out of jams. Although it seems like we are out of buttons, the gold oval logo on the micro:bit board above the LED matrix can act as a touch-sensitive button. In the remote controller code, add a case for <logo is pressed> from the [Input] menu to transmit a -1 value. In the receiver robot code, add a case for (receivedNumber = -1) and set the wheel speeds to (FULL_SPEED x -1) to make the robot reverse direction and back up.
Here are the programs including the reverse mode code: