The Noiasca Tool Kit to Debounce Buttons

The Noiasca Tool Kit contains several classes: It all starts with helpers to let LEDs blink, dim, flicker and several other effects in parallel without the usage of any delay. Furthermore the tool kit contains a class to read a button click very easily. Finally, the tool kit brings two different timers, which can be used to start and stop actions. For example if you want to start a blinking LED on button press and let it blink for 10 seconds, just combine a blink LED, the button and a timer from the Noiasca Tool Kit.

The Noiasca Tool Kit contains a simple class to debounce a push button - a momentary switch. The debouncing is based on the example 02. Digital / Debounce.

On this page only the button class is explained in detail, but you will find a lot of examples for LEDs and timer in the library examples.

Debounce a Button

This is a simple a class to "debounce" a button. If the button was pressed a debug message will be printed to the serial monitor. You can use this event to switch on a LED or increase a counter or call any other function you need on button press:

#include <Noiasca_button.h> // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

Button buttonA {A0}; // active LOW: button connects pin with GND

void setup() {
  Serial.begin(115200);
  buttonA.begin(); // you have to call the .begin() method
}

void loop() {
  if (buttonA.wasPressed() == true) // fire if button was pressed
  {
    Serial.println(F("button A was pressed")); // put here what ever you want to do when the button gets pressed
  }
}

 

The constructor in the example makes one debounced button available on pin A0. By default the library assumes that your button will close the defined pin to GND. Optional you can handover a second parameter. If you connect a permanent external pulldown resistor and your button closes to VCC, you need an "active HIGH" button:

Button buttonA {A0, HIGH}; // active HIGH: button connects pin with GND, external pull down resistor

These member functions can be used in the button class:

begin()
The begin() must be called in setup(). It will do all hardware specific tasks. In our example, it will set the pinMode to INPUT_PULLUP. Therefore the library expects, that you have connected the button to GND and A0.
wasPressed()
this member function will return true if your button was pressed. Use this whenever you want to read if a push button was pressed.
wasReleased()
this member function will return true if a former pressed button was released.
isPressed()
This is similar to a native "digitalRead" but the returned logic depends on the active status during creation of the the instance. Default is active LOW. This member function will return true if the button is closed to GND.
setOnPress()
assign a callback function for on press events.
setOnRelease()
assign a callback function for on release events.
update()
trigger the button to check if a callback function needs to be called.

Note

There are much more advanced "button" libraries available. The button class in the Noiasca Toolkit is only a very basic - but fully working - implementation and often used in other LED or timer examples of the library.

Links

(*) Disclosure: Some of the links above are affiliate links, meaning, at no additional cost to you I will earn a (little) comission if you click through and make a purchase. I only recommend products I own myself and I'm convinced they are useful for other makers.

History

First upload: 2022-02-02 | Version: 2024-03-22