Skip to content

Instantly share code, notes, and snippets.

@jmsaavedra
Last active November 15, 2017 20:22
Show Gist options
  • Save jmsaavedra/7caac21690b72a7d75409c269b201ed4 to your computer and use it in GitHub Desktop.
Save jmsaavedra/7caac21690b72a7d75409c269b201ed4 to your computer and use it in GitHub Desktop.
triple click detection
/* https://github.com/JChristensen/Timer */
#include <Timer.h>
Timer t_press;
int POWER_SNAP_BTN = 8;
int LED_1 = 13;
int LED_2 = 3;
int pressCount = 0;
long btnPressTime = 0; // for button de-bounce
bool btnReleased = true;
void setup(){
pinMode(POWER_SNAP_BTN, INPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
Serial.begin(57600);
}
void loop(){
checkBtn();
t_press.update();
}
void checkBtn(){
int btnState = digitalRead(POWER_SNAP_BTN);
if(btnState && btnReleased && pressCount < 3 && (millis()-btnPressTime > 200)){
btnReleased = false;
btnPressTime = millis();
digitalWrite(LED_1, HIGH);
pressCount++;
Serial.print("++ pressCount now");
Serial.println(pressCount);
int afterEvent = t_press.after(650, pressCtDown);
if(pressCount >= 3){
Serial.println(" >>> TRIPLE CLICK DETECTED");
digitalWrite(LED_2, HIGH);
int afterEvent = t_press.after(3000, ledsOff);
}
}
if (!btnState) {
btnReleased = true;
digitalWrite(LED_1, LOW);
}
}
void pressCtDown(){
pressCount--;
Serial.println(pressCount);
}
void ledsOff(){
digitalWrite(LED_2, LOW);
digitalWrite(LED_1, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment