Re: [PIC}Discriminating between button presses
Neil <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.pic |
|---|---|
| Message-ID | <[email protected]> |
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*