Fireflies¶
Uses¶
LED display, radio
Description¶
Fireflies are a sort of bug that uses bioluminescence to signal (without wires) to its friends.
We’re going to use the radio module to create something akin to a swarm of fireflies signalling to each other.
First import radio to make the functions available to your Python program. Then call the radio.on() function to turn the radio on. Since the radio draws power and takes up memory we’ve made it so you decide when it is enabled (there is, of course a radio.off() function).
At this point the radio module is configured to sensible defaults that make it compatible with other platforms that may target the BBC micro:bit. It is possible to control many of the features discussed above (such as channel and addressing) as well as the amount of power used to broadcast messages and the amount of RAM the incoming message queue will take up. The API documentation contains all the information you need to configure the radio to your needs.
Assuming we’re happy with the defaults, the simplest way to send a message is like this:
radio.send("a message")
The example uses the send function to simply broadcast the string “a message”. To receive a message is even easier:
new_message = radio.receive()
As messages are received they are put on a message queue. The receive function returns the oldest message from the queue as a string, making space for a new incoming message. If the message queue fills up, then new incoming messages are ignored.
That’s really all there is to it! (Although the radio module is also powerful enough that you can send any arbitrary type of data, not just strings.
Basic Task¶
Create a flash animation frames. hint: flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
Turn on the radio. This enables the radio feature.
Pressing button A will send a ‘flash’ message to the other microbits
Checks for incoming ‘flash’ message. If there is an incoming ‘flash’ message it will do the ff:
- sleep for a random period of time (50 to 400 ms)
- Display the flash animation. Add delay
- Randomly rebroadcast the flash message after a slight delay