Trying to time a button press


I’m trying to detect when a button (connected to a GPIO input) is held closed for a certain period of time, like a full second. I can create an interrupt handler that is called when the button is pressed or released, but reviewing the API docs, and my testing, indicates that I can not assign a different handler to an interrupt trigger by a press then one triggered by a release.

I could use the ISR to set a global flag when the button is pressed, and have a time going that periodically checks the value of the GPIO pin to see if the button is released and then compare times, but that seems rather awkward. Is there a better way?


I’m trying to time the duration between a rising and falling edge. As far as I can tell from the documentation, there is no enabling START/STOP/RESET functionality for the timers. So I’m doing it the hard way and counting system uptime with mgos_uptime_micros(void). Watching this thread with interest in case I’ve missed something.


That’s how I ended up doing it as well. If I find a better approach I’ll post it.