Search HoldMyTwist

Browse all posts

Episode 37

The Button That Wouldn't Settle

The current raw signal is what the switch reports. The accepted signal is only changed if the raw signal is consistent for 20ms.

Episodes

Computing Basics

    Duck starts pressing physical button 1 (coral button inside monitor) At 0 milliseconds raw signal starts to go up (accepted level and number of presses is still zero)
    Magnified schematic for bouncing contacts. Raw signal changes at 0, 5, 8, 13 and 16ms. Accept level still 0.
    Robot points out that there are 3 rising edges in the raw signal diagram. Crossed out 3 possible bell rings. Press count accepted is still zero.
    On every raw change start a timer. When the raw level stays the same at least for 20 milliseconds, accept this level. Then increment press count only if the accepted level changes from 0->1.
    5ms = Zero on Raw, timer resets. Robot prevents Duck from ringing bell early.
    8, 13, and 16 ms changes all reset the timer. Last change sets raw level to 1 and accepted level to 0.
    Duck pauses and leaves bellhammer in the air. 35ms. 19ms since 16ms in raw signal.
    Robot stops the hammer.
    Accepted goes up to one. Duck rings bell once.
    Duck presses button for 50 milliseconds. Raw and accepted levels are still 1. Bell is silent. Count is 1.
    Duck releases the button. Resets the timer on raw changes at 60, 64, 67 milliseconds. Accepted level still at one. Press counter still at one.
    87 milliseconds becomes the point at which the stable release is accepted as level zero. The press count is still one. No bell.
    Duck makes new press at hundred millis. At 120 the stable rise is accepted, bell rings once. Count becomes two.
    INITIAL RAW:0; ACCEPTED:0; COUNT:0. At 10 milliseconds the button signal goes high. At 18 milliseconds the button signal goes low. At 21 milliseconds the button signal goes high again. It remains high from that point on.
    Duck compares the checkpoints of forty/forty-one with the last change at twenty-one.
    40 milliseconds; 19 stable milliseconds. Still zero accepted level and count.
    41 millisecond: After not changing for 20 milliseconds since 21 the raw level is still high. Accepted level goes up to 1. The bell rings once. Press count is now 1.
    Duck pours a cup. Robot points to the 20ms rule for this fake device.

    Episode transcript

    One press, many changes

    Duck starts pressing physical button 1 (coral button inside monitor) At 0 milliseconds raw signal starts to go up (accepted level and number of presses is still zero)

    Diagram values: RAW:0 → 1; ACCEPTED:0; COUNT:0

    Duck: The tea bell should ring once when you press.

    Magnified schematic for bouncing contacts. Raw signal changes at 0, 5, 8, 13 and 16ms. Accept level still 0.

    Diagram values: 0:1; 5:0; 8:1; 13:0; 16:1

    Duck: Why did it go through that many changes in one press?

    Robot points out that there are 3 rising edges in the raw signal diagram. Crossed out 3 possible bell rings. Press count accepted is still zero.

    Diagram values: NAIVE RISES:3; ACCEPTED PRESSES:0

    Robot: The raw signal itself shifted throughout a press.

    On every raw change start a timer. When the raw level stays the same at least for 20 milliseconds, accept this level. Then increment press count only if the accepted level changes from 0->1.

    Diagram values: STABLE ≥20 ms; RESET TIMER ON CHANGE; COUNT ACCEPTED 0→1

    Robot: Raw changes reset the timer.

    Restart the wait

    5ms = Zero on Raw, timer resets. Robot prevents Duck from ringing bell early.

    Diagram values: 5 ms:0; TIMER=0

    Duck: Raw changes reset the timer.

    8, 13, and 16 ms changes all reset the timer. Last change sets raw level to 1 and accepted level to 0.

    Diagram values: 8:1 RESET; 13:0 RESET; 16:1 RESET

    Robot: Each wobble resets waiting time.

    Duck pauses and leaves bellhammer in the air. 35ms. 19ms since 16ms in raw signal.

    Diagram values: 35 − 16 = 19 ms

    Duck: We need 20.

    Robot stops the hammer.

    Diagram values: 19 < 20; COUNT:0

    Robot: Only 19ms went by. We need 20.

    Accepted goes up to one. Duck rings bell once.

    Diagram values: 36 − 16 = 20 ms; ACCEPTED:1; COUNT:1

    Duck: 36: only one bell-ring.

    Hold, release, press

    Duck presses button for 50 milliseconds. Raw and accepted levels are still 1. Bell is silent. Count is 1.

    Diagram values: 50 ms:1; COUNT:1

    Duck: Still got it! Need a top up on the tea?

    Duck releases the button. Resets the timer on raw changes at 60, 64, 67 milliseconds. Accepted level still at one. Press counter still at one.

    Diagram values: 60:0; 64:1; 67:0

    Robot: Level holding isn't 2nd accepted press.

    87 milliseconds becomes the point at which the stable release is accepted as level zero. The press count is still one. No bell.

    Diagram values: 87 − 67 = 20 ms; ACCEPTED:0; COUNT:1

    Robot: The press count is still one.

    Duck makes new press at hundred millis. At 120 the stable rise is accepted, bell rings once. Count becomes two.

    Diagram values: 100:1; 120:ACCEPT1; COUNT:2

    Duck: The next stable press makes it two.

    The boundary test

    At 10 milliseconds the button signal goes high. At 18 milliseconds the button signal goes low. At 21 milliseconds the button signal goes high again. It remains high from that point on.

    Diagram values: INITIAL RAW:0; ACCEPTED:0; COUNT:0; 10:1; 18:0; 21:1; NO LATER CHANGES

    Robot: New attempt. 0 when the counter starts.

    Duck compares the checkpoints of forty/forty-one with the last change at twenty-one.

    Diagram values: 40 ms?; 41 ms?

    Duck: How many are there at forty msec? How many at forty-one?

    40 milliseconds; 19 stable milliseconds. Still zero accepted level and count.

    Diagram values: 40 − 21 = 19; COUNT:0

    Robot: Zero count.

    41 millisecond: After not changing for 20 milliseconds since 21 the raw level is still high. Accepted level goes up to 1. The bell rings once. Press count is now 1.

    Diagram values: 41 − 21 = 20; COUNT:1

    Duck: Count one.

    Duck pours a cup. Robot points to the 20ms rule for this fake device.

    Diagram values: THIS DEVICE:20 ms

    Robot: 20 ms, in this case. It may vary for real switches.

    The lesson

    Mechanical button contacts might make more than one fast raw change on a press or release. So let's say our imaginary device begins with raw level=0, accepted level=0 and press count=0. On every raw change start a timer. When the raw level stays the same at least for 20 milliseconds, accept this level. Then increment press count only if the accepted level changes from 0->1. It's not repeating on level=1 holding. And don't increment press count on accepting release.

    Try it

    New device start up. Its level zero raw. Its level zero accepted. Its press count is zero. Raw level starts to increase at 10 ms. Drop at 29 ms. Starts to increase again at 60 ms and stays high. When the raw level stays the same at least for 20 milliseconds, accept this level. What is level accepted at 50 and 80 ms? What is its press count at 50 and 80 ms?

    Answer

    The first high level pulse for 19ms is never accepted. The accepted level and press count is 0 at 50ms. The last high level starts at 60ms and at 80ms has been at the high level for 20ms now. Accepted level is 1 and press count is 1.

    Sources

    Arduino Debounce example (opens in a new tab)