繁体   English   中英

Raspberry PI:2个按钮,2个LED

[英]Raspberry PI: 2 buttons, 2 LEDs

我正在尝试使用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()

您应该尝试删除这些行:

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并检测按钮按下情况,以证明这不是接线问题? 接线错误并不需要太多!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM