# How to configure timers for 7 segment display

Hello, I’m trying to drive a 4 digit 7 segment display with an ATmega328 on a breadboard using it’s 8 MHz internal clock, and I need to set up the display to run using timer1 so that I can do other things while the display is multiplexing, otherwise it stops displaying correctly. Here is my code:

``````bool digits[10][7] = {
{1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1},
{1, 1, 1, 1, 0, 0, 1},
{0, 1, 1, 0, 0, 1, 1},
{1, 0, 1, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1}
};

int segmentPins[] {
2,
3,
4,
5,
6,
7,
8
};

int digitPins[] {
9,
10,
11,
12
};

void setup() {
for(int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
for(int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}

}

void loop() {
display(5678);
delay(500);
}

void display(int number) {
int digit[4];
digit[0] = number / 1000;
digit[1] = number / 100 - digit[0] * 10;
digit[2] = number / 10 - digit[1] * 10 - digit[0] * 100;
digit[3] = number / 1 - digit[2] * 10 - digit[1] * 100 - digit[0] * 1000;
for(int i = 0; i < 4; i++) {
setDigit(digitPins[3 - i], digit[i]);
delay(2);
}
}

void setDigit(int digit, int number) {
for(int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH);
}
for(int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], LOW);
}
digitalWrite(digit, LOW);
for(int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digits[number][i]);
}
}
``````

PS: I’m a noob on arduino timers, but I know that you can use them to do multiple things at once, but none of the tutorials online that I found were easy to follow and/or understand.

Do it in two stages.
Stage 1 to prepare your function display() so it can be called without parameters. That is make number global and also make i global. The function display() should then, each time it is called, display only the digit at position i and then increment i in preparation for the next call. Currently, you have a for loop with a delay which goes through all four digits and you should remove all that.

To test it, modify the loop so every 5 milliseconds, it calls display().

Once you have got that far, it is a simple matter to call display from a timer. See Arduino Playground - Timer1 and use the methods Timer1.initialize() and Timer1.attachInterrupt() to multiplex your display.

Oh, okay I'll try that. seems simple enough