MI-3micro:bitBeginner

micro:bit Mood Badge: Faces with Buttons A and B

Button A shows a happy face, button B a sad one and both together a surprised one; after five seconds without a press the micro:bit falls asleep. is_pressed reads a button and if / elif makes the choice. No extra parts are needed.

0.0(0)
micro:bit Mood Badge: Faces with Buttons A and B
Show how you feel today with a badge on your shirt. You learn to read whether a button is pressed, show different images depending on conditions, tell when both buttons are pressed together, and measure elapsed time so the board falls asleep by itself.

Video

Circuit Diagram

micro:bit Mood Badge: Faces with Buttons A and B - Circuit Diagram

Source Code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   micro:bit Mood Badge: Faces with Buttons A and B
4# ─────────────────────────────────────────
5# Button A shows a happy face, button B a sad one, and both together a
6# surprised one. The face stays on; after five seconds without a press the
7# board falls asleep. No extra parts are needed.
8#
9# Pins:
10#   LED -> Onboard 5x5 LED display
11#   P5 -> Onboard button A
12#   P11 -> Onboard button B
13
14from microbit import *
15
16# ============ SETTINGS ============
17SLEEP_AFTER_MS = 5000    # Falls asleep after this long (ms)
18LOOP_MS = 50             # Loop pause (ms)
19
20last_press = running_time()
21display.show(Image.ASLEEP)
22
23while True:
24    a = button_a.is_pressed()
25    b = button_b.is_pressed()
26    if a and b:
27        display.show(Image.SURPRISED)
28    elif a:
29        display.show(Image.HAPPY)
30    elif b:
31        display.show(Image.SAD)
32
33    # Reset the timer on a press; fall asleep after a long time without one.
34    if a or b:
35        last_press = running_time()
36    elif running_time() - last_press > SLEEP_AFTER_MS:
37        display.show(Image.ASLEEP)
38    sleep(LOOP_MS)
39

FAQ

Why does pressing both buttons give a separate face?

The code first checks whether both buttons are pressed together. Because that condition comes first, only the surprised face shows while A and B are both held. In the opposite order the condition for A would match first and the surprised face would never appear. In an if and elif chain only the first matching condition runs.

Can I use other faces?

Yes. The built-in images include more faces such as Image.ANGRY, Image.CONFUSED, Image.SILLY, Image.FABULOUS and Image.MEH. Just write one of them in place of Image.HAPPY or Image.SAD in the code.

How do I put this code on the micro:bit?

The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.

Does it work on the older micro:bit (V1) too?

The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.