All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 ill connect a push-button to one of the microcontroller’s GPIO pins and configure it as an input with the internal pull-up resistor enabled. In the program, I will measure the time difference between button press and release events using millis(). 

"If the button is released quickly (less than 1 second), I will check if another press occurs within 400 ms to classify it as a double click."

"If only one press occurs within the timeout, I will classify it as a single click."

"If the button is held down longer than 1 second before release, I will classify it as a long press. Each detected action will be printed to the serial monitor"

 

 

 

// Define the pin the button is connected to
const int buttonPin = 2; 

// Define time thresholds in milliseconds
const int longPressTime = 1000;  // 1 second
const int doubleClickTime = 500;   // 0.5 seconds
const int debounceDelay = 50;      // 50 milliseconds

// Variables to keep track of button state and time
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long pressStartTime = 0;
unsigned long lastClickTime = 0;
int clickCount = 0;

void setup() {
  // Initialize the serial communication
  Serial.begin(9600); // Any baud rate should work

  // Set the button pin as an input with a pull-up resistor
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Read the current state of the button
  int reading = digitalRead(buttonPin);

  // Debounce the button
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the state has been stable for the debounce delay
    if (reading != lastButtonState) {
      lastButtonState = reading;

      // Button is pressed (HIGH to LOW)
      if (lastButtonState == LOW) {
        pressStartTime = millis();
        // Check for double click
        if (millis() - lastClickTime < doubleClickTime) {
          clickCount++;
        } else {
          clickCount = 1;
        }
        lastClickTime = millis();
      }
    }
  }

  // Check for button release
  if (lastButtonState == HIGH) {
    // If a button was just released
    if (pressStartTime > 0) {
      unsigned long pressDuration = millis() - pressStartTime;
      pressStartTime = 0; // Reset for the next press

      if (pressDuration >= longPressTime) {
        // Long press detected
        Serial.println("Long press detected");
      } else {
        // If not a long press, it could be a single or double click
        // Wait for the double click timeout
        while (millis() - lastClickTime < doubleClickTime) {
          // Check for a second click
          if (digitalRead(buttonPin) == LOW) {
            // A new press has occurred, it's a double click
            // The loop will handle it, so we can just return
            return;
          }
        }
        // If the loop finished without a second click, it's a single click
        if (clickCount == 1) {
          Serial.println("Single click detected");
        } else if (clickCount == 2) {
          Serial.println("Double click detected");
        }
        clickCount = 0; // Reset for next sequence
      }
    }
  }
}

 

 

 

 

 

 

Output

Video

Add a video of the output (know more)

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!