Deutsch English |
Two micro:bits can communicate with each other via Bluetooth. In a typical application a mbRobot can be remote controlled with a second micro:bit. The necessary functions are imported from the module radio (only realmode).
In this simple but tricky communication model each micro:bit (also called node) is a transmitter and a receiver at the same time. First both nodes create a wireless Bluetooth connection by calling radio.on(). With radio.send(msg), the transmitter sends a message (as a string) to the other node's receiver memory. The receiver retrieves the oldest message from the memory with the radio.receive() command.
In the following examples, the micro:bit, which is not built into the mbRobot, acts as transmitter and the mbRobot as receiver.
Example 1: The red LEDs of the robot are switched on and off remotely.
The two devices create the Bluetooth connection by calling radio.on(). Clicking the left button executes the radio.send("on") command and sends the message "on", clicking the right button sends the message "off". The robot receives the transmitted string with radio.receive() and switches both LEDs on or off.
Transmitter (micro:bit): | Receiver (mbRobot) | |
#Mr5a.py from microbit import * import radio radio.on() while True: if button_a.was_pressed(): radio.send("on") elif button_b.was_pressed(): radio.send("off") |
# Mr5b.py (receiver) import radio from microbit import * from mbrobot import * radio.on() while not button_a.was_pressed(): rec = radio.receive() if rec != None: if rec == "on": ledLeft.write_digital(1) ledRight.write_digital(1) elif rec == "off": ledLeft.write_digital(0) ledRight.write_digital(0) stop() |
Explanations of the program code:
import radio: Imports the module radio that contains the commands for the Bluetooth communication | |
radio.on(): Activates the communication channel | |
radio.send("on") Sends the string "on" | |
rec = radio.receive() Fetches the oldest message from the receiver memory (returns None if the memory is empty) |
• | No button pressed →stopped (state = "STOP") |
• | Left button pressed →move left (state = "LEFT") |
• | Right button pressed →move right (state = "RIGHT") |
• | both buttons pressed →move straight ahead (state = "FORWARD") |
It is advantageous to introduce a state variable for such tasks, where the current state is stored. In particular, a message is only sent if the state has changed.
Transmitter (micro:bit): | Receiver (Roboter) | |
#Mr5c.py from microbit import * import radio radio.on() state = "STOP" oldState = "" while True: if button_a.is_pressed() and button_b.is_pressed(): state = "FORWARD" display.show('F') elif button_a.is_pressed(): state = "LEFT" display.show('L') elif button_b.is_pressed(): state = "RIGHT" display.show('R') else: state = "STOP" display.show('S') if oldState != state: radio.send(state) oldState = state |
# Mr5d.html (receiver) import radio from mbrobot import * radio.on() setSpeed(15) while True: rec = radio.receive() if rec == "FORWARD": forward() elif rec == "LEFT": leftArc(0.1) elif rec == "RIGHT": rightArc(0.1) elif rec == "STOP": stop() delay(100) |
Explanations of the program code:
display.show("F") The letter "F" is shown on the LED display | |
radio.send(state) Depending on which buttons were pressed, FORWARD, LEFT, RIGHT or STOP are sent | |
if oldState != state: A message is only sent if the state has changed |
Exercises: |
1) |
|
2) |
Mark 4 locations according to the picture and write a program so that the robot moves remotely to each location and then back to the starting location. To do so, it must also be able to move backwards. |