import machine
import time
import random
# Define GPIO pin numbers for LEDs and pushbuttons
led_pins = [17, 18, 27]
button_pins = [5, 6, 13]
# Initialize GPIO pins for LEDs as output and pushbuttons as input
leds = [machine.Pin(pin, machine.Pin.OUT) for pin in led_pins]
buttons = [machine.Pin(pin, machine.Pin.IN, machine.Pin.PULL_UP) for pin in button_pins]
while True:
# Wait for a random amount of time
time.sleep(random.uniform(1, 5))
# Turn off all LEDs
for led in leds:
led.off()
# Choose a random LED to light up
random_led = random.choice(leds)
random_led.on()
target_led_index = leds.index(random_led)
# Record the start time
start_time = time.ticks_ms()
# Wait for user input
user_input = None
while user_input is None:
for i, button in enumerate(buttons):
if button.value() == 0:
user_input = i
break
# Record the end time
end_time = time.ticks_ms()
# Calculate reaction time
reaction_time = time.ticks_diff(end_time, start_time)
# Display reaction time
print("Reaction time:", reaction_time, "ms")
# Check if user pressed the correct button
if user_input == target_led_index:
print("Correct!")
else:
print("Incorrect!")