Accelerometer always produces two interrupts [SOLVED]
When I use the accelerometer on my WiPy3 and Pytrack to produce an interrupt, it always produces at least two interrupts in a row even when I just bump it in one direction. I assume this comes from the acceleration and then deceleration of the board. Does anyone know how to produce just one accelerometer interrupt? My thoughts were either to (1) only allow and acceleration (+) to produce an interrupt or (2) to clear the queue of interrupts once one has been detected - but I don't know how to achieve either.
Here is the interrupts code I'm working with:
from pytrack import Pytrack from LIS2HH12 import LIS2HH12 import pycom import time import machine import utime py = Pytrack() pycom.heartbeat(False) py.setup_int_pin_wake_up(False) py.setup_int_wake_up(True, True) int_counter = 0 def accel_activity_handler(pin_o): pycom.rgbled(0x800080) print('saw the interrupt') global int_counter int_counter+=1 print(int_counter) pycom.heartbeat(False) ## ACCELEROMETER INTERRUPT acc = LIS2HH12() acc.enable_activity_interrupt(1000, 200, handler=accel_activity_handler)
And the output from one bump of the board producing two interrupts:
>>> saw the interrupt 1 saw the interrupt 2
@dmayorquin Amazing! Both of those solutions worked for me. Thanks for your help :)
self.int_pin.callback(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=self._int_handler)
which detects both rising and falling edge of the interruption. If you check the code of the function begining at line #161:
def _int_handler(self, pin_o):
you will see that the state of pin_o is evaluated (True for activity and False for inactivity). So, one solution is to modify your handler in main.py:
def accel_activity_handler(pin_o): if pin_o(): pycom.rgbled(0x800080) print('saw the interrupt') global int_counter int_counter+=1 print(int_counter) time.sleep(1) pycom.rgbled(0)
Another solution is to change the line #148 in LIS2HH12.py so it rises just one interruption related to activity detection:
@dmayorquin Great tip, I didn't see that!
However, when I changed that line there was no change in the board behaviour (still two interrupts)...
Even when I tried
(and resetting the board to clear past handlers) there were still two interrupts...Thoughts?
Hi @alexpul, you are activating the activity and inactivity interrupts of the accelerometer:
According to your acc.enable_activity_interrupt() settings, the Pytrack generates an interruption after 200 ms of inactivity and another interruption if the motion goes beyond 1000 mg.
If you just want to detect the motion, use:
This enables activity detection only. Hope this works.