Shop OBEX P1 Docs P2 Docs Learn Events
Contact Bounce — Parallax Forums

Contact Bounce

bob kruggelbob kruggel Posts: 50
edited 2010-04-07 01:35 in Propeller 1
I·have searched this forum for a discussion for contact bounce on the propeller without success. Here is a snippet of code that is included in"The Propeller Education Kit Labs: Fundamentals".

PUB ButtonTime(pin) : delta | time1, time2
··· repeat until ina[noparse][[/noparse]pin] == 1
··· time1 := cnt
··· repeat until ina[noparse][[/noparse]pin] == 0
··· time2 := cnt
··· delta := time2 - time1

Why doesn't delta have a value that is a function of the switch bouncing between 0 volts and 3.3 volts? [noparse][[/noparse]pin] is connected to a normally open push button switch with one terminal tied to 3.3v and the other tied to a 10k resistor to gnd. (I don't know how to draw a schematic with the propeller tool yet). Is there some magic occuring in the propeller?

BobK

Comments

  • iQuitiQuit Posts: 78
    edited 2010-04-06 03:14
    I'm just starting out with Spin, but I'm pretty sure the line below repeat must be indented for it to work? Looks like the result in delta will be the time, in
    clock cycles, it takes for the switch to settle from one state to the other--high to low in this case.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
  • kwinnkwinn Posts: 8,697
    edited 2010-04-06 03:24
    This snippet measures how long the input pin was high. If the "delta" was greater than a threshold value it would be considered a "valid" key press. Typically that would be a count equivalent to a time period in the 10mS to 100mS range.

    repeat until ina[noparse][[/noparse]pin] == 1 - repeats this instruction until the input on the pin is high (3.3v)
    time1 := cnt - saves the current value in the free running counter "cnt" in "time1"
    repeat until ina[noparse][[/noparse]pin] == 0 - repeats this instruction until the input on the pin is low (0.0v)
    time2 := cnt - saves the current value in the free running counter "cnt" in "time2"
    delta := time2 - time1 - calculates the difference (how long the input pin was high) and stores it in "delta"
  • iQuitiQuit Posts: 78
    edited 2010-04-06 15:24
    How about the command: WAITPEQ
    I found it in the Prop Manual. I'm pretty sure that you could use fewer lines of code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
  • bob kruggelbob kruggel Posts: 50
    edited 2010-04-06 17:12
    The attached file describes the situation am I concerned about. View the file in the Propeller Tool to see the graphics.

    Bob Kruggel
  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-06 17:28
    Usually in dealing with contact bounce, you want to use the first signal transition to start a delay, then sample the input signal after the delay. If it's still the same, you return. If it's not, you begin the process over again. Spin code might look like:
    PRI debounce(pin {to be used}, state {when active}, delay {for debouncing})
       repeat
          if ina[noparse][[/noparse]pin] <> state   ' wait for active state
             next
          waitcnt(delay + cnt)   ' wait for specified delay
          if ina[noparse][[/noparse]pin] == state   ' still in active state?
             quit
    


    Note that you can debounce both the switch closing and opening.
  • bob kruggelbob kruggel Posts: 50
    edited 2010-04-07 01:23
    Mike,

    I understand the technique you talked about, and my question was related to the apparent lack of discussion of contact bounce in the forum and in the education courses. The code snippet certainly doesn't acknowledge contact bounce and the code snippet seems to work correctly. It's as if the code assumes a bounceless switch and gets away with it. That's why I asked if there is some magic in the propeller.

    BobK
  • localrogerlocalroger Posts: 3,452
    edited 2010-04-07 01:35
    bob,

    It is extremely common even among highly trained people to ignore contact bounce. I have seen multi-thousand-dollar industrial controls plagued with these issues because the guy behind the firmware didn't know better and there wasn't a team on board with someone to correct him. In practice, if your project is going to be very busy for a while after a keypress, you may be able to get away without explicitly debouncing contacts, but you might also find yourself receiving occasional random keypresses in the form of microsecond-duration noise. Most people just don't notice the latter problem at all or blow it off as user error.

    This isn't good, and I advise debouncing keystrokes if you're scanning keys.
Sign In or Register to comment.