当用户将双手放在 PIR 传感器上时,我无法对 2 个 PIR 传感器的逻辑进行编程以在控制台中打印消息。我已经成功地使用 GPIO、GND 将 PIR 传感器连接到树莓派,并且5v端口。我目前拥有的代码确实会在有人挥手时在控制台中打印出一条消息,但我很难修改代码以在有人在两个 PIR 传感器上挥手时打印出错误消息。
我们可以使用 GP4 和 GP17 从传感器读取输入
这是我在运行代码时收到的错误消息。
Traceback (most recent call last):
File "peter.py", line 22, in <module>
if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):
NameError: name 'TRUE' is not defined
这是代码
import RPi.GPIO as GPIO
import time
sensor = 4
sensor2 = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)
previous_state = False
current_state = False
previous_state2 = False
current_state2 = False
while True:
time.sleep(0.1)
previous_state = current_state
previous_state2 = current_state2
current_state = GPIO.input(sensor)
current_state2 = GPIO.input(sensor2)
if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):
new_state = "HIGH" if current_state else "LOW"
new_state2 = "HIGH" if current_state2 else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))
程序非常简单。 Raspberry Pi GPIO 引脚允许我们使用引脚 4 作为输入;然后它可以检测 PIR 模块何时发送电源。该引脚不断检查是否有任何更改,为此使用 while True 循环。这是一个无限循环,因此程序将连续运行,除非我们使用 Ctrl + C 手动停止它。然后使用两个 bool 变量(True 或 False)表示引脚的先前状态和当前状态,先前状态就是当前状态循环之前的时间
最佳答案
在 Python 中使用“True”,而不是“TRUE”:
if True:
print("True")
else:
print("False")
同时改变:
if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):
收件人:
if current_state2==True and current_state != previous_state2 and previous_state==False:
关于android - 名称错误 : name 'TRUE' is not defined PIR sensors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382525/