python - 如何将两个Python脚本Pi3 GPIO组合在一起

标签 python raspberry-pi3 gpio

我需要使用 Python 编码的帮助。

我有一个树莓派3。我有两个执行不同功能的脚本,但我希望它们能够协同工作。

第一个驱动一个 PIR 传感器,并以 LED 作为输出。当 pir 变高时,它开始倒计时,有足够的时间再次检测到人。在此期间,如果没有检测到任何内容,LED 就会熄灭。

第二个驱动 LDR 传感器。它读取 LDR 传感器的变化值并打开或关闭 LED。我已经为这些设置了所有接线。

主要问题是如何将这两个脚本组合在一起,以便让 PIR 传感器等到天黑(来自 LDR 的值)才开始驱动 LED 在检测到/未检测到人时打开或关闭。这只是为了关闭 PIR 传感器,以便在白天不打开 LED。

顺便说一下,在这个单独的配置中,我只有一个 Pir 传感器和一个 LED,但我只想使用一个 Python 代码和一个 LDR 作为全局光传感器来管理 4 个 Pir 传感器和 4 个 LED。 例如,所有的 PIR 传感器都会等到天黑才开始作为输入工作,并且当天黑时,每个 PIR 传感器都可以控制特定的 LED

pir1=led1,pir2=led2,pir3=led3,pir4=led4

这是 PIR 传感器和 LED 的代码:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN) #pir sensor put as input
GPIO.setup(25, GPIO.OUT) # led put as output
GPIO.output(25, GPIO.LOW)

delay = 10 # set number of seconds delay before light turns off

while True:
#wait for pir to trigger.
print "waiting "
while GPIO.input(21) == 0:
time.sleep (0.5)

print "turn light on here"
GPIO.output(25, GPIO.HIGH)
count = 0

#start count down to turn off
print "count down started "
while count < delay:
count = count + 1

# here if the input goes high again we reset the counter to 0
if GPIO.input(21) == 1:
count = 0

print "count down now ", (delay - count)
time.sleep(1)

print "Turn light off"
GPIO.output(25, GPIO.LOW)

ldr 传感器和 LED 的代码在哪里:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
delayt = .1
value = 0 # this variable will be used to store the ldr value
ldr = 12 #ldr is connected with pin number 12
led = 25 #led is connected with pin number 25

GPIO.output(led, False) # keep led off by default
def rc_time (ldr):
count = 0

#Output on the pin for
GPIO.setup(ldr, GPIO.OUT)
GPIO.output(ldr, False)
time.sleep(delayt)

#Change the pin back to input
GPIO.setup(ldr, GPIO.IN)

#Count until the pin goes high
while (GPIO.input(ldr) == 0):
count += 1

return count

#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print("Ldr Value:")
value = rc_time(ldr)
print(value)
if ( value >= 70):
print("It is dark turn on led")
GPIO.output(led, True)
if (value < 69):
print("It is light turn off led")
GPIO.output(led, False)

except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()

非常感谢任何帮助。请记住,我真的是 Python 编码菜鸟。 我所有的工作都是通过反复试验来完成的。

最佳答案

我认为下面的代码可以工作...我没有尝试,因为我无法测试它,但请尝试一下。

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21, GPIO.IN) #pir sensor put as input
GPIO.setup(25, GPIO.OUT) # led put as output
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, False)
GPIO.output(25, False) # keep led off by default

delayt = .1
ldr = 12 #ldr is connected with pin number 12
led = 25 #led is connected with pin number 25
delay = 10 # set number of seconds delay before light turns off

def is_dark():
  global ldr, led, delayt

  count = 0
  #Output on the pin for
  GPIO.setup(ldr, GPIO.OUT)
  GPIO.output(ldr, False)
  time.sleep(delayt)

  #Change the pin back to input
  GPIO.setup(ldr, GPIO.IN)

  #Count until the pin goes high
  while (GPIO.input(ldr) == 0):
    count += 1

  if count >= 70:
    return True

  return False

def has_someone():
  if GPIO.input(21) == 1:
    return True

  return False


def main():
  global delay

  while True:
    if has_someone() and is_dark():
      print "turn light on here"
      GPIO.output(25, GPIO.HIGH)
      count = 0
      while count < delay:
        count = count + 1

        # here if the input goes high again we reset the counter to 0
        if has_someone() == 1:
          count = 0

        print "count down now ", (delay - count)
        time.sleep(1)

      print "Turn light off"
      GPIO.output(25, GPIO.LOW)


if __name__ == "__main__":
  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    GPIO.cleanup()


关于python - 如何将两个Python脚本Pi3 GPIO组合在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54788108/

相关文章:

c++ - 如何在 QtCreator 上使用带有 highGUI 的 OpenCV?

linux-device-driver - 可变大小的 i2c 读取 Raspberry

python - 带有威胁回调的树莓派 RPi.GPIO 错误

python - 如何使用flask_msearch配置诸如模糊性之类的参数?

python - Matplotlib - 如何为一系列绘图设置 ylim()?

python - 单击Python文件时读取错误消息

python - 无法在 PyCharm 上安装 googleapiclient

python - 来自导入类的对象的 cloudpickle 与在发生 pickle 的同一模块中定义的类

c - 使用 writel 将 4 位写入 ioremap 内存地址

python - 是否可以同时从 Python 脚本和 C++ 程序访问 GPIO 引脚?