import time
import random
import RPi.GPIO as GPIO
# Setup pins
LED_PIN = 18
BUTTON_PIN = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Wait random time
wait_time = random.uniform(2, 5)
time.sleep(wait_time)
# Turn on LED
GPIO.output(LED_PIN, GPIO.HIGH)
start_time = time.time()
# Wait for button press
while GPIO.input(BUTTON_PIN) == GPIO.HIGH:
pass
reaction_time = time.time() - start_time
print(f"Reaction time: {reaction_time:.3f} seconds")This code waits a random time, lights an LED, waits for a button press, then prints the reaction time.