All submissions

Single Double Click and Long Press Detection

Solving Approach

How do you plan to solve it?

 
 Read the Button State: Continuously check the state of the button.

Debounce: Implement a simple software debounce to prevent switch bounce from being interpreted as multiple presses. This can be done by waiting a short time (e.g., 50 ms) after a state change to confirm the new state is stable.

Use a Finite State Machine: A state machine is ideal for this. We can define states like:

IDLE: The button is not being pressed.

SINGLE_CLICK_CANDIDATE: The button was just released, and we're waiting to see if a second click occurs within a short time frame.

LONG_PRESS_CANDIDATE: The button is being held down, and we're timing it to see if it becomes a long press.

Timers: Use non-blocking timers with millis() to track the duration of a press and the time between clicks. This is crucial for avoiding the use of delay(), which would freeze the program.

 

 

 

Code

/*Paste your code here*/
// Define the button pin and timing constants
const int buttonPin = 2; // Use the actual pin number
const int longPressTime = 1000; // milliseconds
const int doubleClickTime = 300; // milliseconds
const int debounceTime = 50; // milliseconds
// State variables
int lastButtonState = HIGH; // Initial button state (HIGH due to INPUT_PULLUP)
unsigned long pressStartTime = 0;
unsigned long releaseTime = 0;
bool longPressDetected = false;
int clickCount = 0;
void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
    Serial.begin(9600); // Start serial communication
    }
    void loop() {
      int currentButtonState = digitalRead(buttonPin);
        // Debouncing logic
          if (currentButtonState != lastButtonState) {
              delay(debounceTime); // Wait for button to settle
                  currentButtonState = digitalRead(buttonPin);
                    }
                      // --- Button Pressed ---
                        if (currentButtonState == LOW && lastButtonState == HIGH) {
                            pressStartTime = millis();
                                longPressDetected = false; // Reset long press flag
                                  }
                                    
                                      // --- Button Held Down ---
                                        if (currentButtonState == LOW) {
                                            if (!longPressDetected && (millis() - pressStartTime) > longPressTime) {
                                                  Serial.println("Long press detected");
                                                        longPressDetected = true;
                                                              clickCount = 0; // Reset click count to ignore any releases after a long press
                                                                  }
                                                                    }
                                                                      // --- Button Released ---
                                                                        if (currentButtonState == HIGH && lastButtonState == LOW) {
                                                                            if (!longPressDetected) {
                                                                                  clickCount++;
                                                                                        releaseTime = millis();
                                                                                            }
                                                                                              }
                                                                                                // --- Check for Single/Double Clicks ---
                                                                                                  if (clickCount > 0 && (millis() - releaseTime) > doubleClickTime) {
                                                                                                      if (clickCount == 1) {
                                                                                                            Serial.println("Single click detected");
                                                                                                                } else if (clickCount == 2) {
                                                                                                                      Serial.println("Double click detected");
                                                                                                                          }
                                                                                                                              clickCount = 0; // Reset for next detection cycle
                                                                                                                                }
                                                                                                                                  lastButtonState = currentButtonState;
                                                                                                                                  }
                                                                                                                                 
                                                                                                                                
                                                                                                                                 
                                                                                                                                  
                                                                                                                              OUT PUT
                                                                                                                              

Submit Your Solution

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