// Binary Counter with LEDs
int ledCount = 4; // number of LEDs
int ledPins[] = {8, 9, 10, 11}; // LED pins (LSB to MSB)
int delayTime = 1000; // 1 second delay
void setup() {
// Initialize all LED pins as outputs
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Count from 0 to 15
for (int count = 0; count < 16; count++) {
displayBinary(count);
delay(delayTime);
}
}
// Function to display a number in binary on LEDs
void displayBinary(int number) {
for (int i = 0; i < ledCount; i++) {
// Check each bit using bitwise AND
// (1 << i) creates a bitmask for the current LED position
// If the bit is set, turn on LED, else turn off
digitalWrite(ledPins[i], (number & (1 << i)) ? HIGH : LOW);
}
}