Program a set of Micro:Bits so that they talk to each other via radio to create a synchonised light show.
-
Connect the Micro:Bit to your computer using the USB cable.
-
Open the Mu editor.
-
Add the following code to import the Micro:Bit libraries you'll need.
import radio import random from microbit import display, Image, button_a, sleep
-
Create an image to be shown on LEDs:
pic = Image("90009:" "07070:" "00500:" "07070:" "90009") display.show(pic, delay=100, wait=False)
-
Click the Flash button at the top of the Mu window. Your image should appear on the LEDs. Can you edit your code to display a different image?
-
Add some code to turn on the radio.
radio.on()
-
Now add some code that will trigger when Button A is pressed and send a radio message. Enter some text to send as the message ("now" is used as an example here).
while True: if button_a.was_pressed(): radio.send('now')
-
Move your ```display_show`` line so that it is only triggered when the same radio message is received.
message = radio.receive() if message == 'now': display.show(pic, delay=100, wait=False)
-
Find someone else who is at the same point and test your code? Does pressing the A button cause their image to appear on their Micro:Bit?
-
Can you add
sleep
anddisplay.clear()
code so that the image disappears after a short delay?
-
The Micro:Bit should sometimes re-broadcast the message after it has received it. Use
random.randint
to choose a number and only transmit the message again if it matches a certain value. -
Use
random.randint
again to include a slight pause of up to 5 seconds before re-transmission.while True: if button_a.was_pressed(): radio.send('now') message = radio.receive() if message == 'now': display.show(pic, delay=100, wait=False) sleep(300) display.clear() if random.randint(0, 9) == 0: sleep(random.randint(0,5000)) radio.send('now')
-
Test your code as group. depending on how many Micro:Bits are taking part, you should adjust the random values so that avalanches occur with a frequency you're happy with.
-
Work as a group to extend the project.
- Modify the code that so pressing button B has a different effect.
- Can you coordinate your Micro:Bits so that pressing a button on one causes a text message to be displayed on the Micro:Bits one letter at a time?