首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按特定顺序触发的PIR传感器

按特定顺序触发的PIR传感器
EN

Stack Overflow用户
提问于 2022-04-04 20:34:13
回答 2查看 63关注 0票数 1

在我的设计中,我有两个PIR传感器连接到一个树莓Pi,一个用于门道的两边。

我需要帮助,一旦感应器以特定的顺序触发,我如何才能做一些事情。

为了简单起见,让我们调用传感器A和B。

由于它们将用于房间计数,我希望当它们按AB顺序触发时会增加,当按BA顺序触发时会减少。

我之前尝试过这段代码,但失败了(我知道这里没有实现roomCount,但我正在测试预期的输出):

代码语言:javascript
复制
While True:
    if GPIO.input(pir1):
        if GPIO.input(pir2):
            print(“AB”)
   
    if GPIO.input(pir2):
        if GPIO.input(pir1):
            print(“BA”)

这段代码不断地给出输出“AB”,不管它们被触发的顺序是什么。

如能在这一问题上提供任何帮助,将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-06 21:55:33

我认为您可能会更好地处理代码,更像这个伪代码:

代码语言:javascript
复制
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)    
票数 1
EN

Stack Overflow用户

发布于 2022-04-04 21:06:30

你可以试试这样的东西:

代码语言:javascript
复制
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

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71743436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档