Shop OBEX P1 Docs P2 Docs Learn Events
Program: SW21-EX14-Debounce.BS2 Here is how you use each button by it self us — Parallax Forums

Program: SW21-EX14-Debounce.BS2 Here is how you use each button by it self us

sam_sam_samsam_sam_sam Posts: 2,286
edited 2009-09-07 01:37 in BASIC Stamp
[font=Tahoma,Bold size=2][font=Tahoma,Bold size=2]I do not have time to night to run this routine and play with now

·
[/font][/font]' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] Program Description ]
'
' This program demonstrates the simultaneous debouncing of multiple inputs.
' The input subroutine is easily adjusted to handle any number of inputs.
'
[noparse][[/noparse] I/O Definitions ]
BtnBus VAR INA ' four inputs, pins 0 - 3
'
[noparse][[/noparse] Variables ]
btns VAR Nib ' debounced inputs
idx VAR Nib ' loop counter
'
[noparse][[/noparse] Program Code ]
Main:
DO
GOSUB Get_Buttons ' get debounced inputs
DEBUG HOME,
"Inputs = ", IBIN4 btns ' display in binary mode
PAUSE 50
LOOP
'
[noparse][[/noparse] Subroutines ]
Get_Buttons:
btns = %1111 ' enable all four inputs
FOR idx = 1 TO 5
btns = btns & ~BtnBus ' test inputs
PAUSE 5 ' delay between tests
NEXT

RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 9/5/2009 4:44:39 PM GMT

