1 数码管介绍
fritzing里面搜:segment
字母 | LED |
---|---|
a | 3 |
b | 4 |
c | 7 |
d | 8 |
e | 9 |
f | 6 |
g | 5 |
2.接线图
上面:
从左到右为:5,6,gnd,3,4
下面:
从左到右为:9,8,gnd,7,null
3.代码
让数码管从0到9依次亮一遍
int a=3,b=4,c=7,d=8,e=9,f=6,g=5;
void drawZero()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
}
void drawOne()
{
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
void drawTwo()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
}
void drawThree()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
}
void drawFour()
{
digitalWrite(f,HIGH);
digitalWrite(b,HIGH);
digitalWrite(g,HIGH);
digitalWrite(c,HIGH);
}
void drawFive()
{
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
}
void drawSix()
{
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
digitalWrite(e,HIGH);
}
void drawSeven()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
void drawEight()
{
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
digitalWrite(e,HIGH);
digitalWrite(b,HIGH);
}
void drawNine()
{
digitalWrite(a,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(d,HIGH);
digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
}
void clear_()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=3;i<=9;i++) pinMode(i,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
drawZero();
delay(200);
clear_();
drawOne();
delay(200);
clear_();
drawTwo();
delay(200);
clear_();
drawThree();
delay(200);
clear_();
drawFour();
delay(200);
clear_();
drawFive();
delay(200);
clear_();
drawSix();
delay(200);
clear_();
drawSeven();
delay(200);
clear_();
drawEight();
delay(200);
clear_();
drawNine();
delay(200);
clear_();
}
简化一下,用二维数组:
int a=3,b=4,c=7,d=8,e=9,f=6,g=5;
int draw[10][7]=
{
{a,b,c,d,e,f,-1}, //0
{b,c,-1,-1,-1,-1,-1}, //1
{a,b,g,d,e,-1,-1}, //2
{a,b,g,d,c,-1,-1}, //3
{f,b,g,c,-1,-1,-1}, //4
{a,f,g,d,c,-1,-1}, //5
{a,f,g,d,c,e,-1}, //6
{a,b,c,-1,-1,-1,-1}, //7
{a,b,c,d,e,f,g}, //8
{a,f,g,d,c,b,-1} //9
};
void clear_()
{
for(int i=3;i<=9;i++) digitalWrite(LOW);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=3;i<=9;i++) pinMode(i,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<10;i++)
{
for(int j=0;j<7;j++)
{
if(draw[i][j]==-1) break;
digitalWrite(draw[i][j],HIGH);
}
delay(200);
clear_();
}
}
加一个按钮,让数字随按钮变化:
按钮管脚是2
int a=3,b=4,c=7,d=8,e=9,f=6,g=5;
int num=0;
int draw[10][7]=
{
{a,b,c,d,e,f,-1}, //0
{b,c,-1,-1,-1,-1,-1}, //1
{a,b,g,d,e,-1,-1}, //2
{a,b,g,d,c,-1,-1}, //3
{f,b,g,c,-1,-1,-1}, //4
{a,f,g,d,c,-1,-1}, //5
{a,f,g,d,c,e,-1}, //6
{a,b,c,-1,-1,-1,-1}, //7
{a,b,c,d,e,f,g}, //8
{a,f,g,d,c,b,-1} //9
};
void clear_()
{
for(int i=3;i<=9;i++) digitalWrite(i,LOW);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i=3;i<=9;i++) pinMode(i,OUTPUT);
pinMode(2,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Serial.println(digitalRead(2));
if(!digitalRead(2))
{
delay(100);
while(!digitalRead(2));
num=(num+1)%10;
clear_();
// Serial.println(num);
}
for(int j=0;j<7;j++)
{
if(draw[num][j]==-1) break;
digitalWrite(draw[num][j],HIGH);
}
}