micro:bit Python - Let's make it chirp!
Let's make a simple digital pet and make it chirp
Introduction
This is an exercise where we can learn some Python while having a bit of fun. We will start making an animated "bird" and make it chirp using the Display and Audio features of the micro:bit.
Set up the project
For this exercise, we will use micro:bit Python Web Editor. Bring your kids along, code snippets can be dragged and dropped for quick code writing and easy understanding as we move along.
- Connect your micro:bit to your computer using the microUSB cable.
- Connect the micro:bit Python Web Editor to your micro:bit.
- Send the Default Sample Code to your micro:bit to make sure it works.
- If everything works, your micro:bit should show a heart on the display, followed by scrolling text "Hello".
- Have you noticed the micro:bit simulation in the upper right corner of the Web Editor? This is very useful.
Our quiet pet
Let's start making our pet bird animation.
- In the left pane, there are samples that can be dragged and dropped to the code window. Expand the Display container.
- Grab the highlighted
display.show(Image(...))
snippet. Then drag and drop it 2 times. Delete the code that displays a heart and scroll message. The code should look like the picture below, needing some changes. - Let's start making our pet. We make a matrix determining the light strength for each LED on the micro:bit display from 0 to 9.
- The animation is probably way too fast! Let's slow it down a bit, shall we? Adding a pause after each picture
using
sleep(400)
should to the trick.
Now we have created a hungry pet bird opening and closing it's beak and blinking it's eyes. Use the simulation, or send the code to your micro:bit for testing.
Our bird is still quiet. In the next part of the exercise, we'll give it some voice.
Time to chirp!!!
To complete this challenge, we only need one line of code for our bird to chirp. To make it easy for beginners or our kids to follow along, we'll drag and drop the snippet we need.
- In the left pane, expand the Sound container.
- Drag and drop
audio.play(Sound.GIGGLE)
to the bottom of the code inside the while loop. - The finished code should look like this:
- Test using the simulation or sending the code to your micro:bit