// Include required libraries
#include <Arduino.h>
// Define the pins for the LEDs
const int ledPins[] = {19, 18, 5, 17}; // Change pins as per your setup
// Define the pin for the push button
const int buttonPin = 16; // Change pin as per your setup
// Variable to store the current count
int count = 0;
// Variable to store the state of the LED counter
bool ledCounterOn = false;
void setup() {
// Set all LED pins as output
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Set the push button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read the state of the push button
int buttonState = digitalRead(buttonPin);
// Toggle LED counter on/off when the button is pressed
if (buttonState == LOW) {
ledCounterOn = !ledCounterOn;
delay(50); // Debouncing delay
}
// If LED counter is on, increment count and update LEDs
if (ledCounterOn) {
count++;
updateLEDs();
delay(500); // Delay between each LED
}
}
void updateLEDs() {
// Reset count to 0 if it reaches 4
if (count == 4) {
count = 0;
}
// Turn off all LEDs
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the current LED
digitalWrite(ledPins[count], HIGH);
}