Advertisement
belrey10

4 – Reaction Time Tester

Nov 5th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import machine
  2. import time
  3.  
  4. # Define GPIO pins for the LED and button
  5. led_pin = machine.Pin(14, machine.Pin.OUT)
  6. button_pin = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
  7.  
  8. # Function to measure reaction time
  9. def reaction_time_tester():
  10.     time.sleep(5)
  11.     led_pin.on()  # Turn on the LED
  12.     start_time = time.ticks_ms()  # Get start time in milliseconds
  13.  
  14.     # Wait for button press
  15.     while True:
  16.         if button_pin.value() == 1:
  17.             end_time = time.ticks_ms()  # Get end time in milliseconds
  18.             reaction_time = end_time - start_time  # Calculate reaction time
  19.             print('Reaction Time:', reaction_time, 'ms')
  20.             break
  21.  
  22.     led_pin.off()  # Turn off the LED
  23.  
  24. # Call the function to start the reaction time tester
  25. reaction_time_tester()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement