Debouncing a Push Button
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.
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
IF (pb1 = 0) AND (Flag = 1) THEN
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
' {$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