int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
int commonCathodePin = 9;
int digit [10] = {
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4 I reversed this part of the code because bitRead
B01101101, //5 function reads from LSB to MSB. In this case,
B01111101, //6 the LSB of the array "segmentPins" is 2 or A, while
B00000111, //7 the MSB is 8 or G. In order to display the correct
B01111111, //8 number, reversing it from the original code
B01101111 //9 (i.e. B11000000 it wont display 0) is essential.
};
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(commonCathodePin, OUTPUT);
}
void loop() {
for (int i=0; i<10; i++) {
displayDigit(i);
delay(1000);
}
}
void displayDigit(int number) {
digitalWrite(commonCathodePin, LOW);
for (int i = 0; i <7; i++) {
digitalWrite(segmentPins[i], bitRead(digit[number], i));
}
digitalWrite(commonCathodePin, HIGH);
}