Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A micro:bit Firefly.
- # By Nicholas H.Tollervey. Released to the public domain.
- # source: https://microbit-micropython.readthedocs.io/en/latest/tutorials/radio.html
- import radio
- import random
- from microbit import display, Image, button_a, sleep
- # Create the "flash" animation frames. Can you work out how it's done?
- flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
- # Event loop.
- while True:
- # Button A sends a "flash" message.
- if button_a.was_pressed():
- radio.send('flash') # a-ha
- # Read any incoming messages.
- incoming = radio.receive()
- if incoming == 'flash':
- # If there's an incoming "flash" message display
- # the firefly flash animation after a random short
- # pause.
- sleep(random.randint(50, 350))
- display.show(flash, delay=100, wait=False)
- # Randomly re-broadcast the flash message after a
- # slight delay.
- if random.randint(0, 9) == 0:
- sleep(500)
- radio.send('flash') # a-ha
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement