Skip to content

State not correctly initialized for INPUT_PULLUP button #96

@navdotnetreqs

Description

@navdotnetreqs

Hi,

I have a panic stop button that is set to INPUT_PULLUP. So the pressed state is LOW.

I noticed that when I boot the board (Teensy) I get a false trigger that the button is pressed for the first update cycle.
The internal state-variable has weird values for three loops, after this the state stabilizes and it works as it should.

in setup()

  PanicBtn.attach(PANIC_BTN_PIN, INPUT_PULLUP);
  PanicBtn.interval(DEBOUCE_INTERVAL);
  PanicBtn.setPressedState(LOW);

  for (int i=0;i<10;i++){
        PanicBtn.update();
        Serial.printf("Loop %i: pressed: %i, ispressed: %i, state: %i \n", i, PanicBtn.pressed(), PanicBtn.isPressed(), PanicBtn.state);
        delay(100);
  }
  • 09:11:00.593 -> Loop 0: pressed: 0, ispressed: 1, state: 0
  • 09:11:00.609 -> Loop 1: pressed: 0, ispressed: 1, state: 2
  • 09:11:00.703 -> Loop 2: pressed: 0, ispressed: 0, state: 7
  • 09:11:00.799 -> Loop 3: pressed: 0, ispressed: 0, state: 3
  • 09:11:00.895 -> Loop 4: pressed: 0, ispressed: 0, state: 3
  • 09:11:00.993 -> Loop 5: pressed: 0, ispressed: 0, state: 3
  • 09:11:01.088 -> Loop 6: pressed: 0, ispressed: 0, state: 3
  • 09:11:01.218 -> Loop 7: pressed: 0, ispressed: 0, state: 3
  • 09:11:01.314 -> Loop 8: pressed: 0, ispressed: 0, state: 3
  • 09:11:01.409 -> Loop 9: pressed: 0, ispressed: 0, state: 3

With a limit switch pulled to ground the same loop goes

  • 09:10:00.298 -> Loop 0: pressed: 0, ispressed: 0, state: 0
  • 09:10:00.441 -> Loop 1: pressed: 0, ispressed: 0, state: 0
  • 09:10:00.490 -> Loop 2: pressed: 0, ispressed: 0, state: 0
  • 09:10:00.588 -> Loop 3: pressed: 0, ispressed: 0, state: 0

It seems to me the initial value of the protected Debouncer variable state should be initialized in setPressedState depending on if it's a LOW or HIGH actuated press.

As a workaround I changed the state to public, and set PanicBtn.state = 3 after setPressedState.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions