Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active August 21, 2018 08:08
Show Gist options
  • Save topshed/94a6b15b056ef7ea24032434cb48f0fd to your computer and use it in GitHub Desktop.
Save topshed/94a6b15b056ef7ea24032434cb48f0fd to your computer and use it in GitHub Desktop.
Draft activity for CoderDojo starter pack (Micro:Bit Python)

Making a Crowd Display with Micro:Bits and Python

Program a set of Micro:Bits so that they talk to each other via radio to create a synchonised light show.

Design your image

  1. Connect the Micro:Bit to your computer using the USB cable.

  2. Open the Mu editor.

  3. 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
  4. Create an image to be shown on LEDs:

    pic = Image("90009:"
            "07070:"
            "00500:"
            "07070:"
            "90009")
    display.show(pic, delay=100, wait=False)
  5. 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?

alt text

Setup the radio

  1. Add some code to turn on the radio.

    radio.on()
  2. 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')
  3. 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)
  4. 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?

  5. Can you add sleep and display.clear() code so that the image disappears after a short delay?

    alt text alt text

Triggering an avalanche of messages

  1. 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.

  2. 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') 
  3. 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.

  4. 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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment