按特定顺序触发 PIR 传感器

发布于 2025-01-18 21:32:51 字数 491 浏览 1 评论 0原文

在我的设计中,我有两个连接到 Raspberry Pi 的 PIR 传感器,一个用于门口的两侧。

我需要帮助,了解一旦传感器按特定顺序触发,我该如何做某事。

为了简单起见,我们将传感器称为 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”。

任何有关此问题的帮助将不胜感激。

I have two PIR Sensors connected to a Raspberry Pi one used for either side of a doorway in my design.

I am needing help on how I can do something once the sensors are triggered in a particular order.

Lets call the sensors for simplicity A and B.

Since they will be used for room count I would like the count to increase when they are triggered in order AB and decrease when triggered in order BA.

I have tried this code earlier but it failed (I understand there is no roomCount implemented here but I was testing for expected output):

While True:
    if GPIO.input(pir1):
        if GPIO.input(pir2):
            print(“AB”)
   
    if GPIO.input(pir2):
        if GPIO.input(pir1):
            print(“BA”)

This code was constantly giving the output “AB” no matter which order they were triggered.

Any help on this issue would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2025-01-25 21:32:52

您可以尝试这样的事情:

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

希望会有所帮助。

You can try something like this:

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

Hope that helps.

若水微香 2025-01-25 21:32:51

我认为您可能会做得更好,更像是这样的伪代码:

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)    

I think you might do better with code more like this pseudo-code:

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)    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文