Countdown timer that resets on "shake"

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
garrygorman
Posts: 1
Joined: Sun Nov 17, 2019 11:34 am

Countdown timer that resets on "shake"

Post by garrygorman » Sun Nov 17, 2019 12:25 pm

Hi all,
I have created a program using blocks that counts down from 100 and sounds an alarm on zero. The timer resets to 100 on "shake". I need to do this in micropython but I am having difficulty running the countdown timer and the shake function at the same time. Any help would be greatly appreciated.

Thanks

Garry

lujo
Posts: 24
Joined: Sat May 11, 2019 2:30 pm

Re: Countdown timer that resets on "shake"

Post by lujo » Mon Nov 18, 2019 12:48 am

Hi,

Microsoft MakeCode has interrupts. Micropython on the micro:bit does not.

This works for me (using the mu-editor):

Code: Select all

from microbit import *

count = 100
while True:
    display.show(count, delay=50)
    count = count - 1
    if count <= 0:
        display.show(Image.ANGRY)
        break
    sleep(10)
    if accelerometer.was_gesture('shake'):
        display.show(Image.HAPPY, delay=1000, clear=True)
        count = 100
lujo

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Countdown timer that resets on "shake"

Post by jimmo » Mon Nov 18, 2019 1:21 am

Here's a response to a similar question that goes into this in more detail -- viewtopic.php?f=17&t=7150#p40706

Post Reply