0-9 ten digits correspond with each segment are as follows (the following table applies common cathode seven segment display device, if you are using a common anode, the table should be replaced every 1 0 0 should all replaced by 1):
Display
dp
a
b
c
d
e
f
g
digital
0
0
1
1
1
1
1
1
0
1
0
0
1
1
0
0
0
0
2
0
1
1
0
1
1
0
1
3
0
1
1
1
1
0
0
1
4
0
0
1
1
0
0
1
1
5
0
1
0
1
1
0
1
1
6
0
1
0
1
1
1
1
1
7
0
1
1
1
0
0
0
0
8
0
1
1
1
1
1
1
1
9
0
1
1
1
1
0
1
1
Connection Diagram:
Wiring schematic:
Physical wiring diagram:
int a = 7;
int b = 6;
int c = 5;
int d = 11;
int e = 10;
int f = 8;
int g = 9;
int ledG = 14;
int ledY = 15;
int ledR = 16;
//Display number 1
void digital_1(void)
{
unsigned char j;
digitalWrite(c, HIGH); //Lower the level of the digital 5 pin and light up segment c
digitalWrite(b, HIGH); //Light section b
for (j = 7; j <= 11; j++) //blanking
digitalWrite(j, LOW);
}
//Display number 2
void digital_2(void)
{
unsigned char j;
digitalWrite(b, HIGH);
digitalWrite(a, HIGH);
for (j = 9; j <= 11; j++)
digitalWrite(j, HIGH);
digitalWrite(c, LOW);
digitalWrite(f, LOW);
}
//Display number 3
void digital_3(void)
{
unsigned char j;
digitalWrite(g, HIGH);
digitalWrite(d, HIGH);
for (j = 5; j <= 7; j++)
digitalWrite(j, HIGH);
digitalWrite(f, LOW);
digitalWrite(e, LOW);
}
//Display number 4
void digital_4(void)
{
digitalWrite(c, HIGH);
digitalWrite(b, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(a, LOW);
digitalWrite(e, LOW);
digitalWrite(d, LOW);
}
//Display number 5
void digital_5(void)
{
unsigned char j;
for (j = 7; j <= 9; j++)
digitalWrite(j, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(b, LOW);
digitalWrite(e, LOW);
}
//Display number 6
void digital_6(void)
{
unsigned char j;
for (j = 7; j <= 11; j++)
digitalWrite(j, HIGH);
digitalWrite(c, HIGH);
digitalWrite(b, LOW);
}
//Display number 7
void digital_7(void)
{
unsigned char j;
for (j = 5; j <= 7; j++)
digitalWrite(j, HIGH);
for (j = 8; j <= 11; j++)
digitalWrite(j, LOW);
}
//Display number 8
void digital_8(void)
{
unsigned char j;
for (j = 5; j <= 11; j++)
digitalWrite(j, HIGH);
}
//Display number 9
void digital_9(void)
{
digitalWrite(c, HIGH);
digitalWrite(b, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(a, HIGH);
digitalWrite(e, LOW);
digitalWrite(d, HIGH);
}
//Pin setting and initialization
void setup()
{
int i;//defined variable
for (i = 5; i <= 16; i++)
pinMode(i, OUTPUT); //Set pins 5 ~ 16 to output mode
}
void loop()
{
while (1)
{
//red light
digitalWrite(ledR, LOW);
digitalWrite(ledY, HIGH);
digitalWrite(ledG, HIGH);
digital_9(); // Show 9
delay(1000); //Delay 1 s
digital_8(); // Show 8
delay(1000); //Delay 1 s
digital_7(); // Show 7
delay(1000); //Delay 1 s
digital_6(); // Show 6
delay(1000); //Delay 1 s
digital_5(); // Show 5
delay(1000); //Delay 1 s
digital_4(); // Show 4
delay(1000); //Delay 1 s
digital_3(); // Show 3
delay(1000); //Delay 1 s
digital_2(); // Show 2
delay(1000); //Delay 1 s
digital_1(); // Show 1
delay(1000); //Delay 1 s
//green light
digitalWrite(ledR, HIGH);
digitalWrite(ledY, HIGH);
digitalWrite(ledG, LOW);
digital_9(); // Show 9
delay(1000); //Delay 1 s
digital_8(); // Show 8
delay(1000); //Delay 1 s
digital_7(); // Show 7
delay(1000); //Delay 1 s
digital_6(); // Show 6
delay(1000); //Delay 1 s
digital_5(); // Show 5
delay(1000); //Delay 1 s
digital_4(); // Show 4
delay(1000); //Delay 1 s
digital_3(); // Show 3
delay(1000); //Delay 1 s
digital_2(); // Show 2
delay(1000); //Delay 1 s
digital_1(); // Show 1
delay(1000); //Delay 1 s
//yellow light
digitalWrite(ledR, HIGH);
digitalWrite(ledY, LOW);
digitalWrite(ledG, HIGH);
digital_3(); // Show 3
delay(1000); //Delay 1 s
digital_2(); // Show 2
delay(1000); //Delay 1 s
digital_1(); // Show 1
delay(1000); //Delay 1 s
}
}