// Define pin numbers for LEDs
const int ledPins[] = {13, 12, 11, 10}; // 4-bit binary counter
// Define pin numbers for push buttons
const int incButtonPin = 2; // Increment button
const int decButtonPin = 3; // Decrement button
// Variables to store the current state of the counter and button states
int counter = 0;
int lastIncButtonState = LOW;
int lastDecButtonState = LOW;
void setup() {
// put your setup code here, to run once:
// Initialize LED pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize button pins as inputs
pinMode(incButtonPin, INPUT);
pinMode(decButtonPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Read the current state of the buttons
int incButtonState = digitalRead(incButtonPin);
int decButtonState = digitalRead(decButtonPin);
// Check if the increment button is pressed
if (incButtonState == HIGH && lastIncButtonState == LOW) {
counter++;
if (counter > 15) { // Reset counter if it exceeds 4-bit limit
counter = 0;
}
updateLEDs();
}
// Check if the decrement button is pressed
if (decButtonState == HIGH && lastDecButtonState == LOW) {
counter--;
if (counter < 0) { // Reset counter if it goes below 0
counter = 15;
}
updateLEDs();
}
// Update the last button states
lastIncButtonState = incButtonState;
lastDecButtonState = decButtonState;
// Small delay to debounce the buttons
delay(50);
}
// Function to update the LEDs based on the current counter value
void updateLEDs() {
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], (counter >> i)& 0x01);
}
}