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.
CAUTION: For some reason the radio library is not available on the online MicroPython editor. All coding for this tutorial is written and uploaded in Mu. If you try to use the online editor you will get an error and the code will not work; because of this we can not use the NeoPixels for this tutorial. I mentioned earlier that you get errors when using Mu and the PWM pins to control the motors so I suggested that you should always use the online editor when utilising the motors. This error only occurs when using the NeoPixels and PWM pins, I’m not sure why this is pin 13 on the Micro:bit seems to cause a few problems at the moment with this being one of them. Use Mu for this tutorial and do not change the code to utilise the NeoPixels, if you do the code will not work properly.
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.
# Controller for the 4tronix Bit:Bot and BBC Micro:Bit # Author David Bradshaw 2018 from microbit import * import radio radio.on() radio.config(channel=33) radio.config(power=7) while True: txMsg = "" if button_a.is_pressed() and button_b.is_pressed(): txMsg = "F" display.show(Image.ARROW_N, loop=False, delay=10) elif button_a.is_pressed(): txMsg = "L" display.show(Image.ARROW_W, loop=False, delay=10) elif button_b.is_pressed(): txMsg = "R" display.show(Image.ARROW_E, loop=False, delay=10) else: txMsg = "S" display.show(Image.STICKFIGURE, loop=False, delay=10) radio.send(txMsg) sleep(10)
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.
Next we will change the program loop to interpret the commands. The code for the robot can be seen below;
# Controlled robot for the 4tronix Bit:Bot and BBC Micro:Bit # Author David Bradshaw 2018 from microbit import * import radio radio.on() radio.config(channel=33) radio.config(power=7) leftSpeed = pin0 leftDirection = pin8 rightSpeed = pin1 rightDirection = pin12 # Motor control to tell the motor what direction and speed to move def move(_leftSpeed, _rightSpeed, _leftDirection, _rightDirection): # speed values between 1 - 1023 # smaller values == faster speed moving backwards # Smaller values == lower speeds when moving forwards # direction 0 == forwards, 1 == backwards leftSpeed.write_analog(_leftSpeed) # Set the speed of left motor rightSpeed.write_analog(_rightSpeed) # Set the speed of right motor if (_leftDirection != 2): leftDirection.write_digital(_leftDirection) # left motor rightDirection.write_digital(_rightDirection) # right motor def drive(speed): if (speed > 0): move(speed, speed, 0, 0) # move the motors forwards else: speed = 1023 + speed move(speed, speed, 1, 1) # move the motors backwards def sharpRight(): move(100, 1023 + -200, 0, 1) def sharpLeft(): move(1023 + -200, 100, 1, 0) def gentleRight(): move(200, 0, 0, 0) def gentleLeft(): move(0, 200, 0, 0) def coast(): move(0, 0, 2, 2) def stop(): move(0, 0, 0, 0) while True: incoming = radio.receive() if incoming is not None: if incoming == "F": drive(1023) display.show(Image.ARROW_N, loop=False, delay=10) elif incoming == "L": gentleLeft() display.show(Image.ARROW_W, loop=False, delay=10) elif incoming == "R": gentleRight() display.show(Image.ARROW_E, loop=False, delay=10) elif incoming == "S": stop() display.show(Image.SKULL, loop=False, delay=10)
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.
Click the following link to download the code file; RC.zip