Advertisement
earlution

fireflies

May 24th, 2023
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. # A micro:bit Firefly.
  2. # By Nicholas H.Tollervey. Released to the public domain.
  3. # source: https://microbit-micropython.readthedocs.io/en/latest/tutorials/radio.html
  4.  
  5. import radio
  6. import random
  7. from microbit import display, Image, button_a, sleep
  8.  
  9. # Create the "flash" animation frames. Can you work out how it's done?
  10. flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
  11.  
  12. # Event loop.
  13. while True:
  14.     # Button A sends a "flash" message.
  15.     if button_a.was_pressed():
  16.         radio.send('flash')  # a-ha
  17.     # Read any incoming messages.
  18.     incoming = radio.receive()
  19.     if incoming == 'flash':
  20.         # If there's an incoming "flash" message display
  21.         # the firefly flash animation after a random short
  22.         # pause.
  23.         sleep(random.randint(50, 350))
  24.         display.show(flash, delay=100, wait=False)
  25.         # Randomly re-broadcast the flash message after a
  26.         # slight delay.
  27.         if random.randint(0, 9) == 0:
  28.             sleep(500)
  29.             radio.send('flash')  # a-ha
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement