mathertel / OneButton

An Arduino library for using a single button for multiple purpose input.
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
Other
954 stars 230 forks source link

Add isPressed function to support latching switch. #134

Closed alw1746 closed 10 months ago

alw1746 commented 10 months ago

Support latching switch usage with isPressed function:

In OneButton.h,

bool isPressed() const { return _state == OCS_DOWN; }

In loop(),


button.tick();
if (button.isPressed()) {            //switch latched on
  if (!ledOn) {
    digitalWrite(LED_BUILTIN,HIGH);
    ledOn=true;
  }
}
else if (button.isIdle()) {           //latch released
  if (ledOn) {
    digitalWrite(LED_BUILTIN,LOW);
    ledOn=false;
  }
}
mathertel commented 10 months ago

You can use the public isIdle() function for this:

if (! button.isIdle()) {
  // on
} else {
  // off
}