Code Monkey home page Code Monkey logo

button-debounce's Introduction

Button Debounce Snippet

A button debounce snippet for Arduino device.

Button bounce happens when you push a tactile button. In a short period, the button may close and open serveral times. But since the clock of microcontroller is fast enough to capture the "noise", we need to get rid of unused peaks. Here is a figure showing what happen in the timeline.

button bounce

How to used debounce code

  1. copy over button_debounce_filter.h and button_debounce_filter.cpp
  2. start your program from the below example.
  3. add your customzed button state logic code.
#include "button_debounce_filter.h"


const int buttonPin = 2;
ButtonDebounceFilter buttonDebounce;

void setup() {
  // inital ButtonDebounceFilter with buttonPin
  buttonDebounce.begin(buttonPin);
  buttonDebounce.setDebounceInterval(50);

  Serial.begin(9600);
}

void loop() {

  buttonDebounce.monitorButtonState();

  bool is_high;
  bool is_low;
  buttonDebounce.isReliableHighLow(is_high, is_low);

  if (is_high) {
    // add your logic for button high
    Serial.println("button turn high");
  }

  if (is_low) {
    // add your logic for button low
    Serial.println("button turn low");
  }

  buttonDebounce.updateButtonState();
}

What is done behind the scene

The intuitive idea of debounce a button is to ignore the former few jumps and take the button state only when it is reliable. In my code:

  1. monitor button voltage change, monitorButtonState
  2. postone button voltage read until reliable isReliableHighLow
  3. update button voltage, so next loop you can correctly recognize change updateButtonState

Reference:

The code is a simple wrap on the code in these two post, but I found easier and cleaner to reuse later. Hope you will enjoy it.

License

The codes are under MIT LICENSE

button-debounce's People

Contributors

shixudongleo avatar

Watchers

 avatar  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.