BBC micro:bit

Button Buddy

Make a little pet that sleeps until you press its buttons, then smiles, frowns or gasps.

Day 2 45 to 60 minutes 6 steps Key Stage 3
Start step 1
Button Buddy, happy because A is pressed.

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, elif and else
  • 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

  1. Step 1: Smile when A is pressed

    Start a new project and call it Button Buddy. button_a.is_pressed() is True while you hold button A down. The while True: loop checks the button ten times a second, forever. In the simulator, press A on the board with the mouse.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.is_pressed():        display.show(Image.HAPPY)    sleep(100)
    Hold A: a happy face.
  2. Step 2: Asleep the rest of the time

    Once the face is happy, it stays happy. else runs when the if is not true, so when A is not pressed, the pet goes back to sleep.

    New: lines 6 and 7.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.is_pressed():        display.show(Image.HAPPY)    else:        display.show(Image.ASLEEP)    sleep(100)
    Let go of A, and the pet falls asleep again.
  3. Step 3: Sad when B is pressed

    elif means "else if": it is only checked when the if above it was false. Add one for button B.

    New: lines 6 and 7.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.is_pressed():        display.show(Image.HAPPY)    elif button_b.is_pressed():        display.show(Image.SAD)    else:        display.show(Image.ASLEEP)    sleep(100)
    Hold B: a sad face.
  4. Step 4: Amazed by both

    and is 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.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if button_a.is_pressed() and button_b.is_pressed():        display.show(Image.SURPRISED)    elif button_a.is_pressed():        display.show(Image.HAPPY)    elif button_b.is_pressed():        display.show(Image.SAD)    else:        display.show(Image.ASLEEP)    sleep(100)
    Hold A and B together: a surprised face.
  5. Step 5: Count the hugs

    A press of A is a hug, so count them in a variable called hugs. was_pressed() is True once 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.

    main.pyMicroPython micro:bit editor
    from microbit import * hugs = 0 while True:    if button_a.was_pressed():        hugs = hugs + 1    if pin_logo.is_touched():        display.scroll(hugs)    elif button_a.is_pressed() and button_b.is_pressed():        display.show(Image.SURPRISED)    elif button_a.is_pressed():        display.show(Image.HAPPY)    elif button_b.is_pressed():        display.show(Image.SAD)    else:        display.show(Image.ASLEEP)    sleep(100)
    Three hugs, then touch the logo: it scrolls 3.
    Watch out The touch logo is only on a micro:bit V2. On an older V1, use button_b.was_pressed() to show the count instead, and take B's sad face out.
  6. 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?

    Send 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.

  1. Sound effects

    On a micro:bit V2, give each face a sound.

    Show a hint

    audio.play(Sound.HAPPY) plays a built in sound. Use was_pressed() for this, so the sound plays once per press, not ten times a second.

  2. 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
    main.pyMicroPython micro:bit editor
    from microbit import *import random display.show(Image.ASLEEP)sleep(random.randint(2000, 5000))display.show(Image.HEART)start = running_time()while not button_a.is_pressed():    sleep(1)display.scroll(running_time() - start)
  3. 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
main.pyMicroPython micro:bit editor
from microbit import * hugs = 0 while True:    if button_a.was_pressed():        hugs = hugs + 1    if pin_logo.is_touched():        display.scroll(hugs)    elif button_a.is_pressed() and button_b.is_pressed():        display.show(Image.SURPRISED)    elif button_a.is_pressed():        display.show(Image.HAPPY)    elif button_b.is_pressed():        display.show(Image.SAD)    else:        display.show(Image.ASLEEP)    sleep(100)
Notes for the teacher

Timing

  1. 5 minutes: recap the editor and the simulator from day 1.
  2. 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.
  3. 15 minutes: step 5, the tests, then sending to the micro:bits.

Mistakes to look out for

  1. elif or else not lined up with its if: an IndentationError, or code that never runs.
  2. Missing brackets: button_a.is_pressed without () is always true, so the pet always smiles.
  3. Counting with is_pressed() instead of was_pressed(): one long press counts several times.
  4. 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.