问题描述
感谢您阅读本文。我正在尝试编写一个 Python 脚本来计算 PIR 传感器在给定时间间隔(1 分钟)内跳闸的次数。我的问题是,无论 PIR 跳闸多少次,并且我的循环花费的时间比我分配的时间间隔长,我的代码只在每个循环中将计数添加 +1。非常感谢任何建议。
from collections import Counter
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
counter=0
while True:
i=GPIO.input(11)
if i==True:
counter = counter + 1
print(counter)
time.sleep(60)
------解决方案----- 如果将来有人遇到同样的问题,这是我的解决方案:
from collections import Counter
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.IN)
while True: #this will create an infinite loop BUT the next while argument will make each loop interval 60 seconds * 1 minute
counter=0
t_end = time.time() + 60 * 1 #60 seconds * 1
while time.time() < t_end:
i=GPIO.input(11)
if i==True:
counter = counter + 1
print(counter)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)