Page 1 of 1

Countdown timer that resets on "shake"

Posted: Sun Nov 17, 2019 12:25 pm
by garrygorman
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

Re: Countdown timer that resets on "shake"

Posted: Mon Nov 18, 2019 12:48 am
by lujo
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

Re: Countdown timer that resets on "shake"

Posted: Mon Nov 18, 2019 1:21 am
by jimmo
Here's a response to a similar question that goes into this in more detail -- viewtopic.php?f=17&t=7150#p40706