const int ledPins[5] = {2, 3, 4, 5, 6}; // LED pins
int count = 0; // Start at 0
bool up = true; // Counting direction (true = up, false = down)
void setup() {
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Display the binary number using LEDs
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], bitRead(count, i));
}
// Change count direction when reaching 0 or 31
if (count == 31) {
up = false; // Start counting down
} else if (count == 0) {
up = true; // Start counting up
}
// Increment or decrement count
count += up ? 1 : -1;
delay(500); // Wait 500ms
}