A reaction time game helps you practice how fast you can respond to a signal. It is fun and trains your reflexes.
Reaction time game in 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) # Game logic # 1. Wait random time # 2. Turn on LED # 3. Measure time until button press # 4. Show reaction time GPIO.output(LED_PIN, GPIO.LOW)
Use GPIO.setup to set pins as input or output.
Use GPIO.input to read button state and GPIO.output to control LED.
GPIO.setup(LED_PIN, GPIO.OUT) GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
button_state = GPIO.input(BUTTON_PIN)This program waits a random time, then lights an LED. When you press the button, it measures how fast you reacted and shows the time.
import time import random import RPi.GPIO as GPIO 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) try: print("Get ready...") GPIO.output(LED_PIN, GPIO.LOW) time.sleep(random.uniform(2, 5)) # Wait random time between 2 and 5 seconds print("Go! Press the button as fast as you can!") GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED on start_time = time.time() # Wait for button press while GPIO.input(BUTTON_PIN): pass # Button not pressed yet reaction_time = time.time() - start_time GPIO.output(LED_PIN, GPIO.LOW) # Turn LED off print(f"Your reaction time is {reaction_time:.3f} seconds.") finally: GPIO.cleanup()
Make sure your button is connected correctly with a pull-up or pull-down resistor to avoid false readings.
Use GPIO.cleanup() to reset pins when the program ends.
Try to keep your hand ready to press the button quickly after the LED lights up.
A reaction time game measures how fast you press a button after a signal.
Use Raspberry Pi GPIO pins to control an LED and read a button press.
Timing and waiting for input are key parts of this simple game.
Practice
Solution
Step 1: Understand the game goal
The reaction time game tests how fast a player reacts to a signal, usually an LED lighting up.Step 2: Identify the key action
The player must press a button as soon as the LED lights up to measure reaction speed.Final Answer:
To measure how quickly a player presses a button after an LED lights up -> Option AQuick Check:
Reaction time = button press speed after LED [OK]
- Confusing reaction time with counting presses
- Thinking the game controls LED brightness
- Assuming it displays colors randomly
Solution
Step 1: Recall GPIO setup syntax
To set a pin as input, use GPIO.setup(pin_number, GPIO.IN).Step 2: Match the correct option
GPIO.setup(17, GPIO.IN) uses GPIO.setup(17, GPIO.IN), which is correct for input pin setup.Final Answer:
GPIO.setup(17, GPIO.IN) -> Option BQuick Check:
GPIO.setup(pin, GPIO.IN) sets input pin [OK]
- Using GPIO.OUT instead of GPIO.IN for input
- Confusing GPIO.input() with GPIO.setup()
- Trying to use GPIO.output() on input pins
import time
start = time.time()
# LED lights up here
input('Press Enter as fast as you can!')
end = time.time()
print(f'Reaction time: {end - start:.2f} seconds')Solution
Step 1: Understand timing with time.time()
The code records time before and after waiting for user input.Step 2: Calculate reaction time
The difference end - start measures how long the user took to press Enter after the prompt.Final Answer:
Reaction time: time taken between prompt and Enter press -> Option DQuick Check:
Reaction time = end - start after input [OK]
- Assuming reaction time is always zero
- Confusing syntax errors with correct code
- Thinking input() is not called
GPIO.setup(18, GPIO.OUT) GPIO.setup(23, GPIO.IN) GPIO.output(23, GPIO.HIGH) # Wait for button press on pin 23
Solution
Step 1: Check pin setup
Pin 23 is set as input with GPIO.setup(23, GPIO.IN).Step 2: Identify incorrect usage
GPIO.output() cannot be used on input pins; it is only for output pins.Final Answer:
GPIO.output() used on input pin 23, which is incorrect -> Option CQuick Check:
GPIO.output() only for output pins [OK]
- Trying to write output on input pins
- Forgetting to setup pins before use
- Assuming no error when pin modes mismatch
Solution
Step 1: Use random.randint for integer delay
random.randint(1, 5) returns an integer between 1 and 5 seconds.Step 2: Sleep before turning LED on
time.sleep(wait) pauses the program before GPIO.output(18, GPIO.HIGH) lights the LED.Final Answer:
import random import time wait = random.randint(1, 5) time.sleep(wait) GPIO.output(18, GPIO.HIGH) -> Option AQuick Check:
random.randint + sleep before LED ON [OK]
- Using random.random with arguments (invalid)
- Sleeping after turning LED on
- Not importing random module
Ready to prove it?
Pass the test and this pattern moves unknown
