【问题标题】:LED control on Raspberry Pi by GPIO with Python通过 GPIO 和 Python 对 Raspberry Pi 进行 LED 控制
【发布时间】:2015-04-06 08:02:24
【问题描述】:

我正在使用连接到 Raspberry Pi 上的 GPIO 引脚的瞬时开关来控制 4 个 LED。 我连接了五个按钮。 按下前 4 个按钮时,根据当前状态将连接的 LED 的状态从打开切换到关闭或从关闭切换到打开。 第五个按钮根据 GPIO 18 的开/关状态打开所有 4 个 LED 或关闭所有 4 个 LED。 我还有一个额外的 LED 连接到 GPIO 引脚 18,这只是根据引脚 18 的状态打开或关闭。 这个项目的想法是能够独立控制 LED 并拥有一个主控制按钮。连接到引脚 18 的 LED 是监控 LED,如果 4 个 LED 中的任何一个点亮,它应该点亮,并且只有在所有 4 个 LED 同时熄灭时它才应该熄灭。 这一切都基于监控按钮按下并采取相应措施的 Python 脚本。

我编写的用于一次控制所有 LED 的代码如下所示:

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)

chan_list = (4,17,22,27)

GPIO.setup(chan_list,GPIO.OUT)

GPIO.output(chan_list,0)

GPIO.setup(18,GPIO.OUT)

GPIO.output(18,0)

while True:
    input_state = GPIO.input(26)
    if input_state == False:
         chan_list = (4,17,22,27)
         GPIO.output(18, not GPIO.input(18))
         time.sleep(0.1)
         GPIO.output(chan_list, GPIO.input(18))
         time.sleep(0.4)

此代码似乎运行良好。如您所见,它切换了引脚 18 的当前状态,然后将该状态应用于引脚 4、17、22 和 27,这些引脚是 LED 所连接的引脚。

我编写的用于控制各个 LED 的代码有点复杂,看起来像这样:

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(4,GPIO.OUT)
GPIO.output(4,0)

GPIO.setup(17,GPIO.OUT)

GPIO.setup(22,GPIO.OUT)

GPIO.setup(27,GPIO.OUT)

GPIO.setup(18,GPIO.OUT)

while True:
    input_state = GPIO.input(5)
    if input_state == False:
        if GPIO.output(17,0) == False:
            GPIO.output(4, not GPIO.input(4))
        elif GPIO.output(22,0) == False:
            GPIO.output(4, not GPIO.input(4))
        elif GPIO.output(27,0) == False:
            GPIO.output(4, not GPIO.input(4))
        else:
            GPIO.output(4, not GPIO.input(4))
            time.sleep(0.1)
            GPIO.output(18, GPIO.input(4))
            time.sleep(0.4)

有 4 个版本的引脚调整,每个 LED 一个版本,这个版本使用引脚 5 作为输入来检测按钮按下,引脚 4 作为输出来激活 LED。我想做的是这样的:

按下时(如果 LED1 熄灭)

- Toggle LED1 on, also toggle pin 18 on to activate the indicator light.
- Take no further action.

按下时(如果 LED1 亮)

- Check if pin 17 is on or off; 
    - If pin 17 is on toggle LED1 off and take no further action. 
    - If pin 17 is off check if pin 22 is on or off; 
        - If pin 22 is on toggle LED1 off and take no further action. 
        - If pin 22 is off check if pin 27 is on or off; 
            - If pin 27 is on toggle LED1 off and take no further action. 
            - If pin 27 is off toggle LED1 off then set pin 18 to the current status of pin 4 (LED1).

然而,实际情况是这样的:

按下时(如果 LED1 熄灭)

- Turns off all of the other 3 LEDs then toggles LED1 on and toggles pin 18 on.

按下时(如果 LED1 熄灭)

- Turns off all LEDs and toggles pin 18 off.

我这辈子都想不通。 非常感谢您的帮助。

附:原谅我的无知,我昨天开始学习python,完全没有任何编程经验。我确定这很简单,但我似乎无法解决它。

