Shop OBEX P1 Docs P2 Docs Learn Events
push button problem. — Parallax Forums

push button problem.

tercou1tercou1 Posts: 25
edited 2012-04-04 01:46 in BASIC Stamp
I'm using two push buttons, one for mode and one for reset. The problem i'm having is that every time I press the PB1 the data displayed is random, where as I want it to follow the sequence of IF statements below. The information displayed will eventually be the actual data and it will be sent to an LCD but for now I would love to get PB1 to be more consistant. If anybody has any ideas in regards to this problem, I'd really like to hear them. Thanks in advance guys.

'
[ I/O Definitions ]

PB1 PIN 13 'Receive Input from pushbutton1(mode)
PB2 PIN 12 'Receive Input from pushbutton2(reset)


'
[ Constants ]



Capture CON 500 ' Num of msec to listen for input


'
[ Variables ]


pulses VAR Word ' input pulses from flow sensor



'
[ Initialization ]
HIGH PB1
HIGH PB2


pulses = 0


'
[ main ]
main:
IF IN13 = 0 THEN mode
DEBUG CLS, "press top btn for mode OR bottom btn for reset ", DEC pulses, CR
PAUSE 500

IF IN12 = 0 THEN reset

GOTO main
'
[ subroutine for current flow rate ]
mode:
DO
COUNT PB1, Capture, pulses

IF pulses = 1 THEN
DEBUG CR, "current flow rate ", DEC pulses, CR
PAUSE 500
ENDIF
IF pulses = 2 THEN
DEBUG CR, "total consump ", DEC pulses, CR
PAUSE 500 'debounce
ENDIF
IF pulses = 3 THEN
DEBUG CR, "resettable consump ", DEC pulses, CR
PAUSE 500
ENDIF
IF pulses = 4 THEN
DEBUG CR, "freezing/normal ", DEC pulses, CR
PAUSE 500
ENDIF
IF pulses = 5 THEN
DEBUG CR, "leaking/ok ", DEC pulses, CR
PAUSE 500
ENDIF

PAUSE 1000


LOOP
GOTO main

reset:
DEBUG CR, "resettable code goes here ", DEC pulses, CR
GOTO main

