Arduino debounce system + time management library
- Add TPush to your Arduino IDE libraries
- Include the library
#include <TPush.h>
- Initialize a component
TPush ButtonX;
#include <TPush.h>
TPush Button1;
void setup() {
// Button1 is active LOW
Button1.setUp(2, LOW);
Serial.begin(9600);
}
void loop() {
// minimum active time of 150ms.
if (Button1.Click(150)) {
// triggered once, till released
Serial.println("Button1 Pressed!");
}
// second param means another index
if (Button1.Click(500, 2)) {
Serial.println("Button1 Action 2 Pressed!");
}
}
Default wait-time is 10 milliseconds, enough for membranes, use 150ms for lamella buttons.
void loop() {
if (Button1.DoubleClick()) {
Serial.println("Button1 Double Click!");
}
}
int t = 0;
void loop() {
if (t = Button1.KeepHold(1000)) { // if ((t = Button1.KeepHold(1000)) > x)
Serial.print("Button1 pressed for ");
Serial.print(t);
Serial.println("ms");
}
}
Here t
is set each frame
if (t = Button1.KeepHold(100, true) > 1000) {
#include <TPush.h>
TPush TIMER;
void setup() {
// Nothing to do here
}
void loop() {
if (TIMER.Wait(150)) {
Serial.println("Once every 150ms");
}
if (TIMER.Wait(500, 2)) {
Serial.println("Use the second parameter for multiple separate timers, on the same object");
}
}
NB: to avoid waste of memory you can use Wait() on Buttons already declared instead of declaring timer objects. Use the second parameter as arrays index.
Avoid floating state useing a pull resistor.
If the buttons is ACTIVE LOW you can use internal pull-up resistance (not on pin 13). See documentation.