Giter VIP home page Giter VIP logo

buttondebounce's Introduction

Button Debounce

Pushbuttons often generate improper open/close transitions when pressed, due to mechanical and physical issues: these transitions may be read as multiple presses in a very short time fooling the program, without debouncing, pressing the button once may cause unpredictable results.

This library will sample the button repeatedly; and if it sees the button in its new state for 8 consequtive checks (equally spaced within 'delay' milli seconds) -- will report the state as changed.

Usage

SETUP

Declare a ButtonDebouce object with pinNumber and timeMillis to debounce as parameter, and defined a callback function.

Example:

void onButtonChange(const int state) {
  Serial.println("Changed: " + String(state));
}
ButtonDebounce button(3, 250); // PIN 3 with 250ms debounce time
button.setCallback(onButtonChange);

Or - in a more asynchroneous style:

void onButtonChange(const int state) {
  Serial.println("Changed: " + String(state));
}
ButtonDebounce button(3, 250); // PIN 3 with 250ms debounce time
button.setCallback([](const int state) {
  Serial.println("Changed: " + String(state));
});

Important: buttons don´t share resources. The following methods are avaliable.

  • update() called in loop method, used to verify the debounce timer and the button state changes.
  • state() return the last button state.
  • setCallback(function) set the callback function that will executed on button state changes.

Note that the constructor also sets the INPUT_PULLUP pinmode() on the pin.

LOOP

Due the time buffer used in each button, please call the update() method in loop, so it can be update and reflect all state changes.

Be advised: Do not declare any delay() before the update functions, as it may interfere with the bounce time buffer.
void loop() {
  buttonUp.update();
  buttonDown.update();
}

Contributors

Thank you for all contributions:

buttondebounce's People

Contributors

abaskin avatar dirkx avatar maykon avatar per1234 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.