Raspberry Pi中使用GPIO LED和按钮无法正常工作的程序

时间:2018-11-29 12:25:29

标签: python raspberry-pi raspberry-pi3 raspbian gpio

基本上,我尝试使用3个LED和3个按钮开关在Raspberry Pi中创建一个Simon游戏,但这似乎不起作用。西蒙是一款游戏,其中随机出现一系列闪烁的灯光,闪烁的数量每回合增加,并且玩家必须按顺序按相应的按钮。有三个LED灯和三个上拉按钮开关。你能帮我吗?

import time
import random
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(23,GPIO.IN)
GPIO.setup(19,GPIO.IN)
GPIO.setup(40,GPIO.IN)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(38,GPIO.OUT)
def main():
    colours = []
    while True:
        choice = random.randint(0, 2)
        print(choice)
        colours.append(choice)
        for c in colours:
            if c == 0:
                GPIO.output(11,True)
                time.sleep(0.5)
                GPIO.output(11,False)
                time.sleep(0.5)
            if c == 1:
                GPIO.output(13,True)
                time.sleep(0.5)
                GPIO.output(13,False)
                time.sleep(0.5)
            if c == 2:
                GPIO.output(38,True)
                time.sleep(0.5)
                GPIO.output(38,False)
                time.sleep(0.5)
        count = 0
        undone = True
        while undone:
            if count >= len(colours):
                print("Correct!")
                break
            elif not GPIO.input(19):
                if colours[count] == 0:
                    count = count + 1
                    while not GPIO.input(19):
                        print("bluecorrect")
                    continue
                else:
                    print("You are wrong!")
                    GPIO.cleanup()
                    quit()
            elif not GPIO.input(23):
                if colours[count] == 1:
                    count = count + 1
                    while (not GPIO.input(23)):
                        print("redcorrect")
                    continue
                else:
                    print("You are wrong!")
                    GPIO.cleanup()
                    quit()
            elif not GPIO.input(40):
                if colours[count] == 2:
                    count = count + 1
                    while not GPIO.input(40):
                        print("greencorrect")
                    continue
                else:
                    print("You are wrong!")
                    GPIO.cleanup()
                    quit()
            else:
                continue
try:
    main()
except KeyboardInterrupt:
    GPIO.cleanup()

0 个答案:

没有答案