int led1 = 8;
bool ledState = 0;
const int buttonPin = 2; // Pin connected to the button
const unsigned long doubleClickTime = 300; // Milliseconds between clicks for a double-click
unsigned long lastClickTime = 0;
byte clickCount = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
Serial.begin(9600);
}
void loop() {
static bool buttonState = HIGH; // Current state of the button (HIGH when not pressed with pull-up)
bool reading = digitalRead(buttonPin);
// Debouncing
if (reading != buttonState) {
delay(50); // Simple debounce delay
reading = digitalRead(buttonPin);
if (reading == LOW && buttonState == HIGH) { // Button is pressed
unsigned long currentTime = millis();
if (currentTime - lastClickTime < doubleClickTime) {
clickCount++;
if (clickCount == 2) {
Serial.println("Double Click!");
clickCount = 0; // Reset for the next double-click
}
} else {
clickCount = 1; // First click
Serial.println("Single Click (potential)");
}
lastClickTime = currentTime;
}
buttonState = reading;
}
// Handle single click action after the doubleClickTime has passed
if (clickCount == 1 && (millis() - lastClickTime > doubleClickTime)) {
Serial.println("Single Click (confirmed)");
// Perform your single click action here
clickCount = 0;
}
}