#define LED_PIN 21
#include "KTS_Button.h"
KTS_Button buttonArray[] = { 7, 6, 5, 4 };
const byte NUM_BUTTONS = sizeof(buttonArray) / sizeof(buttonArray[0]);
KTS_Button singleButtons(3);
void DoThisSingle(void* data){Serial.println((*(int*)data)+66);}
void DoThisDouble(void* data){Serial.println("It seems that, if nothing else, this double callback worked!");}
void DoThisTriple(void* data){Serial.println("It seems that, if nothing else, this triple callback worked!");}
void DoThisLong(void* data){Serial.println("It seems that, if nothing else, this long callback worked!");}
void setup() {
Serial.begin(9600);
for (int i = 0; i < NUM_BUTTONS; ++i) {
buttonArray[i].setButtonFunction(SINGLE_PRESS, &DoThisSingle);
buttonArray[i].setButtonFunction(DOUBLE_PRESS, &DoThisDouble);
buttonArray[i].setButtonFunction(TRIPLE_PRESS, &DoThisTriple);
buttonArray[i].setButtonFunction(LONG_PRESS, &DoThisLong);
}
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Possible RETURN values are NOTHING, SINGLE_PRESS, LONG_PRESS, DOUBLE_PRESS, TRIPLE_PRESS
// Debouncing is built-in
// Read() will return an instant (after debouncing) SINGLE_PRESS on button release,
// or a LONG_PRESS if pushed and held for a timeout.
// Read_MultiPress() There is a small wait window while the presses are being counted.
// After the multiPressTimeout ends, it returns SINGLE_PRESS / DOUBLE_PRESS / TRIPLE_PRESS.
// The price for this is a small delay before the button responds (not a literally delay() call, just a wait time)
// You can change this manually for example: SetMultiPressTimeout(150);
// Try and get it as small as possible (i.e requiring faster double presses);
// LONG_PRESS is achieved by holding it for a timeout as it does with Read().
// You can hook a void function (currently one which takes no paramters) to a button
// by declaring the function and using button.setButtonFunction(&Function);
// Then call it like: if (button.read() == SINGLE_PRESS) button.execute();
int x = 68;
// There are several ways to read the buttons. In an array, handling each press type like this:
for (int i = 0; i < NUM_BUTTONS; i++) {
// switch (buttonArray[i].handlePress()) { // This read can do single/long press
buttonArray[i].handleMultiPress((void*)&i);
}
// Or you can read individual buttons like this:
if (singleButtons.handlePress((void*)int(0)) == SINGLE_PRESS) {
Serial.println("Lonely Button Clicked");
}
// Test for non-blocking code
static unsigned long lastCapture;
if (millis() - lastCapture >= 250) {
lastCapture = millis();
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}