import board
import digitalio
import analogio
import time
# Setup LEDs
led_pins = [board.GP0, board.GP1, board.GP2]
leds = [digitalio.DigitalInOut(pin) for pin in led_pins]
for led in leds:
led.direction = digitalio.Direction.OUTPUT
# Setup button
button = digitalio.DigitalInOut(board.GP15)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
# Setup potentiometer
pot = analogio.AnalogIn(board.GP26)
def read_pot():
return pot.value / 65535 # scale to 0-1
def start_countdown():
countdown_time = 10 # 10 seconds countdown
print("Start countdown...")
for i in range(countdown_time, 0, -1):
print(f"Countdown: {i}")
if i <= 3:
leds[3-i].value = True # Turn on LEDs one by one for the last 5 seconds
time.sleep(potentiometer_delay())
for led in leds:
led.value = False
print("GO!")
def potentiometer_delay():
# Map potentiometer value to delay range (0.2s to 2s)
delay = read_pot() * 1.8 + 0.2
return delay
def measure_reaction_time():
print("Press the button as quickly as possible!")
start_time = time.monotonic()
while button.value:
pass
reaction_time = time.monotonic() - start_time
print(f"Reaction Time: {reaction_time:.3f} seconds")
while True:
if not button.value: # Button pressed
while not button.value: # Wait for button release
pass
start_countdown()
measure_reaction_time()