覆盆子PI:2个按钮,2个LED

时间:2016-06-30 23:02:14

标签: python raspberry-pi

我试图用Raspberry Pi制作一个Python程序,当我按下一个按钮时,只有红色LED变亮,当我按下另一个按钮时,只有绿色LED变亮。没有按下任何按钮时,所有LED都熄灭。但是当我尝试运行它时,两个LED都保持亮起,当我按下按钮时,显示器说我按了它,但没有任何变化。我该怎么做才能解决这个问题?我100%确定没有布线问题,显示器没有显示错误。顺便说一句,这是代码:

        import RPi.GPIO as GPIO
        import time

        GPIO.setwarnings(False)
        red_walk_LED = 16
        green_traf_LED = 15
        Btn_one = 22 # pin12 --- button
        Btn_two = 29 # pin29 --- 2nd button
        # GLOBAL VARIABLES
        red_Led_status = 1
        Green_Led_status = 1
        flag_btn_one_pushed = 0
        flag_btn_two_pushed = 0

        def all_leds_off():
            GPIO.output(green_traf_LED, GPIO.HIGH) 
            GPIO.output(yellow_traf_LED, GPIO.HIGH)
            GPIO.output(red_traf_LED, GPIO.HIGH)
            GPIO.output(red_walk_LED, GPIO.HIGH)
            GPIO.output(white_walk_LED, GPIO.HIGH)
    def setup():
        global green_LED_frequence
        global red_LED_frequence
        GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
        #set LEDs as outputs
        GPIO.setup(green_traf_LED, GPIO.OUT)
        GPIO.setup(red_traf_LED, GPIO.OUT)
        GPIO.setup(Btn_one, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode as input, and pull up to high level(3.3V)
        GPIO.setup(Btn_two, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        red_LED_frequence = GPIO.PWM(red_traf_LED, 1000) # set Frequece to 1KHz
        green_LED_frequence = GPIO.PWM(green_traf_LED, 1000)
        red_LED_frequence.start(0) # Duty Cycle = 0
        green_LED_frequence.start(0)

def Btn_one_push(ev=None):
    print('OK, the 1st button was pushed')
    global red_Led_status #we are allowed to change these variables in this function
    global my_counter 
    global flag_btn_one_pushed
    global red_LED_frequence

    red_Led_status      = not red_Led_status #change LED status 0-1 or 1-0 
    flag_btn_one_pushed = 1

    my_delay = 0.2
    GPIO.output(green_traf_LED, GPIO.HIGH)
    if red_Led_status == 1: #1-on
        print('ok, reds on')
        for dc in range(0, 101, 4): # Increase duty cycle: 0~100
            red_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
            time.sleep(0.02)
        for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
            red_LED_frequence.ChangeDutyCycle(dc)
            time.sleep(0.02)


    all_leds_off() #turn all LEDs off!!
    flag_btn_pushed = 0

def Btn_two_push(ev=None):
    print('OK, the 2nd button was pushed')
    global Green_Led_status
    global flag_btn_two_pushed
    global green_LED_frequence

    Green_Led_status      = not Green_Led_status #change LED status 0-1 or 1-0 
    flag_btn_two_pushed = 1

    my_delay = 0.2
    GPIO.output(red_traf_LED, GPIO.HIGH)
    if Green_Led_status == 1: #1-on
        print('This is supposed to work!')
        for dc in range(0, 101, 4): # Increase duty cycle: 0~100
            print(green_LED_frequence)
            green_LED_frequence.ChangeDutyCycle(dc) # Change duty cycle
            time.sleep(0.08)
        for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
            green_LED_frequence.ChangeDutyCycle(dc)
            time.sleep(0.08)    
    all_leds_off() #turn all LEDs off!!
    flag_btn_two_pushed = 0





def loop():
    global flag_btn_one_pushed
    global flag_btn_two_pushed
    GPIO.add_event_detect(Btn_one, GPIO.FALLING, callback=Btn_one_push) # wait for change in GPIO 0-1 or 1-0
    GPIO.add_event_detect(Btn_two, GPIO.FALLING, callback=Btn_two_push)
    while True: #when the button isn't pushed, do this
        all_leds_off()
    else:
        pass

def destroy():
    all_leds_off()
    GPIO.cleanup() # Release resource

if __name__ == '__main__': # Program start from here
    setup()
    try:
        loop()
    except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
        destroy()

1 个答案:

答案 0 :(得分:0)

你应该尝试删除这些行:

while True: #when the button isn't pushed, do this
    all_leds_off()
else:
    pass

并将其替换为:

all_leds_off()

每次按下按钮都应该触发回调。

以下是另一个例子:

https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=102631&p=760629

另外,您是否尝试过一些简单的代码片段来单独打开和关闭LED并检测按钮按下以证明它不是布线问题?接线错误并不需要太多!