0
0
Raspberry Piprogramming~10 mins

Reaction time game in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reaction time game
Start Game
Wait Random Time
Show Signal (LED ON)
Wait for Button Press
Calculate Reaction Time
Display Reaction Time
Option to Restart or Exit
The game waits a random time, then signals the player to press a button, measures reaction time, and shows the result.
Execution Sample
Raspberry Pi
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.
Execution Table
StepActionVariable ValuesOutput/State
1Setup GPIO pinsLED_PIN=18, BUTTON_PIN=23Pins ready
2Generate wait_timewait_time=3.7 (example)Waiting...
3Sleep for wait_timetime.sleep(3.7)Paused
4Turn on LEDLED_PIN=ONLED lights up
5Record start_timestart_time=1000.0 (example)Timer started
6Wait for button pressBUTTON_PIN=HIGH initiallyWaiting for press
7Button pressedBUTTON_PIN=LOWDetected press
8Calculate reaction_timereaction_time=0.245Reaction time calculated
9Print reaction_timereaction_time=0.245Output: Reaction time: 0.245 seconds
💡 Button pressed, reaction time measured and displayed
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 7Final
wait_timeNone3.73.73.73.73.7
LED_PIN1818ONONONON
BUTTON_PIN23232323LOWLOW
start_timeNoneNoneNone1000.01000.01000.0
reaction_timeNoneNoneNoneNoneNone0.245
Key Moments - 3 Insights
Why do we wait a random time before turning on the LED?
Waiting a random time (see Step 2 and 3) prevents the player from predicting when to press the button, ensuring the reaction time is genuine.
Why do we check if BUTTON_PIN is HIGH in the while loop?
The button input is HIGH when not pressed (Step 6). The loop waits until it becomes LOW (pressed), so we detect the exact moment the player reacts.
How is reaction time calculated?
Reaction time is the difference between the current time and start_time recorded when LED turned on (Step 8). This measures how fast the player pressed the button.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of LED_PIN after Step 4?
AOFF
BON
CHIGH
DLOW
💡 Hint
Check the 'Variable Values' column at Step 4 in the execution_table.
At which step does the program detect the button press?
AStep 7
BStep 6
CStep 5
DStep 8
💡 Hint
Look for the step where BUTTON_PIN changes from HIGH to LOW in the execution_table.
If the random wait_time was shorter, how would the reaction_time change?
AIt would be longer
BIt would be shorter
CIt would stay the same
DIt would be zero
💡 Hint
Reaction time measures time after LED lights up, independent of wait_time (see variable_tracker).
Concept Snapshot
Reaction time game on Raspberry Pi:
- Wait random time (2-5 sec)
- Turn on LED as signal
- Wait for button press
- Calculate reaction time = press time - LED on time
- Show result
Use GPIO pins for LED and button input.
Full Transcript
This reaction time game uses Raspberry Pi GPIO pins. First, the program sets up the LED and button pins. It waits a random time between 2 and 5 seconds to prevent guessing. Then it turns on the LED to signal the player to press the button. The program records the time when the LED turns on. It waits until the button is pressed, detecting the change from HIGH to LOW. When pressed, it calculates the reaction time by subtracting the LED on time from the current time. Finally, it prints the reaction time in seconds. This process repeats if the game restarts.