If Siri and all the other "smart" devices can set a timer and let us know when the time is up, why can't we with the microbit? Here's a code sample which does just that.

The code is built in two halves of functionality, setting the right duration for the timer and doing the actual timing.

We start off waiting for a time to be set and the "a" and "b" buttons are used to increment and decrement the time. Guards are there to stop us going to far up or down below one second.

Shaking the device starts the timer and we display an animated clock moving once per second for twelve seconds in a full revolution (rather than the usual 60).

Once we get to the full time, we stop the timer and display a smiley face.


from microbit import *

started = False
elapsed_seconds = 0
timer_duration = 20

animation = [
    Image.CLOCK1,
    Image.CLOCK2,
    Image.CLOCK3,
    Image.CLOCK4,
    Image.CLOCK5,
    Image.CLOCK6,
    Image.CLOCK7,
    Image.CLOCK8,
    Image.CLOCK9,
    Image.CLOCK10,
    Image.CLOCK11,
    Image.CLOCK12,
]

display.scroll(str(timer_duration))

while True:

    if started:
        sleep(1000)
        elapsed_seconds += 1

        if elapsed_seconds < timer_duration:
            animation_frame = elapsed_seconds % len(animation)
            display.show(animation[animation_frame])
        else:
            display.show(Image.HAPPY)
            started = False
    else:
        if accelerometer.was_gesture("shake"):
            started = True
            elapsed_seconds = 0
        else:
            if button_a.was_pressed():
                timer_duration = min(240, timer_duration + 1)
                display.scroll(str(timer_duration))
            elif button_b.was_pressed():
                timer_duration = max(1, timer_duration - 1)
                display.scroll(str(timer_duration))
        sleep(250)