// Define GPIO pins connected to LED and Pushbutton
const int ledPin = 13; // GPIO 13 for LED
const int buttonPin = 12; // GPIO 12 for Pushbutton
// Variables to store the current state
bool LED_ON = false;
bool buttonPressed = false;
void setup() {
Serial.begin(115200);
// Initialize GPIO pins
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for the pushbutton
}
void loop() {
// Read the state of the pushbutton
bool buttonState = digitalRead(buttonPin);
// Check if the pushbutton is pressed (active LOW due to pull-up)
if (buttonState == LOW && !buttonPressed) {
buttonPressed = true;
Serial.println("Button pressed.");
delay(100); // Debounce delay
// Toggle the LED state based on button press
if (!LED_ON) {
Serial.println("LED ON");
digitalWrite(ledPin, HIGH);
LED_ON = true;
} else {
Serial.println("LED OFF");
digitalWrite(ledPin, LOW);
LED_ON = false;
}
} else if (buttonState == HIGH) {
buttonPressed = false;
}
}