【问题讨论】:

  • 仅供参考,python 有一个名为 elif 的关键字用于 else if。
  • 谢谢@Haketo,我已经用它来清理脚本了。

标签: python raspberry-pi gpio led


【解决方案1】:

听起来你有 3 个逻辑块:

  1. 单独的 LED 控制
  2. 主 LED 控制
  3. 显示器-LED 指示灯控制

将您的代码解耦为函数,我们将它们命名为checkIndividualButtoncheckMasterButtonupdateMonitorLed,每个逻辑块一个,并从主循环中调用它们

import RPi.GPIO as GPIO
import time

# our 3 functions will go here, yet to be written

# setup pins here
all_leds = [???,???,???,???]

GPIO.setup blah blah blah

while True:
    checkIndividualButton(button_pin=17, led_pin=4)  # assuming button wired to pin 17 controls LED on pin 4
    checkIndividualButton(????, ????) # fill this in
    checkIndividualButton(????, ????) # fill this in
    checkIndividualButton(????, ????) # fill this in

    checkMasterButton(master_button_pin=26, monitor_led_pin=18, all_leds) # notice reference to all_leds which we setup above

    updateMonitorLed(all_leds, monitor_led_pin=18)

现在您所要做的就是实现各个功能,每个功能都只做一项工作(TM):

def checkIndividualButton(button_pin, led_pin):
    is_pressed = GPIO.input(button_pin)
    if is_pressed:
        GPIO.output(led_pin, not GPIO.input(led_pin))

def checkMasterButton(master_button_pin, monitor_led_pin, all_led_pins):
    is_pressed = GPIO.input(master_button_pin)
    if is_pressed:
        GPIO.output(monitor_led_pin, not GPIO.input(monitor_led_pin))
        time.sleep(0.1)
        GPIO.output(all_led_pins, GPIO.input(monitor_led_pin))
        time.sleep(0.4)

def updateMonitorLed(all_leds_pins, monitor_led_pin):
    is_any_led_on = False
    for led_pin in all_leds_pins:
        if GPIO.input(led_pin):
            is_any_led_on = True
    GPIO.output(monitor_led_pin, is_any_led_on)
    time.sleep(0.1)

将此函数块粘贴到主程序中的正确位置。

免责声明:我尚未对此进行测试。有一些方法可以进一步优化和清理代码,如果您愿意,很乐意在 cmets 中指导您。

