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.