#define DEBUG_BUTTON false
#define PotButton A1
#define shortPress 1
#define longPress 2
#define multiPress 3
#define MAXMULTIPERIOD 1700 // Time frame for multiple press
#define glitchPressPeriod 500 // Time frame for a Glitch
#define shortPressPeriod 1000 // Time frame for Short Press period
#define longPressPeriod 2000 // Time frame for Long Press period
#define longerPressPeriod 3000 // Time frame for Longer Press period
int pressType = 0; // 0 waiting button release
uint8_t ButtCounter = 0; // Store last button press for multiple presses
uint16_t brightRead = 0; // Time Button detected
uint16_t ButtonStartTime = 0; // Time Button detected
uint16_t LastButtStartTime = 0; // Time since 1st Button press
void resetButton();
void userIF();
int clickCounter();
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
pinMode(PotButton, INPUT_PULLUP);
pinMode(A2, INPUT);
ButtonStartTime = 0; // Capture the time
}
void loop() {
userIF();
if(ButtCounter > 0){
// Serial.println(ButtCounter+1);
}
delay(100);
}
void userIF(){
brightRead = analogRead(PotButton);
#if DEBUG_BUTTON
Serial.print( "(" ); Serial.print(brightRead); Serial.print( ") " );
#endif
if(brightRead > 0 && ButtonStartTime == 0){
#if DEBUG_BUTTON
Serial.println("\tGot Yer!!!\tContinuing on");
//Serial.println();
#endif
// ************** Do something.... *****************
return;
}
if(ButtonStartTime == 0) {
ButtonStartTime = millis(); // Start Timer and return
#if DEBUG_BUTTON
#endif
// Serial.println("Timer started!");
return;
}
// Timer is ticking
uint16_t buttonPeriod = millis() - ButtonStartTime;
#if DEBUG_BUTTON
Serial.println(buttonPeriod);
#endif
if(brightRead == 0 && ButtonStartTime ) return; // Waiting release
// Button release so calculate length
uint16_t period = millis() - ButtonStartTime;
if(period > longerPressPeriod) pressType = longPress;
else if(period < glitchPressPeriod) {resetButton; return;} // GLITCH.... reset button & get out of here
else if(period < shortPressPeriod) {
pressType = shortPress;
int clickCount = clickCounter();
}
else pressType = longPress;
resetButton();
// delay(500);
return;
}
/*
* We are here because we detected a click
* So if we have not timed out add click
* Otherwise if timed out update click responder i.e global variable indicating click count
*/
// Global variable (Program)
// ButtCounter
// Local variable (Inside this routine)
uint32_t conTimeout = 0;
int conClickCount = 0;
int clickCounter(){
ButtCounter = 1;
conTimeout = millis() + MAXMULTIPERIOD; // Start timeout
//Serial.println("Timeout Started");
conClickCount = 1; // Reset local counter and Timeout
while( millis() < conTimeout ){ // Wait for next click
if(analogRead(PotButton) == 0) {
++conClickCount;
while( analogRead(PotButton) == 0 ){
//delay(1);
}; // Wait release
}
delay(30);
}
//Serial.println("DONE");
Serial.println( conClickCount );
ButtCounter = conClickCount;
return conClickCount;
}
// Reset everything
void resetButton() {
ButtonStartTime = 0; // Capture the time
pressType = 0;
}