byte fontArray[] = {
0b00111111, // 0 DP-g-f-e-d-c-b-a, 1 = segment on
0b00000110, // 1
0b01011011, // 2
0b01001111,
0b01100110,
0b01101101,
0b01111101,
0b00000111,
0b01111111,
0b01101111, // 9
};
unsigned long currentMillis=millis();
unsigned long previousMillis=0;
unsigned long elapsedMillis=currentMillis-previousMillis;
long onDuration=3000;
unsigned char digitDrive[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
int digitPin=13;
void setup(){
}
void loop(){
currentMillis = millis(); // all time elements are type unsigned long, declare before setup()
elapsedMillis = currentMillis - previousMillis;
if (elapsedMillis >= onDuration){ // onDuration set for 2, 3 whatever you use, up to about 8 for 30Hz refresh
previousMillis = previousMillis + onDuration; // set up for next interval
// turn off current digit drive transistor, assume using pins 15-14-13-12, set up in an array:
digitalWrite (digitPin[digitDrive], LOW);
digitDrive = digitDrive[10] + 1; // point to next digit
if (digitDrive == 4){ digitDrive = 0;} // reset to initial digit
// assume you’re using 4 bits of PORTD and 4 bits of PORTB: 7-6-5-4, and 11-10-9-8, for DP-g-f-e, d-c-b-a
// and data is stored in an array
// clear lower 4 bits of PORTB
PORTB = PORTB & 0b11110000;
// look up the font (segment mapping), mask off lower 4 bits of data, OR into output register
PORTB = PORTB | (fontArray[dataArray[digitDrive]] & 0b00001111);
// clear upper 4 bits of PORTD,
// look up the font (segment mapping), mask off upper 4 bits of data, OR into output register
PORTD = PORTD & 0b00001111;
PORTD = PORTD | (fontArray[dataArray[digitDrive]] & 0b11110000);
// turn on the transistor for the current data
digitalWrite (digitPin[digitDrive], HIGH);
}
// do whatever you do to update dataArray[0], dataArray[1], dataArray[2], dataArray[3] with data while the 2-8mS is passing by to turn on the next digit
}