#define PATTERN_COUNT 11 //number of segment patterns, 0-9 and all off
#define SEGMENT_COUNT 7 //how many segments I'm controlling
#define DIGIT_COUNT 4 //how many digits I'm controlling
// A,B,C,D,E,F,G
int segmentPins[SEGMENT_COUNT] = {2,3,4,5,6,7,8};
//the pins for each digit
int digitPins[] = { 9, 10, 11, 12 };
// 0 1 2 3 4 5 6 7 8 9 OFF
byte digitPatternsHex[PATTERN_COUNT] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00 };
//counter
int counter = 0;
void setup(){
Serial.begin(9600);
//init segment pins to output
for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
pinMode(segmentPins[thisPin], OUTPUT);
}
//init digit pins to output
for(int thisPin=0; thisPin<DIGIT_COUNT; thisPin++){
pinMode(digitPins[thisPin], OUTPUT);
}
}
//turn all digits off, HIGH because common cathode
void allDigitsOff(){
for(int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++){
digitalWrite(digitPins[thisPin], HIGH);
}
}
//turn specific digit on
void digitOn(int digitNum){
digitalWrite(digitPins[digitNum], LOW);
delay(2);
}
void setPattern(int pattern){
allDigitsOff();
//data pins, 8bits
for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
if(bitRead(digitPatternsHex[pattern], thisPin) == 1){
digitalWrite(segmentPins[thisPin], HIGH);
}else{
digitalWrite(segmentPins[thisPin], LOW);
}
}
}
void showNumber(int currentNumber){
//display digit from right to left
int digitInFront = currentNumber/10;
for(int currentDigit = 0; currentDigit < DIGIT_COUNT; currentDigit++){
//get number in the ones place
int number = currentNumber % 10; //5
currentNumber /= 10;
setPattern(number);
digitOn(currentDigit);
}
}
void loop(){
//return first 2 digits of ms, 1 sec = 1000 millisec, 1.5 sec = 1500 millisec
int currentNumber = (millis()/1000);
showNumber(currentNumber);
}