I'm working on a toy that will have a bunch of inputs. Right now I'm testing it out with buttons. What I'd like to do is detect the following:
Button 1:
Single click - Event A
Double click - Event B
Button 2:
Single click - Event C
Double click - Event D
Hold both buttons for 2 seconds: Event E
I've been looking at various libraries and examples and found a really nice one linked here on the forum:
It's very well documented and uses a fast, efficient debouncing method.
The author had been very helpful in answering questions, but when I looked to see what he'd done recently, there was nothing - and no updates on his Github either. Sadly, a Google search informed me he'd passed away from cancer a few years ago.
Sorry to bring that news.
Anyway, I can get buttons 1 and 2 to detect single and double clicks on their own, but I haven't been able to figure out how to get the hold feature working with them. Here is the code so far:
#define KEY1_PIN A2
#define KEY2_PIN A3
#define LEDA_PIN 8
#define LEDB_PIN 13
#define LEDC_PIN 10
#define BLINK_TIME 300
#define REACTION_TIME 500
#include "SwitchPack.h"
DoubleClick key1(KEY1_PIN, PULLUP, REACTION_TIME);
DoubleClick key2(KEY2_PIN, PULLUP, REACTION_TIME);
//setup==========
void setup() {
key1.begin();
key1.setSensitivity(16);
key2.begin();
key1.setSensitivity(16);
pinMode(LEDA_PIN, OUTPUT);
pinMode(LEDB_PIN, OUTPUT);
pinMode(LEDC_PIN, OUTPUT);
}
//loop=======================
void loop() {
int event;
event = key1.clickCount();
// This section causes issues
if (key1.closed() && key2.closed()) {
event = 3;}
switch (event) {
case 1:
clickEvent1();
break;
case 2:
doubleClickEvent1();
break;
case 3:
doubleHoldEvent();
break;
}
}
//=================================================
// Events to trigger
void clickEvent1() {
(digitalWrite(LEDA_PIN, HIGH));
delay(250);
digitalWrite(LEDA_PIN, LOW);
}
void doubleClickEvent1() {
digitalWrite(LEDB_PIN, HIGH);
delay(250);
digitalWrite(LEDB_PIN, LOW);
}
void doubleHoldEvent() {
(digitalWrite(LEDC_PIN, HIGH));
delay(250);
digitalWrite(LEDC_PIN, LOW);
}
Where I mentioned "This section causes issues" if I un-comment it, the code will sometimes interpret a click incorrectly - a double click instead of a single.