在我的设计中,我有两个PIR传感器连接到一个树莓Pi,一个用于门道的两边。
我需要帮助,一旦感应器以特定的顺序触发,我如何才能做一些事情。
为了简单起见,让我们调用传感器A和B。
由于它们将用于房间计数,我希望当它们按AB顺序触发时会增加,当按BA顺序触发时会减少。
我之前尝试过这段代码,但失败了(我知道这里没有实现roomCount,但我正在测试预期的输出):
While True:
if GPIO.input(pir1):
if GPIO.input(pir2):
print(“AB”)
if GPIO.input(pir2):
if GPIO.input(pir1):
print(“BA”)这段代码不断地给出输出“AB”,不管它们被触发的顺序是什么。
如能在这一问题上提供任何帮助,将不胜感激。
发布于 2022-04-06 21:55:33
我认为您可能会更好地处理代码,更像这个伪代码:
import time
maxWait = 2 # Longest time (in seconds) to wait for other PIR to trigger
while True:
if A is triggered:
endTime = time.time() + maxWait
while time.time() < endTime:
if B is triggered:
print("AB")
# Sleep till the person has fully passed both sensors
while A is triggered or B is triggered:
sleep(0.1)
if B is triggered:
endTime = time.time() + maxWait
while time.time() < endTime:
if A is triggered:
print("BA")
while A is triggered or B is triggered:
sleep(0.1) 发布于 2022-04-04 21:06:30
你可以试试这样的东西:
condition = "" #create empty string
counter=0 #create counter :)
while True:
if GPIO.input(pir1):
condition+="A" #add char to it
if GPIO.input(pir2):
condition+="B" #add another char to it
if "AB" in condition:
counter += 1
condition="" #reset the string
elif "BA" in condition:
counter -= 1
condition="" #reset the string希望这能有所帮助。
https://stackoverflow.com/questions/71743436
复制相似问题