How do you plan to solve it?
// 4-Bit Binary Up/Down Counter (2026 Version)
const int ledPins[] = {2, 3, 4, 5}; // Pins for LEDs (LSB to MSB)
const int btnUp = 8; // Increment button
const int btnDown = 9; // Decrement button
int counter = 0; // Stores value 0-15
bool lastUpState = HIGH;
bool lastDownState = HIGH;
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Use internal pull-ups: button reads LOW when pressed
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
displayBinary(counter);
}
void loop() {
bool currentUp = digitalRead(btnUp);
bool currentDown = digitalRead(btnDown);
// Check Up Button (State Change Detection)
if (currentUp == LOW && lastUpState == HIGH) {
delay(50); // Debounce
counter = (counter + 1) % 16;
displayBinary(counter);
}
lastUpState = currentUp;
// Check Down Button (State Change Detection)
if (currentDown == LOW && lastDownState == HIGH) {
delay(50); // Debounce
counter--;
if (counter < 0) counter = 15;
displayBinary(counter);
}
lastDownState = currentDown;
}
void displayBinary(int num) {
for (int i = 0; i < 4; i++) {
// Check if the i-th bit of 'num' is set to 1
digitalWrite(ledPins[i], (num >> i) & 1);
}
}
Add a video of the output (know more)
