On Button Interface help
skynugget
Posts: 172
hey all,
i have been using this code for a one button interface that does something different on tap and hold. it works, but it seems a little hacky. it runs a counter while the button is pressed then acts accordingly based on hold time. i'm also wanting to add another condition where if i hold it really long it will do something.
can anyone help me clean this up?
thanks in advance!
i have been using this code for a one button interface that does something different on tap and hold. it works, but it seems a little hacky. it runs a counter while the button is pressed then acts accordingly based on hold time. i'm also wanting to add another condition where if i hold it really long it will do something.
CheckButton: IF IN4=1 THEN 'beep on button press GOSUB speak ELSE DO WHILE (IN4=1) AND holdcnt < 250 'count button hold time holdcnt = holdcnt + 1 LOOP ENDIF IF holdcnt > 200 THEN 'if held for a bit then do holdcnt = 0 GOSUB Cancel ELSE IF holdcnt > 0 THEN if tapped or held less then holdtime then do holdcnt = 0 SpkPattern = 1 GOSUB speak GOSUB advance ENDIF ENDIF RETURN
can anyone help me clean this up?
thanks in advance!
Comments
Then I'd realize that most switches have a "debounce" time, of like 5 to 10 mSec. So, the first time it went 'down', I'd ignore the state for 10 mSec, then read it at mSec 11 and see if it was still 1. If so, I'd continue looping through for about 100 mSec -- Longer than 10 mSec but less than 100 mSec would be a 'tap', more than 1/10 second would be a 'press'.
You MIGHT have to use "less than 1/4 second" for a 'tap' -- 1/10 second is a pretty short time for humans.
Unless I'm reading the program logic incorrectly, it appears that the routines are presumptive for the IN4=1 case. What is supposed to happen if the button IS NOT pressed? Is it just supposed to RETURN to the calling routine? That's what it seems to do.
BTW - My best advice is to flowchart the logic in a program with many varied conditions.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"Genius is one percent inspiration and ninety-nine percent perspiration."
Thomas Alva Edison