[英]How do I pause a script in python to keep 2 sensors from being detected at same time?
I have this function in a python script that detects 2 vibrations sensors, the problem is the sensors are very sensitive so usually when one is hit they are both detected which gives me a false reading. 我在python脚本中具有此功能,该脚本可检测2个振动传感器,问题在于传感器非常敏感,因此通常当一个传感器被击中时,它们都会被检测到,这给了我错误的读数。 How would I stop them both from being detected at the same time?
如何阻止他们同时被发现? I want to detect whichever was first.
我想检测哪个是第一个。 This is what I have tried -
这是我尝试过的-
#!/usr/bin/env python
import RPi.GPIO as GPIO
from time import sleep
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ShockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def shock():
print('shock')
def knock():
print('knock')
def register_callbacks():
if GPIO.add_event_detect(ShockPin, GPIO.FALLING, callback=shock, bouncetime=5000):
sleep(5)
elif GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=knock, bouncetime=5000):
sleep(5)
if __name__ == '__main__':
try:
setup()
register_callbacks()
Just a suggestion, I don't have the setup to test it. 只是一个建议,我没有安装程序可以对其进行测试。 Save the time of the last event (using datetime ), and then check if the last event is more than 5 seconds ago.
保存最后一个事件的时间(使用datetime ),然后检查最后一个事件是否在5秒钟前。
import datetime
sensor_delay = 5 #delay in seconds
last_event = datetime.datetime.now()
def shock():
global last_event
if datetime.datetime.now() > last_event + datetime.timedelta(seconds=sensor_delay):
print ('shock')
last_event = datetime.datetime.now()
def knock():
global last_event
if datetime.datetime.now() > last_event + datetime.timedelta(seconds=sensor_delay):
print('knock')
last_event = datetime.datetime.now()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.