const int buttonPin = 13;
const int ledPin = 12;
int btnPressedCount = 0;
int btnReleasedCount = 0;
bool lastButtonState = LOW; // Stores the previous button state
void setup() {
pinMode(buttonPin, INPUT_PULLDOWN); // Ensures button reads LOW when not pressed
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
bool buttonState = digitalRead(buttonPin);
// Check for button press (LOW to HIGH transition)
if (buttonState == HIGH && lastButtonState == LOW) {
btnPressedCount++;
digitalWrite(ledPin, HIGH); // Turn LED ON
Serial.print("Button Pressed: ");
Serial.println(btnPressedCount);
}
// Check for button release (HIGH to LOW transition)
if (buttonState == LOW && lastButtonState == HIGH) {
btnReleasedCount++;
digitalWrite(ledPin, LOW); // Turn LED OFF
Serial.print("Button Released: ");
Serial.println(btnReleasedCount);
}
lastButtonState = buttonState; // Update last button state
delay(50); // Small delay to prevent bouncing issues
}