#include <Arduino.h>
const int buttonPin = 21; // Pin where the button is connected
volatile int pressCount = 0; // Count of button presses
unsigned long lastPressTime = 0; // Time of the last button press
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
const unsigned long pressDelay = 400; // Time to detect multiple presses
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, FALLING); // Interrupt on button press
Serial.begin(9600); // Start serial communication
}
void loop() {
static unsigned long lastCheckTime = 0;
unsigned long currentTime = millis();
// Check for multiple presses based on delay
if (currentTime - lastCheckTime > pressDelay) {
lastCheckTime = currentTime;
// Output based on the number of presses detected
if (pressCount == 1) {
Serial.println("Single Press Detected");
} else if (pressCount == 2) {
Serial.println("Double Press Detected");
} else if (pressCount >= 3) {
Serial.println("Triple Press Detected");
}
pressCount = 0; // Reset count after processing
}
}
void buttonPressed() {
unsigned long currentTime = millis(); // Get current time
// Check for debounce
if (currentTime - lastPressTime > debounceDelay) {
lastPressTime = currentTime; // Update last press time
pressCount++; // Increment press count
}
}