试图让灯光与实验室相对应

时间:2018-03-10 16:24:32

标签: python led

你好,所以我做了一个反应时间游戏,就像当你按下回车键开始游戏时,你等待灯打开,然后按下按钮结束游戏。所以对于反应灯,我有完全与实验室合作,但我的问题是我的另外两个灯没有工作。这些灯是你有一个很好的反应时间,然后绿灯会亮。但如果你的反应时间不好然后红色会亮起。两者都不适合我,我不明白这个问题。 `

反应时间实验室

 import RPi.GPIO as GPIO
 import time
 from time import time as the_timer
 import random

#Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#Button LED setup
led = 19,12,22
button = 21

#GPIO setup channels for the pins 
GPIO.setup(19, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)

GPIO.output(12,GPIO.LOW)
GPIO.output(22,GPIO.LOW)

#Introduction to the lab and pressing enter to input th start of the lab

print("Welcome to the reaction time lab. here we will want you to  press 
the button as soon at the light turn"
  "on too see if you have a good reaction time or not." "if you recieved the Green LED, then you have a"
  "good reacting time.  If you recieved the Red LED, then you have a bad reacting time.")       
input("press enter to start")
wait_time = (random.randint(5,10))
print(wait_time)
GPIO.output(19,GPIO.LOW)
time.sleep(wait_time)
GPIO.output(19,GPIO.HIGH)
start_time = the_timer()


try:
    while(1):
        if (GPIO.input(button) == False):
            print("Button Pressed")
            break 
    end_time = the_timer()

    print("Started at" +time.strftime("%X", time.localtime(start_time)))
    print("Ended at" +time.strftime("%X", time.localtime(end_time)))

    print("Your reaction time was {}".format(end_time - start_time))
#Led color lights up depending on the reaction time.
    if (format(end_time - start_time < 1)):
        GPIO.output(22,GPIO.HIGH)   
    else: 
        GPIO.output(12,GPIO.HIGH)

except KeyboardInterrupt:
    print("Reset Program")
finally:
    GPIO.cleanup()`

任何人都可以告诉我我的代码存在什么问题吗?

0 个答案:

没有答案