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. 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.
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()