【讨论】:

    【解决方案2】:

    感谢@pbkhrv 提供了非常有用的答案。 我从中学到了很多东西,并设法得到了一段完美地满足我需求的代码。

    最后,我运行了 2 个 python 脚本,其中一个可以在按下按钮时更改引脚状态:

    import RPi.GPIO as GPIO
    import webiopi
    import time
    
    GPIO.setwarnings(False)
    
    GPIO.setmode(GPIO.BCM)
    
    chan_list = (4,17,22,27)
    
    GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
    GPIO.setup(4,GPIO.OUT)
    GPIO.output(4,0)
    
    GPIO.setup(17,GPIO.OUT)
    GPIO.output(17,0)
    
    GPIO.setup(22,GPIO.OUT)
    GPIO.output(22,0)
    
    GPIO.setup(27,GPIO.OUT)
    GPIO.output(27,0)
    
    GPIO.setup(18,GPIO.OUT)
    GPIO.output(18,0)
    
    while True:
    #This portion is pin 4 control from physical switches
        if GPIO.input(5) == False:
            GPIO.output(4, not GPIO.input(4))
            time.sleep(0.3)
    
    #This portion is pin 17 control from physical switches
        if GPIO.input(6) == False:
            GPIO.output(17, not GPIO.input(17))
            time.sleep(0.3)
    
    #This portion is pin 22 control from physical switches
        if GPIO.input(19) == False:
            GPIO.output(22, not GPIO.input(22))
            time.sleep(0.3)
    
    #This portion is pin 27 control from physical switches
        if GPIO.input(13) == False:
            GPIO.output(27, not GPIO.input(27))
            time.sleep(0.3)
    
    #This portion is pins 4,17,22,27 as one control from physical switches. Toggles all on/off
    # based on the current state of pin 18.
        if GPIO.input(26) == False:
            chan_list = (4,17,22,27)
            GPIO.output(18, not GPIO.input(18))
           # time.sleep(0.01)
            GPIO.output(chan_list, GPIO.input(18))
            time.sleep(0.3)
    

    这个是看 LED 指示灯的:

    import webiopi
    import time
    
    GPIO = webiopi.GPIO
    
    GPIO.setup(4,GPIO.OUT)
    GPIO.output(4,0)
    
    GPIO.setup(17,GPIO.OUT)
    GPIO.output(17,0)
    
    GPIO.setup(22,GPIO.OUT)
    GPIO.output(22,0)
    
    GPIO.setup(27,GPIO.OUT)
    GPIO.output(27,0)
    
    GPIO.setup(18,GPIO.OUT)
    GPIO.output(18,0)
    
    # loop function is repeatedly called by WebIOPi 
    while True:
    #Block to control pin 18 state by pin 4 state
        if (GPIO.digitalRead(4) == GPIO.HIGH):
            #webiopi.sleep(0.1)
            GPIO.digitalWrite(18, GPIO.HIGH)
    
        if (GPIO.digitalRead(4) == GPIO.LOW):
            webiopi.sleep(0.01)
            if (GPIO.digitalRead(17) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(22) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(27) == GPIO.HIGH):
                webiopi.sleep(0.01)
            else: 
                GPIO.digitalWrite(18, GPIO.LOW)
                webiopi.sleep(0.01)
    
    #Block to control pin 18 state by pin 17 state
        if (GPIO.digitalRead(17) == GPIO.HIGH):
            #webiopi.sleep(0.1)
            GPIO.digitalWrite(18, GPIO.HIGH)
    
        if (GPIO.digitalRead(17) == GPIO.LOW):
            webiopi.sleep(0.01)
            if (GPIO.digitalRead(4) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(22) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(27) == GPIO.HIGH):
                webiopi.sleep(0.01)
            else:
                GPIO.digitalWrite(18, GPIO.LOW)
                webiopi.sleep(0.01)
    
    #Block to control pin 18 state by pin 22 state
        if (GPIO.digitalRead(22) == GPIO.HIGH):
            #webiopi.sleep(0.1)
            GPIO.digitalWrite(18, GPIO.HIGH)
    
        if (GPIO.digitalRead(22) == GPIO.LOW):
            webiopi.sleep(0.01)
            if (GPIO.digitalRead(4) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(17) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(27) == GPIO.HIGH):
                webiopi.sleep(0.01)
            else:
                GPIO.digitalWrite(18, GPIO.LOW)
                webiopi.sleep(0.01)
    
    #Block to control pin 18 state by pin 27 state
        if (GPIO.digitalRead(27) == GPIO.HIGH):
            #webiopi.sleep(0.1)
            GPIO.digitalWrite(18, GPIO.HIGH)
    
        if (GPIO.digitalRead(27) == GPIO.LOW):
            webiopi.sleep(0.01)
            if (GPIO.digitalRead(4) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(17) == GPIO.HIGH):
                webiopi.sleep(0.01)
            elif (GPIO.digitalRead(22) == GPIO.HIGH):
                webiopi.sleep(0.01)
            else:
                GPIO.digitalWrite(18, GPIO.LOW)
                webiopi.sleep(0.01)
    

    这两个 python 脚本在 raspberry pi 和 webiopi 启动时启动,它为我提供了一个可以控制 LED 的 Web UI。

    这 3 样东西加起来就是我所追求的,以及一个 Web 界面,它可以根据每个 LED 的当前高或低状态实时更新按钮。

    这是一个概念验证,LED 将很快被继电器取代,继电器将打开或关闭我家不同房间中连接的扬声器组,Raspberry Pi 多房间音频控制器。树莓派也将成为连接区域的音频源,通过无线播放和蓝牙流式传输。

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多