Shop OBEX P1 Docs P2 Docs Learn Events
Debouncing a Push Button — Parallax Forums

Debouncing a Push Button

DmageeDmagee Posts: 20
edited 2012-01-07 09:24 in BASIC Stamp
I don't want to use a PAUSE to debounce the pb so I'm using a flag routine. Counting up the flag debounce works but counting down the debounce did not work. What am I doing wrong?

Thanks.

' {$STAMP BS2}
' {$PBASIC 2.5}

D_Set            VAR   Word
Flag             VAR   Bit
MoveTo           CON   2
ClrRt            CON   11
pb1              PIN   2
pb2              PIN   3
D_SET = 70
DO

DEBUG MoveTo, 0, 1
DEBUG "Defalt Setpoint... "
  DEBUG DEC2 (D_Set ), CR
GOSUB PB
LOOP
PB:
IF (pb2 = 0) THEN
Flag = 1
ENDIF
IF (pb2 = 0) AND (Flag = 1) THEN
D_Set = D_Set - 1
Flag = 0
ENDIF
IF (pb1 = 0) THEN
Flag = 1
ENDIF
IF (pb1 = 1) AND (Flag = 1) THEN
D_Set = D_Set + 1
Flag = 0
ENDIF
RETURN
 
 

Comments

  • ZootZoot Posts: 2,227
    edited 2012-01-05 20:39
    Shouldn't this be:
    IF (pb1 = 0) AND (Flag = 1) THEN
    
  • DmageeDmagee Posts: 20
    edited 2012-01-05 20:54
    Thanks, but that did not work.
  • RiJoRiRiJoRi Posts: 157
    edited 2012-01-06 06:18
    First, add comments, so you -- and others -- can see what you are trying to do. Guaranteed you'll come to this bit of code in a few months and say, "What the HECK was I doing??" :)

    Second, I'd add more constants: "IF (pb2 = PRESSED) THEN" is more comprehensible than "IF (pb2 = 0) THEN"

    Third, it looks like you are using two inputs to read one switch? Or two switches? (This is where comments are invaluable.)

    --Rich
  • DmageeDmagee Posts: 20
    edited 2012-01-06 07:17
    Ok I got the program to work, I just needed to add a "Flag2 CON 0". Now every thing works.


      
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    D_Set            VAR   Word                 ' Defalt Setpoint
    Flag             VAR   Bit                  ' Flag that button was pressed
    Flag2            VAR   Bit                  ' Flag that button was pressed
    MoveTo           CON   2                    ' Curser position
    PRESSED          CON   0                    ' Push Button state
    pb1              PIN   2                    ' Push Button 0ne
    pb2              PIN   3                    ' Push Button two
    D_SET = 70                                  ' Defalt Seting
    DO
    
    DEBUG MoveTo, 0, 1
    DEBUG "Defalt Setpoint... "
      DEBUG DEC2 (D_Set ), CR
    GOSUB PB
    LOOP
    PB:
    IF (pb2 = PRESSED) THEN                     ' pb2 pressed
    Flag2 = 1                                   ' Flag2 set
    ENDIF
    IF (pb2 = 1) AND (Flag2 = 1) THEN           ' pb not pressed
    D_Set = D_Set - 1                           ' True -1
    Flag2 = 0                                   ' Reset Flag2
    ENDIF
    IF (pb1 = PRESSED) THEN                     ' pb1 pressed
    Flag = 1                                    ' Flag1 set
    ENDIF
    IF (pb1 = 1) AND (Flag = 1) THEN            ' pb not pressed
    D_Set = D_Set + 1                           ' True +1
    Flag = 0                                    ' Reset Flag2
    ENDIF
    RETURN
    
    
  • DmageeDmagee Posts: 20
    edited 2012-01-06 07:26
    Sorry, "Flag2 VAR Bit" not "Flag2 CON 0".
  • ZootZoot Posts: 2,227
    edited 2012-01-07 09:24
    Glad it worked out.
Sign In or Register to comment.