Step 1: Make it
What is it?
Code your own electronic pet and customise it to make it your own. The new micro:bit's built-in speaker makes it even more fun with the new 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
- new micro:bit with sound (or MakeCode simulator)
- MakeCode or Python editor
- battery pack (optional)
Step 2: Code it
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 new 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.