直接上代码
int a = 6;
int b = 7;
int c = 8;
int d = 9;
int e = 10;
int f = 11;
int g = 12;
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
Serial.begin(9600);
}
int dd = 2;
int d1[4] = {1, 0, 0, 0};
int d2[4] = {0, 1, 0, 0};
int d3[4] = {0, 0, 1, 0};
int d4[4] = {0, 0, 0, 1};
int current = 0;
void loop() {
// put your main code here, to run repeatedly:
if (dd > 5) {
dd = 2;
}
if (current > 9) {
current = 0;
}
Serial.println(dd);
switch (dd) {
case 2: open1(); break;
case 3: open2(); break;
case 4: open3(); break;
case 5: open4(); break;
default: open1(); break;
}
digitalWrite(6, HIGH);
switch (current) {
case 0: print0(); break;
case 1: print1(); break;
case 2: print2(); break;
case 3: print3(); break;
case 4: print4(); break;
case 5: print5(); break;
case 6: print6(); break;
case 7: print7(); break;
case 8: print8(); break;
case 9: print9(); break;
default: print0(); break;
}
current++;
delay(1000);
dd++;
}
void open1() {
int b = sizeof(d1) / sizeof(d1[0]);
int j = 2;
for (int i = 0; i < b; i++) {
//digitalWrite(d+i, d1[i]);
digitalWrite(i + 2, d1[i]);
}
}
void open2() {
int b = sizeof(d2) / sizeof(d2[0]);
for (int i = 0; i < b; i++) {
digitalWrite(i + 2, d2[i]);
}
}
void open3() {
int b = sizeof(d3) / sizeof(d3[0]);
for (int i = 0; i < b; i++) {
digitalWrite(i + 2, d3[i]);
}
}
void open4() {
int b = sizeof(d4) / sizeof(d4[0]);
for (int i = 0; i < b; i++) {
digitalWrite(i + 2, d4[i]);
}
}
void print0() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void print1() {
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void print2() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
void print3() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}
void print4() {
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void print5() {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void print6() {
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void print7() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
void print8() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
void print9() {
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}
接线是控制四个数码管的引脚分别为2,3,4,5.控制abcdefg的引脚为6,7,8,9,10,11,12