const int GPIO_Pins[] = {5, 4, 3, 2}; // pin nos 2 to 5 represent four bit binary number
// bit bit 0 = pin no 5, bit 1 = 4, b2 = 3 and bit 3 = 2
// You can use same colour LEDs for four bits
const int numLEDs = 4;
const int delayLEDsNum = 2000; // in milliseconds
const int delayLEDsEnd = 3000; // additional delay of 3 seconds while highest number is being displayed
void setup() {
byte temp;
for(int led = 0; led < numLEDs; led++)
{
pinMode(GPIO_Pins[led], OUTPUT); // Set the LEDs in OUTPUT mode
digitalWrite(GPIO_Pins[led], LOW); // clear all the bits initially
}
}
void loop() {
// put your main code here, to run repeatedly:
static byte num = 0;
displayNum(num);
displayNum(~num);
num++;
if(num > 15){
num = 0;
delay(delayLEDsEnd);
}
} // end of loop()
void displayNum(int num){
byte mask = 0x01; // only the LSB is one, all other 7 bits are zeros
byte tempMask;
for(int i = 0; i < numLEDs; i++){
tempMask = mask << i;
if( (num & tempMask) != 0){
digitalWrite(GPIO_Pins[i], HIGH);
}
else{// if the bit is zero
digitalWrite(GPIO_Pins[i], LOW);
}
} // end of for loop
delay(delayLEDsNum);
} // end of displayNum()