Wireless Remote Control with micro:bit
Experiment 3: Wirelessly Controlling the micro:bot
Now that we are able to wirelessly move our robot forward, we will want additional commands for turning and driving in reverse. While we are at it, we are going to give a special function to control the servos attached to our battle bot.
You will need the following parts:
- 2x micro:bit Boards
- 2x Micro-B USB Cables
- 1x Assembled micro:bot Kit
- 1x gamer:bit Carrier Board
- 6x AA Batteries
For a few items listed, you will need more than one unit (i.e. micro:bits, AA batteries, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
3.1 Full Remote Control
For this part of the experiment, we will repeat the steps in 2.1 to transmit commands for turning or driving in reverse. The built-in accelerometer will be used to detect a shake. A command to control the servo motors will be sent out as soon as there is a shake detected.
If you have not already, insert one micro:bit into the gamer:bit's carrier board. Make sure to insert the micro:bit with the LED array facing up (the same direction as all of the buttons) as explained in the micro:arcade kit experiment guide. We’re continuing on from experiment 3. Make sure to check out the guide before continuing on.
Disconnect the battery pack from micro:bit from the previous experiment. The JST connector is a tight fit in the micro:bit so you will need to carefully remove the connector out by wiggling it side to side using your index finger and thumb. It is easier to remove the micro:bit from the gamer:bit carrier board.
Then connect the first micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment. Remember, if you are building from scratch, you will need to install the appropriate extension when using the gamer:bit.
Code to Note
Using the same template for driving forward, we assign a unique number to the right (P2), down (P8), and left (P1) buttons on the direction pad. For simplicity, we will just want the robot to move forward when turning right or left. There is not as much animation with the LED array in this example since it can slow down your micro:bit. Using the Input's
on shake block from the
Input, a unique number is sent out to control the servos. Again, an arrow will display briefly for feedback in each case.
3.2 Full Battle Bot Control
For this part of the experiment, we will repeat the steps in 2.2 to receive the commands before moving the robot. Instead of just driving forward, the robot can now turn, reverse, and control the servo motors.
We'll be using the same servo connection on the battle bot as explained in experiment 5. Make sure to check out the guide before continuing on.
Remove the battery pack from the moto:bit carrier board from the previous experiment.
To avoid confusion when uploading code, unplug the first micro:bit from your computer. Then connect the second micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example and build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment. Remember, if you are building from scratch, you will need to install the appropriate extension when using the moto:bit.
Code to Note
We set up the second micro:bit to use the same channel and motor logic as explained in experiment 2.2. Four more commands were added to the
on radio received __________ to turn right, reverse, turn left, and control the servo motors of our battle bot. When turning, the motor closest to the inside of the turn is set to a lower value. That way the robot will continue to drive forward but also move right or left. To reverse, both motors used the
reverse option to drive backwards. Finally, if the robot receives a command indicating that there was a shake from the first micro:bit, the servos will move. If you look closely at the
receivedNumber, each of the commands match the same number that was transmitted from the first micro:bit.
What You Should See
Now that both micro:bits are programmed with more commands, let's test the code out. Disconnect the USB cable from both micro:bits and power each with the respective battery packs again.
Pressing the gamer:bit's drive button (P16) and any of the buttons located in the direction pad (P0, P2, P8, P1) will cause the robot to move. Shaking the first micro:bit will make the servos on the micro:bot move.
Powering Down Your Controller and Robot
When finished, make sure to power down your controller and micro:bot by disconnecting the JST connector and barrel jack on the battery packs. This will save you some power in the long run for more fun! Again, the JST connector is a tight fit in the micro:bit so you will need to carefully remove the connector out by wiggling it side to side using your index finger and thumb.