#include <Arduino.h>
const int switchUpPin = 13; // Connect the up counter push button to digital pin 13
const int switchDownPin = 12; // Connect the down counter push button to digital pin 12
const int ledPins[] = {23, 22, 21, 4}; // Connect the LEDs to digital pins 23, 22, 21, and 4
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); // Calculate the number of LEDs
bool buttonUpState = false; // Variable to store the state of the up counter push button
bool lastButtonUpState = false; // Variable to store the previous state of the up counter push button
bool buttonDownState = false; // Variable to store the state of the down counter push button
bool lastButtonDownState = false; // Variable to store the previous state of the down counter push button
int ledState = 0; // Variable to store the LED pattern, starts from 0 (binary: 0000)
void setup() {
Serial.begin(9600);
pinMode(switchUpPin, INPUT_PULLUP); // Enable internal pull-up resistor for the up counter push button
pinMode(switchDownPin, INPUT_PULLUP); // Enable internal pull-up resistor for the down counter push button
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT); // Set LED pins as OUTPUTs
}
updateLeds(); // Initially update LEDs based on the initial ledState
}
void loop() {
buttonUpState = digitalRead(switchUpPin);
buttonDownState = digitalRead(switchDownPin);
if (buttonUpState != lastButtonUpState) {
if (buttonUpState == LOW) {
// Increment ledState for up counter
ledState++;
if (ledState > 15) {
ledState = 0; // Reset ledState if it exceeds 15
}
updateLeds(); // Update LEDs based on the new ledState
}
lastButtonUpState = buttonUpState;
delay(50); // Debounce delay
}
if (buttonDownState != lastButtonDownState) {
if (buttonDownState == LOW) {
// Decrement ledState for down counter
ledState--;
if (ledState < 0) {
ledState = 15; // Reset ledState if it goes below 0
}
updateLeds(); // Update LEDs based on the new ledState
}
lastButtonDownState = buttonDownState;
delay(50); // Debounce delay
}
}
// Function to update the LEDs based on the current ledState
void updateLeds() {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], bitRead(ledState, numLeds - 1 - i)); // Set LED based on bit value of ledState
}
}