Shop OBEX P1 Docs P2 Docs Learn Events
On Button Interface help — Parallax Forums

On Button Interface help

skynuggetskynugget Posts: 172
edited 2008-02-28 13:26 in BASIC Stamp
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.

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

  • allanlane5allanlane5 Posts: 3,815
    edited 2008-02-28 04:32
    I'd put a "PAUSE 1" in the loop, to make the loop time more determinant.

    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.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-28 07:22
    skynugget -

    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
  • skynuggetskynugget Posts: 172
    edited 2008-02-28 13:26
    hi bruce, you are correct, if IN4 = 0 (no press) it returns to the main routine.
Sign In or Register to comment.