Re: [PIC}Discriminating between button presses
Neil <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <[email protected]> |
Actually yes... the release after a specific time threshold is not what determines a long click, but the timer threshold elapsing. I found >2 seconds for long press worked well. I had also wondered back then about the non-intuitive release being the point of action vs the press, but that's the way most of the world works nowadays... on a PC and even on devices. There's no way to tell on press if it will be a short vs long press. For double-clicks... when the button is released to register a short-press, then set (and look for) a first-click flag... the first time, the flag will be clear, so that release counts as short press. Start a timer. On a release again, if that flag is set, then it's a double-click. Another timer would be counting that double-click time and if it expires, set the short-press flag, clear the first-click flag, and stop counting. I guess you could get fancy and say that after first-click, if the second "press" (not release) occurs within a specific time then it's a double-click. But I think resetting and cleaning up variables to tell it to ignore the rest of that existing press would get messy. And unnecessary IMO, as this all happens too quickly for the user to notice anything odd. FWIW, when I did this on STM32's, I had the interrupt run quicker (~ 20 times/sec) and used more bits (of the 32-bit registers). I don't remember it being any better in terms of user feel. Cheers, -Neil On 12/28/2024 1:41 PM, David C Brown wrote: > That is almost exactly the algorithm that I have come up with, with the > addition that if there is no release after three seconds a long press is > recorded > I find it unsatisfactory because the action occurs when the button is > released which is counterintuitive. > Users expect pressing, not releasing, a button to do something. > > You have now set me the challenge of doing double clicks. After a short > click wait half a second to see if there is another press? > __________________________________________ > David C Brown > 43 Bings Road > Whaley Bridge > High Peak Phone: 01663 733236 > Derbyshire eMail: [email protected] > SK23 7ND web: www.bings-knowle.co.uk/dcb > <http://www.jb.man.ac.uk/~dcb> > > > > *Sent from my etch-a-sketch* > > > On Sat, 28 Dec 2024 at 18:08, Neil <[email protected]> wrote: > >> I did this for a product several years ago using 8-bit PICs and can >> distinguish between short presses, long presses, and double-clicks (<-- >> this was most challenging). It also included debouncing. Worked very >> well over several thousands of units. >> >> I thought of it "graphically": an interrupt about 10 times/sec would >> left shift a register and capture the button state into the LSB. So the >> bits of that register really looked like a logic analyzer trace. >> On each interrupt I'd look for a pattern using a mask... if the button >> was 0 on press, I would look for 0x___11110 which means that the button >> was just pressed. If there was bounce, then the 1111 part would include >> 0's, would would only register the press after the bounces stabilized. >> The amount of 1's depends on how bouncy the button is, but less if more >> responsive. >> >> When the press occurs I start counting time... actually just reset a >> counter that another timer interrupt was counting. >> The prior interrupt also looked for release ( 0x_____01). The amount of >> time it took to get the release determined if it was a short vs long >> press. Set flag for short and long-presses accordingly to let main code >> process it. >> >> FWIW, a short press just set a flag and another time-counter and if >> second short-press was received within a specific time, then that was a >> double-click, else the time-counter generated the click event and flags. >> There was some tricky code to do this part, so I can't remember the >> low-level details on it currently. >> >> Cheers, >> -Neil. >> >> >> >> On 12/28/2024 12:15 PM, David C Brown wrote: >>> Using mid range 16bit PICs I am attempting to write an algorithm that can >>> discriminate between short - less than 1 second - and long - greater >> than 3 >>> second - button presses. >>> I have written some thing which works but I am not entirely satisfied >> with >>> it and would appreciate and guidance/suggestion >>> __________________________________________ >>> David C Brown >>> 43 Bings Road >>> Whaley Bridge >>> High Peak Phone: 01663 733236 >>> Derbyshire eMail: [email protected] >>> SK23 7ND web: www.bings-knowle.co.uk/dcb >>> <http://www.jb.man.ac.uk/~dcb> >>> >>> >>> >>> *Sent from my etch-a-sketch* >> -- >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >> View/change your membership options at >> https://mailman.mit.edu/mailman/listinfo/piclist >>