All submissions

Solving Approach

How do you plan to solve it?

 
 

 

 

 

Code

/*Paste your code here*/
const int buttonPin = 2;

// Timing constants (in milliseconds)
const long longPressTime = 1000;  // 1 second
const long doubleClickTime = 500; // 0.5 seconds
const int debounceDelay = 50;     // 50 milliseconds

// State variables
int lastButtonState = HIGH;      // the previous button state
unsigned long pressStartTime = 0;   // the time the button was pressed
unsigned long lastClickTime = 0;    // the time of the last click
int clickCount = 0;              // number of clicks detected

void setup() {
  // Initialize the button pin with an internal pull-up resistor
  pinMode(buttonPin, INPUT_PULLUP);
  // Start serial communication at 115200 baud
  Serial.begin(115200);
}

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

  // Debouncing
  if (buttonState != lastButtonState) {
    delay(debounceDelay);
    buttonState = digitalRead(buttonPin);
  }

  // Check for button press
  if (buttonState == LOW && lastButtonState == HIGH) {
    pressStartTime = millis(); // Record the start time of the press
  }

  // Check for button release
  if (buttonState == HIGH && lastButtonState == LOW) {
    unsigned long pressDuration = millis() - pressStartTime;

    // Check for a long press
    if (pressDuration >= longPressTime) {
      Serial.println("Long press detected");
      clickCount = 0; // Reset click count after a long press
      lastClickTime = 0;
    } else {
      // It's a click, either single or double
      if (millis() - lastClickTime <= doubleClickTime) {
        // This is a double click
        clickCount++;
        if (clickCount >= 2) {
          Serial.println("Double click detected");
          clickCount = 0; // Reset for next sequence
          lastClickTime = 0;
        }
      } else {
        // This is the first click in a potential double-click sequence
        clickCount = 1;
        lastClickTime = millis();
      }
    }
  }

  // Handle single click if the double-click timeout has passed
  if (clickCount == 1 && millis() - lastClickTime > doubleClickTime) {
    Serial.println("Single click detected");
    clickCount = 0;
    lastClickTime = 0;
  }

  // Save the current state for the next loop iteration
  lastButtonState = buttonState;
}

 

 

 

Output 

Video

Add video of the output (know more)

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

 

 

 

 

 

 

Submit Your Solution

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