// Pin definitions
const int ledPins[] = {2, 4, 5}; // GPIO pins for LEDs
const int buttonPin = 14; // GPIO pin for the push button
// Variables
int ledState = LOW; // Current LED state
int buttonState = LOW; // Current button state
int lastButtonState = LOW; // Previous button state
unsigned long lastDebounceTime = 0; // Last time the button state changed
unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize LED pins as OUTPUT
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize button pin as INPUT_PULLDOWN
pinMode(buttonPin, INPUT_PULLDOWN);
}
void loop() {
// Read the state of the push button
int reading = digitalRead(buttonPin);
// Check if the button state has changed
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// Check if the button state has been stable for the debounce delay
if ((millis() - lastDebounceTime) > debounceDelay) {
// If it has, update the button state
if (reading != buttonState) {
buttonState = reading;
// If the button is pressed (HIGH), toggle LEDs
if (buttonState == HIGH) {
toggleLEDs();
}
}
}
// Save the last button state
lastButtonState = reading;
}
void toggleLEDs() {
for (int i = 0; i < 3; i++) {
// Toggle the LED state
ledState = (ledState == LOW) ? HIGH : LOW;
digitalWrite(ledPins[i], ledState);
delay(500); // Change this delay to adjust the blinking speed
}
}