Comments

  • tercou1tercou1 Posts: 25
    edited 2012-04-02 14:09
    I should point out that the PB's are active high.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-04-02 14:33
    Pushbuttons have a tendency to "bounce" when they make contact, and the COUNT command interprets the short electrical impulses as many events. Try putting a capacitor of 0.01 or 0.1µF in parallel with the pulldown resistor to smooth it out. Alternatively, there are ways to debounce in software, by requiring multiple samples of the input to agree.

    The SELECT CASE syntax might be good for your Mode function:
    SELECT pulses
       CASE 1
       CASE 2
       CASE 3
    ENDSELECT
    
  • tercou1tercou1 Posts: 25
    edited 2012-04-02 15:11
    Hi Tracy,

    I thought the pause 500 after each if statement would take care of the debounce, obviously not, thanks for the heads up on the select case syntax, I'll check it out.
  • tercou1tercou1 Posts: 25
    edited 2012-04-04 01:46
    Hi Tracy

    Thanks for the advice, it works like a charm now.. here's the code, let me know if I can make any improvements on it... thanks again =)

    '
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    ' =========================================================================
    '
    '
    '
    [ I/O Definitions ]

    PB1 PIN 13 'Receive Input from pushbutton1
    PB2 PIN 12 'Receive Input from pushbutton2
    tx PIN 8
    result PIN 1


    '
    [ Constants ]

    leaks CON 99
    freezing CON 98
    currentflow CON 97
    totalconsump CON 96
    consumpreset CON 95

    #SELECT $STAMP
    #CASE BS2, BS2E, BS2PE
    T2400 CON 396
    T9600 CON 84
    T19K2 CON 32
    #CASE BS2SX, BS2P
    T2400 CON 1021
    T9600 CON 240
    T19K2 CON 110
    #ENDSELECT

    LcdBaud CON T19K2

    LcdBkSpc CON $08 ' move cursor left
    LcdRt CON $09 ' move cursor right
    LcdLF CON $0A ' move cursor down 1 line
    LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
    LcdCR CON $0D ' move pos 0 of next line
    LcdBLon CON $11 ' backlight on
    LcdBLoff CON $12 ' backlight off
    LcdOff CON $15 ' LCD off
    LcdOn1 CON $16 ' LCD on; cursor off, blink off
    LcdOn2 CON $17 ' LCD on; cursor off, blink on
    LcdOn3 CON $18 ' LCD on; cursor on, blink off
    LcdOn4 CON $19 ' LCD on; cursor on, blink on
    LcdLine1 CON $80 ' move to line 1, column 0
    LcdLine2 CON $94 ' move to line 2, column 0

    LcdCC0 CON $F8 ' define custom char 0
    LcdCC1 CON $F9 ' define custom char 1
    LcdCC2 CON $FA ' define custom char 2
    LcdCC3 CON $FB ' define custom char 3
    LcdCC4 CON $FC ' define custom char 4
    LcdCC5 CON $FD ' define custom char 5
    LcdCC6 CON $FE ' define custom char 6
    LcdCC7 CON $FF ' define custom char 7
    Capture CON 500 ' Num of msec to listen for input


    '
    [ Variables ]

    State VAR Word
    pulses VAR Word ' input pulses from flow sensor



    '
    [ Initialization ]

    HIGH PB1
    HIGH PB2
    HIGH result
    HIGH 15
    State = leaks
    pulses = 0


    '
    [ main ]
    main:
    IF IN13 = 0 THEN mode
    DEBUG CLS, "press top btn for mode OR bottom btn for reset ", DEC pulses, CR
    PAUSE 500

    IF IN12 = 0 THEN reset

    GOTO main
    '
    [ subroutine for current flow rate ]
    mode:

    DO
    SELECT State
    CASE leaks 'pushbutton pressed
    IF IN13 = 0 THEN
    PAUSE 250 'DEBOUNCE
    IF OUT15 = 1 THEN
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["No Leaks"]
    DEBUG CLS, "No Leaks ", DEC pulses, CR
    ENDIF
    IF OUT15 = 0 THEN
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["ATTENTION!! Water is Leaking."]
    DEBUG CLS, "ATTENTION!! Water is Leaking. ", DEC pulses, CR
    ENDIF
    State = freezing
    ENDIF
    CASE freezing 'pushbutton pressed
    IF IN13 = 0 THEN
    PAUSE 250 'DEBOUNCE
    IF Result > 930 THEN
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["Pipes are going to freeze!!!"]
    DEBUG CLS, "Pipes are going to freeze!!! ", DEC pulses, CR
    ENDIF
    IF Result <= 930 THEN
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["Temperature of pipes are OK"]
    DEBUG CLS, "temperature of pipes are ok ", DEC pulses, CR
    ENDIF
    State = currentflow
    ENDIF
    CASE currentflow 'pushbutton pressed
    IF IN13 = 0 THEN
    PAUSE 250 'DEBOUNCE
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["Current flow is ???????"]
    DEBUG CLS, "current flow rate is ", DEC pulses, CR

    State = totalconsump
    ENDIF
    CASE totalconsump 'pushbutton pressed
    IF IN13 = 0 THEN
    PAUSE 250 'DEBOUNCE
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["Total Consumption is ???????"]
    DEBUG CLS, "Total consumption ", DEC pulses, CR

    State = consumpreset
    ENDIF
    CASE consumpreset 'pushbutton pressed
    IF IN13 = 0 THEN
    PAUSE 250 'DEBOUNCE
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, ["Ttl Consump snce lst rst is ??"]
    DEBUG CLS, "total consump since reset ", DEC pulses, CR

    State = leaks
    ENDIF
    ENDSELECT
    LOOP
    GOTO main

    reset:
    IF IN12 = 0 THEN
    PAUSE 250 'DEBOUNCE
    SEROUT TX, LcdBaud, [LcdBLoff, LcdOn1, LcdCls]
    PAUSE 250
    SEROUT TX, LcdBaud, [" rstable code goes here??"]
    DEBUG CR, "resettable code goes here ", DEC pulses, CR
    PAUSE 2000
    ENDIF
    GOTO main
Sign In or Register to comment.