GPIO Raspberry Pi LED Loop

时间:2017-09-25 21:05:43

标签: python raspberry-pi gpio led

当我按下按钮时,我试图使LED无限闪烁,但是当我按下另一个按钮使闪烁停止时。我的代码使按钮开始无限闪烁,但第二个按钮不会阻止LED闪烁。

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Setting Up All LEDs
GPIO.setup(18,GPIO.OUT)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(4,GPIO.OUT)
#Setting Up All Buttons
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Button Event Handling
while True:
        input_state = GPIO.input(20)
    input_state2 = GPIO.input(21)

        if input_state == False:
        x = 1
        while x == 1:
                GPIO.output(18,GPIO.HIGH)
                time.sleep(.2)
                GPIO.output(18,GPIO.LOW)
                                time.sleep(.2)
                if input_state2 == False:
                    x = 2
        GPIO.output(18,GPIO.LOW)

0 个答案:

没有答案