Reaction Time Tester
Fatley’s Pi Pico Reaction Time Game — This project helped me understand the wiring and coding and was very useful during the creation of my project.
Mini STEM LED Game Platform — This project showed useful wiring and coding examples, although it used a different Pi Pico model.
After Dinner Reaction Time Tester — This project helped me understand the coding and improve my own program.
Supplies
Parts:
1x Button
1x I2C Screen
1x Pi Pico
1x Protoboard
Tools and components:
Hookup Wire
3D Printer
soldering iron
wire stripers
Wiring Diagram
Wire as shown
Code
import utime
import urandom
from machine import Pin, SoftI2C
from pico_i2c_lcd import I2cLcd
# --- Configuration ---
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C and LCD
i2c = SoftI2C(sda=Pin(4), scl=Pin(5), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Initialize Button on GP15
button = Pin(15, Pin.IN, Pin.PULL_DOWN)
# Global variable to store the best time (initialized to a very high value)
best_time = 999999
def format_time_decimal(total_ms):
"""Helper to convert ms into 'S.ms sec' string"""
seconds = total_ms // 1000
milisec = total_ms % 1000
return "{}.{:03d} sec".format(seconds, milisec)
def run_reaction_test():
global best_time
lcd.clear()
lcd.putstr("Get Ready...")
random_wait = urandom.randint(1500, 5000)
start_wait = utime.ticks_ms()
# Jump Start Detection
while utime.ticks_diff(utime.ticks_ms(), start_wait) < random_wait:
if button.value() == 1:
lcd.clear()
lcd.putstr(" JUMP START!")
lcd.move_to(0, 1)
lcd.putstr(" Try again...")
utime.sleep(2)
return
utime.sleep_ms(5)
# Signal GO
lcd.move_to(0, 0)
lcd.putstr("--- !!!GO!!! --- ")
start_time = utime.ticks_ms()
while button.value() == 0:
pass
end_time = utime.ticks_ms()
current_ms = utime.ticks_diff(end_time, start_time)
# Check if this is a new Record
is_new_record = False
if current_ms < best_time:
best_time = current_ms
is_new_record = True
# Display Results
lcd.clear()
if is_new_record and best_time != current_ms: # Only flash 'New Record' if not the very first run
lcd.putstr(" NEW RECORD! ")
else:
lcd.putstr("Time: " + format_time_decimal(current_ms))
lcd.move_to(0, 1)
lcd.putstr("Best: " + format_time_decimal(best_time))
utime.sleep(5)
lcd.clear()
lcd.putstr("Press the button\nto try again")
# Main loop
lcd.clear()
lcd.putstr("Reaction Timer\nPress to Start")
while True:
if button.value() == 1:
while button.value() == 1:
utime.sleep_ms(10)
run_reaction_test()
utime.sleep_ms(50)