Step 1: Make it
What is it?
Code your own electronic pet and customise it to make it your own. The micro:bit's built-in speaker makes it even more fun with its expressive sounds.
What you'll learn
- How to use variables and loops to make a simple timer.
- How to use selection to make different events happen at different times.
- A trick to make it appear as if a micro:bit program has stopped running!
How it works
- Electronic pets were invented in Japan in the 1990s and soon became a popular toy all over the world.
- They’re small gadgets that you can keep on your keyring. A creature is shown on a simple display. You have to look after it, interacting with it by pressing buttons and playing simple games. If you ignore it and don’t feed or clean it, it will get sad, misbehave or become unwell.
- The code uses a variable called timer to keep track of how long your pet has been ignored. A forever loop adds 1 to the timer every 1 second (1000 milliseconds).
- If timer reaches 20, your pet shows a sad face on the LED display and makes a sad sound on the built-in speaker outputs.
- If it reaches 30, your pet falls asleep.
- If it gets to 40 your pet plays a mysterious sound and dies. The code uses the 'set built-in speaker off' block to stop any other sounds coming out of the speaker. A 'while true' loop makes sure only the skull icon is shown on the LED display.
- Your pet will stay alive and happy if you interact with it, however! Stroke the logo to make it happy, or shake it to make it giggle. This resets the timer back to 0.
- If your pet dies you can bring it back to life by pressing the reset button on the back of your micro:bit.
What you need
- V2 micro:bit with sound (or MakeCode simulator)
- MakeCode or Python editor
- battery pack (optional)
Step 2: Code it
1from microbit import * 2import audio 3 4timer = 0 5display.show(Image( 6 "00000:" 7 "09090:" 8 "00000:" 9 "09990:" 10 "00000")) 11audio.play(Sound.HELLO) 12 13while True: 14 if pin_logo.is_touched(): 15 timer = 0 16 display.show(Image.HAPPY) 17 audio.play(Sound.HAPPY) 18 elif accelerometer.was_gesture('shake'): 19 timer = 0 20 display.show(Image.SURPRISED) 21 audio.play(Sound.GIGGLE) 22 else: 23 sleep(500) 24 timer += 0.5 25 # sleep for half a second only to make it react more quickly to logo touch & shake 26 27 if timer == 20: 28 display.show(Image.SAD) 29 audio.play(Sound.SAD) 30 elif timer == 30: 31 display.show(Image.ASLEEP) 32 audio.play(Sound.YAWN) 33 elif timer == 40: 34 display.show(Image.SKULL) 35 audio.play(Sound.MYSTERIOUS) 36 break 37
Step 3: Improve it
- Experiment with different times for each stage of its life.
- Create your own facial expressions using the micro:bit's LED display.
- Add more variables to track how hungry or dirty your pet is, and add new interactions to feed or clean your pet, for example by pressing buttons or making a loud sound picked up by the micro:bit's built-in microphone.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.