// Pin definitions
const int ledPins[] = {9, 10, 11, 12}; // Pins for the 4 LEDs
const int incrementButtonPin = 7; // Pin for the increment button
const int decrementButtonPin = 6; // Pin for the decrement button
const int numLeds = 4; // Number of LEDs
int counter = 0; // Initial counter value
void setup() {
// Set LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Set button pins as inputs with pull-down resistors
pinMode(incrementButtonPin, INPUT_PULLUP);
pinMode(decrementButtonPin, INPUT_PULLUP);
// Initialize LEDs
updateLeds();
}
void loop() {
Serial.println(counter);
// Check if increment button is pressed
if (digitalRead(incrementButtonPin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(incrementButtonPin) == LOW) { // Check again
counter++;
if (counter > 15) { // Max count for 4 LEDs (binary 1111)
counter = 0;
}
updateLeds();
while (digitalRead(incrementButtonPin) == LOW); // Wait until button is released
}
}
// Check if decrement button is pressed
if (digitalRead(decrementButtonPin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(decrementButtonPin) == LOW) { // Check again
counter--;
if (counter < 0) { // Min count
counter = 15;
}
updateLeds();
while (digitalRead(decrementButtonPin) == LOW); // Wait until button is released
}
}
}
void updateLeds() {
// Update LED states based on counter value
for (int i = 0; i < numLeds; i++) {
if (bitRead(counter, i) == 1) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
}