Shop OBEX P1 Docs P2 Docs Learn Events
Switch debounce — Parallax Forums

Switch debounce

ArchiverArchiver Posts: 46,084
edited 2002-04-04 01:05 in General Discussion
Could someone please give me a suggestion why the following program
doesn't work..

I am trying to write a program to debounce a switch. I thought I would
like have the stamp check the switch & if it found it closed then check
it 50 more times and if it was still closed after that time then state
it is closed and vice versa for it being open. Here's my program. Yep,
you guessed right, it doesn't work.


Thanks for your help..

Leroy

'{$STAMP BS2}
x var byte
x0 var bit
x1 var bit
x2 var bit
pin1 con 1


loop:

if pin1 = 1 then one
if pin1 = 0 then zero
debug cr

DEBUG "the pin status is: ", bin1 x1

goto loop


zero:
x1=1
for x = 1 to 50
if pin1 = 1 then loop
x1=0
next
return

one:
x0=0
for x = 1 to 50
if pin1 = 0 then loop
x1=1
next
return

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-04-03 20:39
    Leroy,

    Two thoughts without knowing what your failure is.
    1) In your debug statement you print x1 but in your one: routine you
    set both x0 and x1. What is x0 used for?

    2) I would be worried about the number of times that you break out
    of a subroutine without a return when you detect a changed value at
    the pin. (Others more knowledgeable can tell you if this is a real
    problem.) You might try the following:

    zero:
    x1=1
    for x = 1 to 50
    if pin1 = 1 then zero_one
    next
    x1=0
    zero_one:
    return

    one:
    x1=0
    for x = 1 to 50
    if pin1 = 0 then one_zero
    next
    x1=1
    one_zero:
    return


    ( * * * Untested code, but compiles OK. * * * )

    Phil

    --- In basicstamps@y..., Leroy Hall <leroy@f...> wrote:
    > Could someone please give me a suggestion why the following program
    > doesn't work..
    >
    > I am trying to write a program to debounce a switch. I thought I
    would
    > like have the stamp check the switch & if it found it closed then
    check
    > it 50 more times and if it was still closed after that time then
    state
    > it is closed and vice versa for it being open. Here's my program.
    Yep,
    > you guessed right, it doesn't work.
    >
    >
    > Thanks for your help..
    >
    > Leroy
    >
    > '{$STAMP BS2}
    > x var byte
    > x0 var bit
    > x1 var bit
    > x2 var bit
    > pin1 con 1
    >
    >
    > loop:
    >
    > if pin1 = 1 then one
    > if pin1 = 0 then zero
    > debug cr
    >
    > DEBUG "the pin status is: ", bin1 x1
    >
    > goto loop
    >
    >
    > zero:
    > x1=1
    > for x = 1 to 50
    > if pin1 = 1 then loop
    > x1=0
    > next
    > return
    >
    > one:
    > x0=0
    > for x = 1 to 50
    > if pin1 = 0 then loop
    > x1=1
    > next
    > return
  • ArchiverArchiver Posts: 46,084
    edited 2002-04-03 22:15
    Hi Leroy,

    You have pin1 defined as a constant=1, so it is always going to be
    one. I think what you want is an alias variable:
    pin1 var in1 ' another name for the input pin 1
    Also, "zero:" and "one:" are subroutines with returns at the end,
    but the lines that go there ("if pin1=1 then one") are plain branch
    commands, not subroutine calls. You may find the actual BRANCH
    command useful:
    BRANCH in1,[noparse][[/noparse]zero,one]


    My bias is to implement the debounce function with math only and no
    branching. Here is one way:

    x var byte
    loop:
    x=x max 49 min 1 + (in1<<1-1)
    x1 = x1 | (x min 49 - 49) & (x max 1) max 1
    debug "count=",dec x,tab, "the pin status=",bin x1,cr
    goto loop


    It is kind of funny math, but it makes sense if you think it through.
    The first line increments the count x up as high as 50 or decrements
    back to zero 0, depending on the state of the input. Think about the
    term in parentheses:
    (0<<1-1) if in1=0, then () equals -1 so x decrements
    (1<<1-1) if in1=1, then () equals +1 so x increments
    The "<<1" means shift left one, which is equivalent to mulitply by two.
    The next line starting with "x1=" works on the debounced status bit
    x1. x1 appears on the right hand side of the expression, so x1 holds
    its value unless something happens in the terms in parentheses. The
    first term in parentheses, (x min 49 - 49), is zero until x reaches
    50, at which time it flips the status output to x1=1. The "|" is
    logical OR. The second term in parentheses (x max 1) is one unless
    x=0, so when x gets back to zero, the output status flips back to
    x1=0. The "&" is the logical AND operator.


    -- regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...





    >Could someone please give me a suggestion why the following program
    >doesn't work..
    >
    >I am trying to write a program to debounce a switch. I thought I would
    >like have the stamp check the switch & if it found it closed then check
    >it 50 more times and if it was still closed after that time then state
    >it is closed and vice versa for it being open. Here's my program. Yep,
    >you guessed right, it doesn't work.
    >
    >
    >Thanks for your help..
    >
    >Leroy
    >
    >'{$STAMP BS2}
    >x var byte
    >x0 var bit
    >x1 var bit
    >x2 var bit
    >pin1 con 1
    >
    >
    >loop:
    >
    >if pin1 = 1 then one
    >if pin1 = 0 then zero
    >debug cr
    >
    >DEBUG "the pin status is: ", bin1 x1
    >
    >goto loop
    >
    >
    >zero:
    >x1=1
    >for x = 1 to 50
    >if pin1 = 1 then loop
    >x1=0
    >next
    >return
    >
    >one:
    >x0=0
    >for x = 1 to 50
    >if pin1 = 0 then loop
    >x1=1
    >next
    >return
  • ArchiverArchiver Posts: 46,084
    edited 2002-04-04 00:07
    Have you looked at the Button command
    It is setup for Debounce Button Input


    Ken Kaufman
    Senior Engineer
    Vertex/RSI
    Phone: 903-295-1480 Ext 270
    Fax: 903-295-1479
    E-Mail: ken.kaufman@t...


    Original Message
    From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=1w6cwsZICXAJOIZFZ9GT3aBH02o7C3pFt4zR6v9vJIO8l05CApe5JRWB4hUSIEezkOrkdgSmRPOzPQ0]tracy@e...[/url
    Sent: Wednesday, April 03, 2002 3:16 PM
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] Switch debounce


    Hi Leroy,

    You have pin1 defined as a constant=1, so it is always going to be
    one. I think what you want is an alias variable:
    pin1 var in1 ' another name for the input pin 1
    Also, "zero:" and "one:" are subroutines with returns at the end,
    but the lines that go there ("if pin1=1 then one") are plain branch
    commands, not subroutine calls. You may find the actual BRANCH
    command useful:
    BRANCH in1,[noparse][[/noparse]zero,one]


    My bias is to implement the debounce function with math only and no
    branching. Here is one way:

    x var byte
    loop:
    x=x max 49 min 1 + (in1<<1-1)
    x1 = x1 | (x min 49 - 49) & (x max 1) max 1
    debug "count=",dec x,tab, "the pin status=",bin x1,cr
    goto loop


    It is kind of funny math, but it makes sense if you think it through.
    The first line increments the count x up as high as 50 or decrements
    back to zero 0, depending on the state of the input. Think about the
    term in parentheses:
    (0<<1-1) if in1=0, then () equals -1 so x decrements
    (1<<1-1) if in1=1, then () equals +1 so x increments
    The "<<1" means shift left one, which is equivalent to mulitply by two.
    The next line starting with "x1=" works on the debounced status bit
    x1. x1 appears on the right hand side of the expression, so x1 holds
    its value unless something happens in the terms in parentheses. The
    first term in parentheses, (x min 49 - 49), is zero until x reaches
    50, at which time it flips the status output to x1=1. The "|" is
    logical OR. The second term in parentheses (x max 1) is one unless
    x=0, so when x gets back to zero, the output status flips back to
    x1=0. The "&" is the logical AND operator.


    -- regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...





    >Could someone please give me a suggestion why the following program
    >doesn't work..
    >
    >I am trying to write a program to debounce a switch. I thought I would
    >like have the stamp check the switch & if it found it closed then check
    >it 50 more times and if it was still closed after that time then state
    >it is closed and vice versa for it being open. Here's my program. Yep,
    >you guessed right, it doesn't work.
    >
    >
    >Thanks for your help..
    >
    >Leroy
    >
    >'{$STAMP BS2}
    >x var byte
    >x0 var bit
    >x1 var bit
    >x2 var bit
    >pin1 con 1
    >
    >
    >loop:
    >
    >if pin1 = 1 then one
    >if pin1 = 0 then zero
    >debug cr
    >
    >DEBUG "the pin status is: ", bin1 x1
    >
    >goto loop
    >
    >
    >zero:
    >x1=1
    >for x = 1 to 50
    >if pin1 = 1 then loop
    >x1=0
    >next
    >return
    >
    >one:
    >x0=0
    >for x = 1 to 50
    >if pin1 = 0 then loop
    >x1=1
    >next
    >return

    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-04-04 01:05
    Leroy,

    I think that this little piece of code will accomplish what you
    are looking for.

    Larry

    P.S. This is "payback" for your help! ;-)
    ============================================================

    TBit VAR Bit
    INX VAR Byte
    Loop:
    TBit=IN1 XOR 1
    FOR INX=1 TO 50
    IF IN1=TBit THEN Loop
    NEXT
    DEBUG DEBUG "the pin status is: ", IN1, CR

    ==========================================================
    Explanation:

    Using the XOR operator will set TBit to the opposite of IN1;
    i.e.,
    IN1=1, TBit=0; IN1=0, TBit=1. If the value of IN1 changes within
    the FOR loop we will restart the FOR loop and test for the "new"
    IN1
    value. We will stay in this looping situation until we see a
    constant
    value for IN1 for 50 iterations.



    >
    Original Message
    > From: Leroy Hall [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=M-0TwHr5rpVUaNo7Q0IKvg9kCzxLaNSVUMe4S-ctioL4mXdB1JaCOpldr2bMRKEPr0nEzg]leroy@f...[/url
    > Sent: Wednesday, April 03, 2002 11:46 AM
    > To: basicstamps@yahoogroups.com; Leroy Hall
    > Subject: [noparse][[/noparse]basicstamps] Switch debounce
    >
    >
    > Could someone please give me a suggestion why the
    > following program
    > doesn't work..
    >
    > I am trying to write a program to debounce a switch. I
    > thought I would
    > like have the stamp check the switch & if it found it
    > closed then check
    > it 50 more times and if it was still closed after that
    > time then state
    > it is closed and vice versa for it being open. Here's
    > my program. Yep,
    > you guessed right, it doesn't work.
    >
    >
    > Thanks for your help..
    >
    > Leroy
    >
    > '{$STAMP BS2}
    > x var byte
    > x0 var bit
    > x1 var bit
    > x2 var bit
    > pin1 con 1
    >
    >
    > loop:
    >
    > if pin1 = 1 then one
    > if pin1 = 0 then zero
    > debug cr
    >
    > DEBUG "the pin status is: ", bin1 x1
    >
    > goto loop
    >
    >
    > zero:
    > x1=1
    > for x = 1 to 50
    > if pin1 = 1 then loop
    > x1=0
    > next
    > return
    >
    > one:
    > x0=0
    > for x = 1 to 50
    > if pin1 = 0 then loop
    > x1=1
    > next
    > return
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text
    > in the Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/
    > loop:
    >
    > if pin1 = 1 then one
    > if pin1 = 0 then zero
    > debug cr
    >
    > DEBUG "the pin status is: ", bin1 x1
    >
    > goto loop
    >
    >
    > zero:
    > x1=1
    > for x = 1 to 50
    > if pin1 = 1 then loop
    > x1=0
    > next
    > return
    >
    > one:
    > x0=0
    > for x = 1 to 50
    > if pin1 = 0 then loop
    > x1=1
    > next
    > return
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text
    > in the Subject and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/
Sign In or Register to comment.