Handling buttons is a challenge... I would never ever (sorry for the strong 'never ever') trust those things... You already use in your code concepts of too rapidly firing things and put a stop to it by defining a minimal time range for something - event - to be in the past for accepting the same thing - event -again. Handling touches or presses is a business of itself. Therefore, when I want to be safe to not have bouncing - even though debouncing is built-in - I enforce it myself.
The easiest is to not take the press but watch the release and watch it only once. Then do all the business I want to do and then - if I need the button again watched (to establish, for example, a toggle), I set the watch again, on release and only once again. With hat I make sure I do not create race conditions that you may have had... Bad thing about race conditions is that they can be so unpredictable that only with heave machinery they can be shown. If they can be prevented by logic, then let's do so.
Thanks for pointing that out.
But actually there were no issues with the buttons yet other than there was no response at all with the setHandler() part when the whole thing got stuck.
Do you see any issue in my code?
The freezes happened even when not pressing any button for a longer period.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Handling buttons is a challenge... I would never ever (sorry for the strong 'never ever') trust those things... You already use in your code concepts of too rapidly firing things and put a stop to it by defining a minimal time range for something - event - to be in the past for accepting the same thing - event -again. Handling touches or presses is a business of itself. Therefore, when I want to be safe to not have bouncing - even though debouncing is built-in - I enforce it myself.
The easiest is to not take the press but watch the release and watch it only once. Then do all the business I want to do and then - if I need the button again watched (to establish, for example, a toggle), I set the watch again, on release and only once again. With hat I make sure I do not create race conditions that you may have had... Bad thing about race conditions is that they can be so unpredictable that only with heave machinery they can be shown. If they can be prevented by logic, then let's do so.