To complete this tutorial you will need another Micro:Bit. In this tutorial we will program another Micro:Bit to control Bit:bot using the radio service.
Controller
To start we will write the program for the remote controller. The controller will communicate with the robot using the radio service. This utilises the Bluetooth transceiver on the Micro:Bit however it does not use the Bluetooth protocol instead it uses a Nordic propriety protocol.
The controller will send characters to the robot. The controls are as follows;
- When Button A is pressed, the letter L will be sent to the robot indicating that the robot should turn left.
- When Button B is pressed, the letter R will be sent to the robot indicating that the robot should turn right.
- When Button A and button B is pressed, the letter F will be sent to the robot indicating that the robot should go forwards.
- When no buttons are pressed, the letter S will be sent indicating that the robot should stop.
[pastacode lang=”python” manual=”%23%20Controller%20for%20the%204tronix%20Bit%3ABot%20and%20BBC%20Micro%3ABit%0A%23%20Author%20David%20Bradshaw%202018%0A%0Afrom%20microbit%20import%20*%0Aimport%20radio%0A%0Aradio.on()%0Aradio.config(channel%3D33)%0Aradio.config(power%3D7)%0A%0Awhile%20True%3A%0A%20%20%20%20txMsg%20%3D%20%22%22%0A%20%20%20%20if%20button_a.is_pressed()%20and%20button_b.is_pressed()%3A%0A%20%20%20%20%20%20%20%20txMsg%20%3D%20%22F%22%0A%20%20%20%20%20%20%20%20display.show(Image.ARROW_N%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20elif%20button_a.is_pressed()%3A%0A%20%20%20%20%20%20%20%20txMsg%20%3D%20%22L%22%0A%20%20%20%20%20%20%20%20display.show(Image.ARROW_W%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20elif%20button_b.is_pressed()%3A%0A%20%20%20%20%20%20%20%20txMsg%20%3D%20%22R%22%0A%20%20%20%20%20%20%20%20display.show(Image.ARROW_E%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20txMsg%20%3D%20%22S%22%0A%20%20%20%20%20%20%20%20display.show(Image.STICKFIGURE%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20radio.send(txMsg)%0A%20sleep(10)” message=”Remote Controller Code” highlight=”” provider=”manual”/]
I have used a sleep command at the end of the code, this is needed if this is taken out the receiver will receive corrupt data and an error will be displayed. When editing the code keep the delay in or you will have issues.
Robot
Next we will change the program loop to interpret the commands. The code for the robot can be seen below;
[pastacode lang=”python” manual=”%23%20Controlled%20robot%20for%20the%204tronix%20Bit%3ABot%20and%20BBC%20Micro%3ABit%0A%23%20Author%20David%20Bradshaw%202018%0A%0Afrom%20microbit%20import%20*%0Aimport%20radio%0A%0Aradio.on()%0Aradio.config(channel%3D33)%0Aradio.config(power%3D7)%0A%0ArobotType%20%3D%20%22%22%0A%0Adef%20detectModel()%3A%20%20%23%20Detects%20which%20model%20were%20using%20XL%20or%20classic%0A%20%20%20%20global%20robotType%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20value%20%3D%20i2c.read(28%2C%201%2C%20repeat%3DFalse)%20%20%23%20Read%20i2c%20bus%0A%20%20%20%20%20%20%20%20robotType%20%3D%20%22XL%22%20%20%23%20If%20we%20can%20read%20it%20then%20it%20must%20be%20XL%0A%20%20%20%20%20%20%20%20display.show(%22X%22)%0A%20%20%20%20except%3A%0A%20%20%20%20%20%20%20%20robotType%20%3D%20%22classic%22%20%20%23%20If%20we%20can’t%20read%20it%20it%20must%20be%20classic%0A%20%20%20%20%20%20%20%20display.show(%22C%22)%20%20%20%20%20%20%23%20or%20Micro%3Abit%20is%20unplugged%0A%20%20%20%20sleep(1000)%20%20%23%20Do%20this%20so%20the%20user%20can%20see%20if%20the%20correct%20model%20is%20found%0A%0AdetectModel()%0A%0A%23%20Motor%20pins%3B%20these%20tell%20the%20motor%20to%20go%0A%23%20forward%2C%20backwards%20or%20turn%0Aif%20robotType%20%3D%3D%20%22classic%22%3A%0A%20%20%20%20leftSpeed%20%3D%20pin0%0A%20%20%20%20leftDirection%20%3D%20pin8%0A%20%20%20%20rightSpeed%20%3D%20pin1%0A%20%20%20%20rightDirection%20%3D%20pin12%0A%0Aelse%3A%20%20%23%20Bit%3ABot%20XL%0A%20%20%20%20leftSpeed%20%3D%20pin16%0A%20%20%20%20leftDirection%20%3D%20pin8%0A%20%20%20%20rightSpeed%20%3D%20pin14%0A%20%20%20%20rightDirection%20%3D%20pin12%0A%0A%0A%23%20Motor%20control%20to%20tell%20the%20motor%20what%20direction%20and%20speed%20to%20move%0Adef%20move(_leftSpeed%2C%20_rightSpeed%2C%20_leftDirection%2C%20_rightDirection)%3A%0A%20%20%20%20%23%20speed%20values%20between%201%20-%201023%0A%20%20%20%20%23%20smaller%20values%20%3D%3D%20faster%20speed%20moving%20backwards%0A%20%20%20%20%23%20Smaller%20values%20%3D%3D%20lower%20speeds%20when%20moving%20forwards%0A%20%20%20%20%23%20direction%200%20%3D%3D%20forwards%2C%201%20%3D%3D%20backwards%0A%20%20%20%20leftSpeed.write_analog(_leftSpeed)%20%20%23%20Set%20the%20speed%20of%20left%20motor%0A%20%20%20%20rightSpeed.write_analog(_rightSpeed)%20%20%23%20Set%20the%20speed%20of%20right%20motor%0A%20%20%20%20if%20(_leftDirection%20!%3D%202)%3A%0A%20%20%20%20%20%20%20%20leftDirection.write_digital(_leftDirection)%20%20%23%20left%20motor%0A%20%20%20%20%20%20%20%20rightDirection.write_digital(_rightDirection)%20%20%23%20right%20motor%0A%0A%0Adef%20drive(speed)%3A%0A%20%20%20%20if%20(speed%20%3E%200)%3A%0A%20%20%20%20%20%20%20%20move(speed%2C%20speed%2C%200%2C%200)%20%20%23%20move%20the%20motors%20forwards%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20speed%20%3D%201023%20%2B%20speed%0A%20%20%20%20%20%20%20%20move(speed%2C%20speed%2C%201%2C%201)%20%20%23%20move%20the%20motors%20backwards%0A%0A%0Adef%20sharpRight()%3A%0A%20%20%20%20move(100%2C%201023%20%2B%20-200%2C%200%2C%201)%0A%0A%0Adef%20sharpLeft()%3A%0A%20%20%20%20move(1023%20%2B%20-200%2C%20100%2C%201%2C%200)%0A%0A%0Adef%20gentleRight()%3A%0A%20%20%20%20move(200%2C%200%2C%200%2C%200)%0A%0A%0Adef%20gentleLeft()%3A%0A%20%20%20%20move(0%2C%20200%2C%200%2C%200)%0A%0A%0Adef%20coast()%3A%0A%20%20%20%20move(0%2C%200%2C%202%2C%202)%0A%0A%0Adef%20stop()%3A%0A%20%20%20%20move(0%2C%200%2C%200%2C%200)%0A%0A%0Awhile%20True%3A%0A%20%20%20%20incoming%20%3D%20radio.receive()%0A%20%20%20%20if%20incoming%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20if%20incoming%20%3D%3D%20%22F%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20drive(1023)%0A%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image.ARROW_N%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20%20%20%20%20elif%20incoming%20%3D%3D%20%22L%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sharpLeft()%0A%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image.ARROW_W%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20%20%20%20%20elif%20incoming%20%3D%3D%20%22R%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sharpRight()%0A%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image.ARROW_E%2C%20loop%3DFalse%2C%20delay%3D10)%0A%20%20%20%20%20%20%20%20elif%20incoming%20%3D%3D%20%22S%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20stop()%0A%20%20%20%20%20%20%20%20%20%20%20%20display.show(Image.SKULL%2C%20loop%3DFalse%2C%20delay%3D10)%0A” message=”Robot Code” highlight=”” provider=”manual”/]
Now upload the Remote code to one Micro:Bit and upload the Robot code to the Micro:Bit that is plugged into Bit:Bot. If you don’t have a battery pack for you remote Micro:Bit you can plug it into you computer via the USB port to power it. Have a play and see how the code works the change the code and add more features. Perhaps you could use the accelerometer or compass to add features.
Code Files
Click the following link to download the code file; RCAUTO