time_pulse_us triggered 5 times after timeout. Why? #16045
-
I am using the When a timeout occurs, Why does What would be a better way to detect when a digital signal times-out which does not block or consume much processor? def sense2_signal(x):
if (machine.time_pulse_us(sense2,1,2000000)) == -1:
Yellow()
sense2.irq(sense2_signal,machine.Pin.IRQ_RISING) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You did not tell which port you use. In your code you set up a Pin irq to call sense2_signal(). It is a soft irq, so the Pin.irq is sheduled for every rising slope that occurs and sense2_signal may be called several times. If the input signal can have several fast transitions like they are created from a bouncing switch, one can get a burst of IRQ calls. I would set up two irq handlers, the Pin IRQ for detecting input transitions and a timer irq to detect the timeout, which just measures the time between a start time and the actual time. The Pin irq would reset the start time to the actual time. Time sampling can be done with time.ticks_ms() and time.ticks_diff(). The frequency of the timer IRQ is up to the required precision, from a fee ms up. That way nothing is blocking an Pin irq bursts will not harm. Edit: And of course you can use asyncio. |
Beta Was this translation helpful? Give feedback.
You did not tell which port you use.
In your code you set up a Pin irq to call sense2_signal(). It is a soft irq, so the Pin.irq is sheduled for every rising slope that occurs and sense2_signal may be called several times. If the input signal can have several fast transitions like they are created from a bouncing switch, one can get a burst of IRQ calls.
Also, you mentioned it, having the IRQ handler to block for 2 seconds is not good.
I would set up two irq handlers, the Pin IRQ for detecting input transitions and a timer irq to detect the timeout, which just measures the time between a start time and the actual time. The Pin irq would reset the start time to the actual time. Time sampli…