# Reaction timer: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/reaction-timer
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Reaction timer: press B, wait for the dot, then press A as fast as you can.
from microbit import *
import random

display.show("B")
while True:
    if button_b.was_pressed():
        display.clear()
        button_a.was_pressed()
        wait = random.randint(1000, 4000)
        began = running_time()
        too_soon = False
        while running_time() - began < wait:
            if button_a.was_pressed():
                too_soon = True
                break
            sleep(10)
        if too_soon:
            display.show(Image.NO)
        else:
            display.set_pixel(2, 2, 9)
            start = running_time()
            while not button_a.was_pressed():
                sleep(1)
            display.scroll(running_time() - start)
    sleep(20)
