BBC micro:bit
Button Buddy
Make a little pet that sleeps until you press its buttons, then smiles, frowns or gasps.
Your micro:bit is about to become a pet. Left alone, it sleeps. Press A and it smiles, press B and it is sad, press both and it is amazed. Then it starts counting how many times you have pressed A.
You will learn
- Read the A and B buttons
- Make decisions with
if,elifandelse - Check two things at once with
and - Count with a variable
You will need
- A computer with Chrome or Edge
- A micro:bit and a USB data cable, or the simulator
- A micro:bit V2 for step 5, which uses the touch logo
Words to know
- if
- Runs code only when a condition is true.
- elif
- Short for else if: checked only when everything above it was false.
- else
- Runs when none of the conditions above it were true.
- and
- True only when the conditions on both sides of it are true.
Step by step
Step 1: Smile when A is pressed
Start a new project and call it Button Buddy.
button_a.is_pressed()isTruewhile you hold button A down. Thewhile True:loop checks the button ten times a second, forever. In the simulator, press A on the board with the mouse.1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 sleep(100)ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 sleep(100)Send to micro:bitSaveOpen…SimulatorHold A: a happy face. Step 2: Asleep the rest of the time
Once the face is happy, it stays happy.
elseruns when theifis not true, so when A is not pressed, the pet goes back to sleep.New: lines 6 and 7.
1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 else:7 display.show(Image.ASLEEP)8 sleep(100)ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 else:7 display.show(Image.ASLEEP)8 sleep(100)Send to micro:bitSaveOpen…SimulatorLet go of A, and the pet falls asleep again. Step 3: Sad when B is pressed
elifmeans "else if": it is only checked when theifabove it was false. Add one for button B.New: lines 6 and 7.
1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 elif button_b.is_pressed():7 display.show(Image.SAD)8 else:9 display.show(Image.ASLEEP)10 sleep(100)ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3while True:4 if button_a.is_pressed():5 display.show(Image.HAPPY)6 elif button_b.is_pressed():7 display.show(Image.SAD)8 else:9 display.show(Image.ASLEEP)10 sleep(100)Send to micro:bitSaveOpen…SimulatorHold B: a sad face. Step 4: Amazed by both
andis true only if both sides are true. Put the check for both buttons first. If it came after the check for A on its own, pressing both would match A first, and the pet would just smile.New: lines 4 to 6.
1from microbit import *2 3while True:4 if button_a.is_pressed() and button_b.is_pressed():5 display.show(Image.SURPRISED)6 elif button_a.is_pressed():7 display.show(Image.HAPPY)8 elif button_b.is_pressed():9 display.show(Image.SAD)10 else:11 display.show(Image.ASLEEP)12 sleep(100)ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3while True:4 if button_a.is_pressed() and button_b.is_pressed():5 display.show(Image.SURPRISED)6 elif button_a.is_pressed():7 display.show(Image.HAPPY)8 elif button_b.is_pressed():9 display.show(Image.SAD)10 else:11 display.show(Image.ASLEEP)12 sleep(100)Send to micro:bitSaveOpen…SimulatorHold A and B together: a surprised face. Step 5: Count the hugs
A press of A is a hug, so count them in a variable called
hugs.was_pressed()isTrueonce for each press, however long it is held, which makes it right for counting. Touch the gold logo to see the count. In the simulator, click the gold logo at the top of the board.New: lines 3, 4 and 6 to 10.
1from microbit import *2 3hugs = 04 5while True:6 if button_a.was_pressed():7 hugs = hugs + 18 if pin_logo.is_touched():9 display.scroll(hugs)10 elif button_a.is_pressed() and button_b.is_pressed():11 display.show(Image.SURPRISED)12 elif button_a.is_pressed():13 display.show(Image.HAPPY)14 elif button_b.is_pressed():15 display.show(Image.SAD)16 else:17 display.show(Image.ASLEEP)18 sleep(100)ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3hugs = 04 5while True:6 if button_a.was_pressed():7 hugs = hugs + 18 if pin_logo.is_touched():9 display.scroll(hugs)10 elif button_a.is_pressed() and button_b.is_pressed():11 display.show(Image.SURPRISED)12 elif button_a.is_pressed():13 display.show(Image.HAPPY)Send to micro:bitSaveOpen…SimulatorThree hugs, then touch the logo: it scrolls 3. Watch out The touch logo is only on a micro:bit V2. On an older V1, usebutton_b.was_pressed()to show the count instead, and take B's sad face out.Step 6: Try it on the real thing
Send it to your micro:bit and give it to a friend to play with. Can they work out every face without being told?
ReferenceIdeasAPIProjectButton Buddy1from microbit import *2 3hugs = 04 5while True:6 if button_a.was_pressed():7 hugs = hugs + 18 if pin_logo.is_touched():9 display.scroll(hugs)10 elif button_a.is_pressed() and button_b.is_pressed():11 display.show(Image.SURPRISED)12 elif button_a.is_pressed():13 display.show(Image.HAPPY)Send to micro:bitSaveOpen…SimulatorSend it to your micro:bit and pass it round.
Test it
Tick each one when it works. If one does not, look back at the step it comes from.
Stretch challenges
Finished early? Try these. Each one has a hint if you get stuck.
Sound effects
On a micro:bit V2, give each face a sound.
Show a hint
audio.play(Sound.HAPPY)plays a built in sound. Usewas_pressed()for this, so the sound plays once per press, not ten times a second.Reaction timer
Wait a random time, show a heart, and time how fast you can press A.
Show a hint
running_time()is the number of milliseconds since the micro:bit started. Read it when the heart appears, and again when A is pressed: the difference is your reaction time.Show one answer
1from microbit import *2import random3 4display.show(Image.ASLEEP)5sleep(random.randint(2000, 5000))6display.show(Image.HEART)7start = running_time()8while not button_a.is_pressed():9 sleep(1)10display.scroll(running_time() - start)Mood ring
Each press of A changes to the next face in a list of moods, going back to the first after the last.
Show a hint
Keep the faces in a list and the number of the current one in a variable. Add 1 on each press, and use
% len(moods)to go back to 0 at the end.
The finished code
Show the whole program
1from microbit import *2 3hugs = 04 5while True:6 if button_a.was_pressed():7 hugs = hugs + 18 if pin_logo.is_touched():9 display.scroll(hugs)10 elif button_a.is_pressed() and button_b.is_pressed():11 display.show(Image.SURPRISED)12 elif button_a.is_pressed():13 display.show(Image.HAPPY)14 elif button_b.is_pressed():15 display.show(Image.SAD)16 else:17 display.show(Image.ASLEEP)18 sleep(100)Notes for the teacher
Timing
- 5 minutes: recap the editor and the simulator from day 1.
- 30 minutes: steps 1 to 4. Step 4's order question is worth stopping the class for: ask what happens if the check for both buttons comes last, then try it.
- 15 minutes: step 5, the tests, then sending to the micro:bits.
Mistakes to look out for
eliforelsenot lined up with itsif: an IndentationError, or code that never runs.- Missing brackets:
button_a.is_pressedwithout()is always true, so the pet always smiles. - Counting with
is_pressed()instead ofwas_pressed(): one long press counts several times. - A V1 micro:bit has no touch logo, so step 5 needs the change in its "Watch out" box.
Going further
This is selection: the program picks one path out of several. Draw the if, elif, else chain as a flowchart with the class, and show why its order matters.