const int buttonPin = 18; // Define the GPIO pin where your button is connected
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int counter = 0; // Counter variable
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as an input with internal pull-up resistor
Serial.begin(115200); // Initialize serial communication for debugging
}
void loop() {
// Read the current state of the button
buttonState = digitalRead(buttonPin);
// Check if the button has been pressed (transition from HIGH to LOW)
if (buttonState == LOW && lastButtonState == HIGH) {
// Button was pressed, increment the counter
counter++;
Serial.print("Button Pressed! Count: ");
Serial.println(counter);
}
// Update the lastButtonState for the next iteration
lastButtonState = buttonState;
}