Create a new Python script named reaction_time.py
.
At the top of the file, import LED
from the GPIO module. Setup your LED with the pin number that you connected it to when building your circuit.
from gpiozero import LED
led = LED(-1) # Change the pin number to match your circuit
At the end of the file, write code which turns the LED on, waits five seconds, and then turns the LED off.
In order to wait, recall the sleep
function from the time
module. To use it, add the following import to the top of your file.
from time import sleep
Run your program and check that the LED turns on, and turns off five seconds later. If the LED doesn't do what you expect it to, there may be a problem in your circuit or code. Identify and fix any problems, then run your program again to check if the problem has been fixed.
In order to make the LED turn off randomly, so players can't be prepared to push their buttons, use the random module. At the top of your file, add the following import.
from random import uniform
The uniform
function can be used to generate a random floating point number (i.e. a decimal instead of an integer). The random module's documentation provides the following description of uniform
.
random.uniform(a, b)
Return a random floating point number
N
such thata <= N <= b
fora <= b
andb <= N <= a
forb < a
.The end-point value
b
may or may not be included in the range depending on floating-point rounding in the equationa + (b-a) * random().
Replace the five second time you added above with a random time by using the return value of the uniform
function. You will need to choose values of a
and b
to set the maximum and minimum delay for your game.
Before going further, make sure that your LED code is working. Run your program several times and check that the LED turns on and then off after a random amount of time. The time it takes until the LED turns off should be different each time your program runs, although you might not be able to see a difference between the times depending on how close the minimum and maximum delay you chose are.
If the LED doesn't do what you expect it to, there may be a problem in your circuit or code. Identify and fix any problems, then run your program again to check if the problem has been fixed.