I will add more detail about the build later but here is the code.
I have a green LED connected to GPIO output 7 and a red LED connected to output 3.
I have a switch connected to input 23.
The program starts with the Green LED on, after a random time period (1-10 seconds) the LED turns off and the red one comes on. It then measures the time taken to press the switch.
import time
import random
import RPi.GPIO as GPIO
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(3,GPIO.OUT)
GPIO.setup(23,GPIO.IN, pull_up_down=GPIO.PUD_DOWN )
GPIO.output(3,0), GPIO.output(7,0)
print ""
print ""
print ""
print ""
print "You may begin now"
print "The Green light will come on for a random amount of time"
print "It will then change to red"
print "As soon as it changed to red hit the blue button"
print "The game will start in 5 seconds"
time.sleep(5)
GPIO.output(7,1)
r = random.randint(1,10)
time.sleep(r)
GPIO.output(7,0), GPIO.output(3,1)
start = time.time()
try:
while True:
if (GPIO.input(23) == 1):
end = time.time()
print "you pressed the button"
elapsed = end - start
print "it took you "
print round(elapsed,2)
print "Try to beat that next time"
GPIO.output (3,0)
GPIO.output (7,0)
break
else:
GPIO.output(7,0), GPIO.output(3,1)
except KeyboardInterrupt:
GPIO.output(3,0)
GPIO.output(7,0)