from machine import Pin
import utime
import urandom
# set up
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
# global variables
timer_start = 0
# interrupt handler
def button_handler(pin):
button.irq(handler=None) # turn off the trigger, so it´s used only once
reaction_time = utime.ticks_diff(utime.ticks_ms(), timer_start)
print("Your reaction time was " +str(reaction_time) + " milliseconds.")
print("Program complete.")
led.value(1)
utime.sleep(urandom.uniform(5 ,10)) # random amount of seconds (5-10)
# turn off led
led.value(0)
timer_start = utime.ticks_ms() # starts counting after the led turns off
# enable interrupt
button.irq(trigger=Pin.IRQ_RISING, handler=button_handler)