Comments

  • $WMc%$WMc% Posts: 1,884
    edited 2009-09-03 03:33
    sam_sam_sam

    This is a good Question that I have wondered about for a few years.

    I was never brave enough to ask it !

    ___________$WMc%_________

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there············································ BoogerWoods, FL. USA
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-05 16:31
    ·I had some time to Play with this Routine and here is what I came up with

    ' =========================================================================
    '
    '·· File...... SW21-EX14-Debounce.BS2
    '·· Purpose... SCANNING AND DEBOUNCING MULTIPLE INPUTS
    '·· Author.... Parallax Stamp Works EXPERIMENT #14
    '·· E-mail....
    '·· Started...
    '·· Updated...
    '
    '·· {$STAMP BS2}
    '·· {$PBASIC 2.5}
    '
    ' =========================================================================

    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program demonstrates the simultaneous debouncing of multiple inputs.
    ' The input subroutine is easily adjusted to handle any number of inputs.
    '
    '
    [noparse][[/noparse] Revision History ]

    '
    [noparse][[/noparse] I/O Definitions ]
    '
    BtnBus······· VAR········· INB·········· ' four inputs,··· pins 4 - 8
    Led·········· VAR········· OUTA········· ' four outputs,· pins 0 - 3
    '
    [noparse][[/noparse] Constants ]

    '
    [noparse][[/noparse] Variables ]
    btns····· VAR······ Nib··········· ' debounced inputs
    idx······ VAR······ Nib··········· ' loop counter
    '
    [noparse][[/noparse] EEPROM Data ]

    '
    [noparse][[/noparse] Initialization ]

    DIRS = %1111111100001111·········· ' This is a must or this will not work

    '
    [noparse][[/noparse] Program Code ]
    Main:

    DO
    GOSUB Get_Buttons············· ' get debounced inputs

    IF btns = %0001 THEN·········· ' You can not use HIGH OR LOW command it has to be done with BIT 0 or 1
    led = %0001·························' This turns ON your LED Light········

    ENDIF·································

    DEBUG HOME,
    "Inputs = ", IBIN4 btns······· ' display in binary mode
    PAUSE 5
    LOOP

    '
    [noparse][[/noparse] Subroutines ]
    Get_Buttons:

    btns = %1111 ' enable all four inputs

    FOR idx = 1 TO 5

    btns = btns & ~BtnBus ' test inputs

    PAUSE 5 ' delay between tests

    NEXT

    led = %0000················ ' This turns OFF the LED Light

    RETURN


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/5/2009 6:48:40 PM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-05 16:38
    ' =========================================================================
    '
    '·· File...... SW21-EX14-De-bounce Part 2.BS2
    '·· Purpose... SCANNING AND DE-BOUNCING MULTIPLE INPUTS
    '·· Author.... Parallax Stamp Works EXPERIMENT #14
    '·· E-mail....
    '·· Started...
    '·· Updated...
    '
    '·· {$STAMP BS2}
    '·· {$PBASIC 2.5}
    '
    ' =========================================================================


    '
    [noparse][[/noparse] Program Description ]
    '
    ' This program demonstrates the simultaneous debouncing of multiple inputs.
    ' The input subroutine is easily adjusted to handle any number of inputs.
    '
    '
    [noparse][[/noparse] Revision History ]


    '
    [noparse][[/noparse] I/O Definitions ]
    '
    BtnBus······· VAR········· INB·········· ' four inputs, pins 4 - 8
    '
    '
    [noparse][[/noparse] Constants ]


    '
    [noparse][[/noparse] Variables ]
    xbtns····· VAR······ Nib
    nbtns····· VAR······ Nib··········· ' debounced inputs
    idx······· VAR······ Nib··········· ' loop counter
    oBtns····· VAR······ Nib
    '
    [noparse][[/noparse] EEPROM Data ]


    '
    [noparse][[/noparse] Initialization ]


    '
    [noparse][[/noparse] Program Code ]
    Main:
    DO
    GOSUB Get_Buttons············· ' get debounced inputs
    DEBUG HOME,
    "Inputs = ", IBIN4 xbtns······· ' display in binary mode
    PAUSE 5

    IF xBtns = %0001 THEN····· ' This stay TRUE for one seconds then go False this is the only different from the one above
    HIGH 0
    ELSE
    IF xBtns = %0000 THEN
    LOW 0


    ENDIF
    ENDIF
    LOOP


    '
    [noparse][[/noparse] Subroutines ]

    ·Get_Buttons:

    nBtns = %1111 ' enable all four inputs

    FOR idx = 1 TO 5
    nBtns = nBtns & ~BtnBus ' test new inputs
    PAUSE 50 ' delay between tests

    NEXT
    xBtns = nBtns ^ oBtns & nBtns ' look for 0 -> 1 changes····· This is different from the one above
    oBtns = nBtns ' save this scan


    RETURN




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/5/2009 4:49:46 PM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-05 16:51
    Here is both routines

    There was a two·little bug in this routine code it has been fix

    ·SCANNING AND DEBOUNCING MULTIPLE INPUT.BS2

    I will have a use for this routine when I to scan 4 input and if more than one input go high then do some routine

    I have just got a PDB a week ago and thought I would try this routine out·· SCANNING AND DEBOUNCING MULTIPLE INPUTS

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/5/2009 10:07:17 PM GMT
  • skylightskylight Posts: 1,915
    edited 2009-09-05 19:09
    sam-sam-sam wrote:

    IF Btns = %0001 THEN ' Here is the answer to this question that I ask earlier How ever if you do this Btns = %0101 THEN both button
    HIGH 0 ' need to be pushed at the same time for the statement to be TRUE
    ELSE


    Wonder if anyone else thought Matrix Keypad Decoder at this point? cool.gif

    Strange that the section above disappears when trying to quote the post it was in, so i just copied and pasted the paragraph,
    possible forum bug?
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-05 20:35
    skylight

    Strange that the section above disappears when trying to quote the post it was in, so i just copied and pasted the paragraph,
    possible forum bug?



    There was a little bug in this routine code it has been fix

    SCANNING AND DEBOUNCING MULTIPLE INPUT.BS2

    I was editing my post at that time and you have not lost it

    The problem with what I had posted first was that if you pushed two button one and· another button the led would stay on and that was·not what I was looking for with what is posted now if you try pushing two buttons at the same time the led goes·OFF or dose not come·ON at all
    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/5/2009 8:59:55 PM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-05 20:47
    skylight

    Wonder if anyone else thought Matrix Keypad Decoder at this point? cool.gif

    I think that if you used the SCANNING AND DEBOUNCING MULTIPLE INPUT·4 TO 8·*.BS2 that
    I posted that should work for this Matrix Keypad Decoder


    Because in that code routine the Led stay·ON for one second and then goes OFF when the right button is pushed

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/5/2009 10:21:10 PM GMT
  • $WMc%$WMc% Posts: 1,884
    edited 2009-09-06 04:40
    sam_sam_sam

    I think the Button/Btns command is worthless, It needs to much code to make it work.

    Just My thoughts


    ________$WMc%____________

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Truth is out there············································ BoogerWoods, FL. USA
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-06 17:28
    $WMc%
    ·
    I think the Button/Btns command is worthless, It needs to much code to make it work.

    This routine is really good if you need to compare more than one input to another other wise I have to agree with on that it take a lot of coding
    other wide to do one input


    The time that I send learning how to do·it was well worth the time I·spend doing because
    I was going do a Project that would entail compare two inputs and would take a lot of coding
    to make sure that both input are 1 before the routine would start and would exit the routine if one or the other input· was 0 this would be need
    for safety reasons




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-09-06 18:47
    I am sure there are many ways to accomplish button debouncing.
    Here is what I have cobbled together.
    It can monitor all 16 inputs if necessary.
    It detects positive going pulse - from 0 to 1.
    It can be modified for negative pulses.
    It has programmable debounce timer - in 1 ms increments.

    Basically a change detect loop followed by a delay loop.

    Just keep in mind that we are trying to filter out erroneous pulses in 5 to 30 ms range.
    So the overall delay should reflect that.



    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    counter VAR Word
    debounce VAR Word
    testVariable VAR Word
    debounce = 20

    Main:
    testvariable = 1
    DO
    · DO
    ··· DEBUG BIN16 ? testvariable
    · LOOP UNTIL (testvariable > 0)
    · DEBUG "Input detected - high",CR
    · ' debounce delay
    · DO
    ··· counter = counter + 1
    ··· PAUSE 1
    ··· DEBUG ? counter, CR
    ··· PAUSE 200···· ' test delay
    ··· IF· ((counter = debounce) AND testvariable > 0) THEN
    ····· DEBUG "process subroutine",CR
    ····· GOSUB sub
    ··· ELSEIF (testvariable = 0) THEN
    ····· counter = 0 ' ready for next one
    ····· DEBUG "return to Main",CR
    ····· GOTO Main
    ··· ENDIF
    · LOOP
    · testVariable = testVariable + 1
    LOOP
    sub:
    · DEBUG "subroutine",CR
    · DEBUG BIN16 ? testvariable
    · END
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-07 01:37
    vaclav_sal

    Thank You for sharing you code routine I will give it a try

    I am sure there are many ways to accomplish button·DE·bouncing.

    I do not always use· (switch buttons to compare inputs) so I·do not alway need to·DE bouncing the inputs

    So I was looking at making·a routine that could be used for both type of inputs switches and just power to the inputs

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 9/7/2009 1:49:43 AM GMT
Sign In or Register to comment.