BBC micro:bit Bird Activity GitHub

Advanced steps

Glad to see you are still here!

These are some additional exercises you can do on top of the previous activity steps.

Expanding the moods

In main.py we’ve checked the bird mood using the bird.current_mood() function:

while True:
    bird_mood = bird.current_mood()
    if bird_mood == "chill":
        display.show(Image.HAPPY)

How does that work? Let’s look at the bird.py file, and find the current_mood() function (it’s around line 150):

def current_mood():
    # Check for motion
    if __mb.accelerometer.was_gesture('shake'):
        return 'angry'

    __mb.sleep(10)   # Ensure run_every/gestures have a chance to run
    return "chill"

Exercise:
Birds can be noisy, but they don't like loud sounds themselves! The micro:bit has a microphone, and we can use it to detect loud noises, so let's create a new mood: "startled".
Let's add another if statement inside the current_mood() function inside the bird.py file. So, if there was a "loud" event, the function can return the `startled` mood.
You can find more info about the microphone in the Microphone documentation.

Solution

Now make a noise, like a clap, near the microphone and watch your bird get startled!

Hint: If the conference area is too loud, you can blow on the microphone or change the loud threshold with a microphone method.

Sending a Hello radio message

Our birds are reacting to events in their environment like another bird saying hello, or seeing a cat.

How does this work?

The micro:bit radio is used to broadcast and receive these messages.

Let’s look at the warn_about_cat() function towards the bottom of the bird.py file (around line 180):

def warn_about_cat():
    # Impose a delay before warning others
    __mb.sleep(__rand.randint(500,1500))
    __radio.send("cat->all")

Here we can see two thing:

  1. A delay for a random amount of time between 500 milliseconds to 1.5 seconds.
    • This is done so that there is a bit of delay between each bird warning each other
  2. Sending the cat->all radio message

Exercise:
Let's create a new function in bird.py that says "hello" to all the birds nearby. This will be very similar to the warn_about_cat() function.

Now, in the while True: loop, let's call the new bird function when button_a is pressed, and you will be able to see the other birds react to your "Hello" message each time the button A is pressed.

Hint: You might need to add a small delay after saying hello, or you can end up sending a lot of hello messages around on a single button press.

Solution

Say hello to a specific neighbour

The format of these radio messages is very simple: event->target

  • event is the name of the event
  • target is the “friendly name” of the micro:bit (unique to each board), or “all” to send to everyone

So you can send a “hello” message to a specific bird, but how do you find out the “friendly name” of a micro:bit?

Exercise:
The friendly name can be obtained using the `bird.friendly_name()` function. Let's update the code to scroll the friendly name on the display (display.scroll()) when the B button is pressed.

For the next part you can find a friend on your table to do this together, or ask one of the micro:bit helpers to get a “neighbour micro:bit”.

Exercise:
Can you update your "Say hello to all" function in the bird.py file, from the previous exercise, to take a "friendly name" as and argument and send the message only to that bird?
Then, let's update the code that runs in the infinite loop in the main.py file, so that when button A is pressed it says hello only to your friend!

Solution