MI-12micro:bitBeginner

micro:bit Click Counter: How Many Went By

Button A adds one, button B takes one away and the gold logo resets. get_presses counts the presses; numbers above 9 scroll round on the display. No extra parts are needed.

0.0(0)
micro:bit Click Counter: How Many Went By
Count the people at a door, the baskets you score or the laps you run. You learn to count button presses without missing any, keep a number in a variable, write your own function and scroll long numbers in the background.

Video

Circuit Diagram

micro:bit Click Counter: How Many Went By - Circuit Diagram

Source Code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   micro:bit Click Counter: How Many Went By
4# ─────────────────────────────────────────
5# Button A adds one, button B takes one away and a touch on the gold logo
6# resets the counter. The number shows on the LED display; two-digit
7# numbers scroll past. Use it to count people at a door or the laps you
8# run. No extra parts are needed.
9#
10# Pins:
11#   LED -> Onboard 5x5 LED display
12#   P5 -> Onboard button A
13#   P11 -> Onboard button B
14#   LOGO -> Touch logo
15
16from microbit import *
17
18# ============ SETTINGS ============
19LOOP_MS = 50         # Loop pause (ms)
20
21
22# One digit fits on the display; bigger numbers keep scrolling in the background.
23def show_count(number):
24    if number < 10:
25        display.show(number)
26    else:
27        display.scroll(number, wait=False, loop=True)
28
29
30count = 0
31show_count(count)
32
33while True:
34    # get_presses gives the number of presses since the last check, so no fast press is lost.
35    change = button_a.get_presses() - button_b.get_presses()
36    if pin_logo.is_touched() and count != 0:
37        count = 0
38        show_count(count)
39    elif change != 0:
40        count = max(0, count + change)       # Never goes below zero
41        show_count(count)
42    sleep(LOOP_MS)
43

FAQ

How high can it count?

The code has no upper limit; the number grows as far as you like. 0 to 9 stay on as one picture, 10 and above scroll round the display. One pass of a three-digit number takes about three seconds. The counter never goes below zero.

Is the number lost when the board is switched off?

Yes. The number lives in the board's working memory; when the battery comes out or the reset button on the back is pressed, the counter starts from 0. For a long count run the board from a battery pack and write an important number down.

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.