样品谈到最后一个驱动程序LED数码管,采用了最简单的解决方案之一,对于每一个LED高低电平控制,这样的好处是每个LED控制可检。避免短路造成的错觉,因为,但是对于数字的变化是,它是多余的写,因此,这种试图通过一组方法,来实现。
//设置阴极接口int d1 = 1;int d2 = 2;int d3 = 3;int d4 = 4;int d5 = 5;int d6 = 6;int d7 = 7;//设置阳极接口int a = 8;int b = 9;int c = 10;int d = 11;int e = 12;int f = 13;int g = A0;int h = A1;byte gyang[8] = { a, b, c, d, e, f, g, h };byte gyin[7] = { d1, d2, d3, d4, d5, d6, d7 }; byte gong_yang[14][8] = { { 1,1,0,0,0,0,0,0 }, // 0 = 缺水 { 0,0,1,1,0,0,0,0 }, // 1 = 低水位 { 0,0,0,0,1,1,1,0 }, // 2 = 定时关机 { 1,1,1,1,1,1,0,0 }, // 3 = 0 { 0,1,1,0,0,0,0,0 }, // 4 = 1 { 1,1,0,1,1,0,1,0 }, // 5 = 2 { 1,1,1,1,0,0,1,0 }, // 6 = 3 { 0,1,1,0,0,1,1,0 }, // 7 = 4 { 1,0,1,1,0,1,1,0 }, // 8 = 5 { 1,0,1,1,1,1,1,0 }, // 9 = 6 { 1,1,1,0,0,0,0,0 }, // 10 = 7 { 1,1,1,1,1,1,1,0 }, // 11 = 8 { 1,1,1,1,0,1,1,0 }, // 12 = 9 { 0,0,0,0,0,0,0,1 }, // 13 = 摄氏度符号、睡眠符号 }; byte gong_yin[5][7] = { { 1,1,1,1,1,1,0 }, // 0 = 缺水、低水位、定时关机 { 0,1,1,1,1,1,1 }, // 1 = 温度十位、摄氏度符号 { 1,0,1,1,1,1,1 }, // 2 = 温度个位、睡眠符号 { 0,0,1,1,1,1,1 }, // 3 = 温度个和十位 { 1,1,0,0,0,0,1 }, // 4 = 时钟个、十位和两点 }; void setup(){pinMode(d1, OUTPUT);pinMode(d2, OUTPUT);pinMode(d3, OUTPUT);pinMode(d4, OUTPUT);pinMode(d5, OUTPUT);pinMode(d6, OUTPUT);pinMode(d7, OUTPUT);pinMode(a, OUTPUT);pinMode(b, OUTPUT);pinMode(c, OUTPUT);pinMode(d, OUTPUT);pinMode(e, OUTPUT);pinMode(f, OUTPUT);pinMode(g, OUTPUT);pinMode(h, OUTPUT);digitalWrite(a, LOW);digitalWrite(b, LOW);digitalWrite(c, LOW);digitalWrite(d, LOW);digitalWrite(e, LOW);digitalWrite(f, LOW);digitalWrite(g, LOW);digitalWrite(h, LOW);digitalWrite(d1, HIGH);digitalWrite(d2, HIGH);digitalWrite(d3, HIGH);digitalWrite(d4, HIGH);digitalWrite(d5, HIGH);digitalWrite(d6, HIGH);digitalWrite(d7, HIGH);}void loop(){ GongYang(0); GongYin(0); delay(300); GongYang(1); delay(300); GongYang(2); delay(300); GongYin(3); GongYang(3); delay(300); GongYang(4); delay(300); GongYang(5); delay(300); GongYang(6); delay(300); GongYang(7); delay(300); GongYang(8); delay(300); GongYang(9); delay(300); GongYang(10); delay(300); GongYang(11); delay(300); GongYang(12); delay(300); GongYang(13); GongYin(1); delay(300); GongYin(2); delay(300); GongYin(4); GongYang(3); delay(300); GongYang(4); delay(300); GongYang(5); delay(300); GongYang(6); delay(300); GongYang(7); delay(300); GongYang(8); delay(300); GongYang(9); delay(300); GongYang(10); delay(300); GongYang(11); delay(300); GongYang(12); delay(300);}void GongYang(int x){ for (int i = 0; i < 8; i++) { digitalWrite(gyang[i], gong_yang[x][i]); }}void GongYin(int y){ for (int i = 0; i < 7; i++) { digitalWrite(gyin[i], gong_yin[y][i]); }}
SETUP语句和LOOP语句还有非常多冗余的代码,用for循环优化代码。
//设置阴极接口int d1 = 1;int d2 = 2;int d3 = 3;int d4 = 4;int d5 = 5;int d6 = 6;int d7 = 7;//设置阳极接口int a = 8;int b = 9;int c = 10;int d = 11;int e = 12;int f = 13;int g = A0;int h = A1;byte gyang[8] = { a, b, c, d, e, f, g, h };byte gyin[7] = { d1, d2, d3, d4, d5, d6, d7 }; byte gong_yang[14][8] = { { 1,1,0,0,0,0,0,0 }, // 0 = 缺水 { 0,0,1,1,0,0,0,0 }, // 1 = 低水位 { 0,0,0,0,1,1,1,0 }, // 2 = 定时关机 { 1,1,1,1,1,1,0,0 }, // 3 = 0 { 0,1,1,0,0,0,0,0 }, // 4 = 1 { 1,1,0,1,1,0,1,0 }, // 5 = 2 { 1,1,1,1,0,0,1,0 }, // 6 = 3 { 0,1,1,0,0,1,1,0 }, // 7 = 4 { 1,0,1,1,0,1,1,0 }, // 8 = 5 { 1,0,1,1,1,1,1,0 }, // 9 = 6 { 1,1,1,0,0,0,0,0 }, // 10 = 7 { 1,1,1,1,1,1,1,0 }, // 11 = 8 { 1,1,1,1,0,1,1,0 }, // 12 = 9 { 0,0,0,0,0,0,0,1 }, // 13 = 摄氏度符号、睡眠符号 }; byte gong_yin[5][7] = { { 1,1,1,1,1,1,0 }, // 0 = 缺水、低水位、定时关机 { 0,1,1,1,1,1,1 }, // 1 = 温度十位、摄氏度符号 { 1,0,1,1,1,1,1 }, // 2 = 温度个位、睡眠符号 { 0,0,1,1,1,1,1 }, // 3 = 温度个和十位 { 1,1,0,0,0,0,1 }, // 4 = 时钟个、十位和两点 }; void setup(){ pinMode(A0, OUTPUT); pinMode(A1, OUTPUT); for(int i=1; i<14;i++) { pinMode(i,OUTPUT); }}void loop(){ GongYin(0); for(int i=0; i<3; i++) { GongYang(i); delay(300); } GongYin(3); for(int i=3; i<13; i++) { GongYang(i); delay(300); } GongYang(13); GongYin(1); delay(300); GongYin(2); delay(300); GongYin(4); for(int i=3; i<13; i++) { GongYang(i); delay(300); }}void GongYang(int x){ for (int i = 0; i < 8; i++) { digitalWrite(gyang[i], gong_yang[x][i]); }}void GongYin(int y){ for (int i = 0; i < 7; i++) { digitalWrite(gyin[i], gong_yin[y][i]); }}
版权声明:本文博客原创文章,博客,未经同意,不得转载。