#define LEDred A5//红灯引脚定义
#define LEDyellow A4//绿灯引脚定义
#define LEDgreen A3//黄灯灯引脚定义
//设置阴极接口
int a = 13;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int h =8;
//设置阳极接口
int d1 = 9;
int d2 = 10;
int d3 = 11;
int d4 = 12;
void setup()
{
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, 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);
pinMode(LEDred, OUTPUT);
pinMode(LEDyellow, OUTPUT);
pinMode(LEDgreen, OUTPUT);
}
void loop()
{
digitalWrite(LEDred,HIGH);//点亮红灯
for(int i=14;i>=0;i--)//点亮15秒
{
for(int k=500;k>0;k--){//切换秒数的快慢 k的初值越大 切换就越慢 k的初值越小 切换的越快
//两位的数字分别设置
Display(1,i/10);//十位
Display(2, i%10);//个位
}
}
digitalWrite(LEDred,LOW);//熄灭红灯
digitalWrite(LEDyellow,HIGH);//点亮黄灯
for(int i=4;i>=0;i--)//点亮5秒
{
for(int k=500;k>0;k--){
Display(1,i/10);//十位
Display(2, i%10);//个位
}
}
digitalWrite(LEDyellow,LOW);//熄灭黄灯
digitalWrite(LEDgreen,HIGH);//点亮绿灯
for(int i=14;i>=0;i--)//点亮15秒
{
for(int k=500;k>0;k--)
{
Display(1,i/10);
Display(2, i%10);
}
}
digitalWrite(LEDgreen,LOW);//熄灭绿灯
}
//
void WeiXuan(unsigned char n)//定义选择亮的位置
{
switch(n)
{
case 1:
digitalWrite(d1,HIGH);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
break;
case 2:
digitalWrite(d1, LOW);
digitalWrite(d2, HIGH);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
break;
case 3:
digitalWrite(d1,LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, HIGH);
digitalWrite(d4, LOW);
break;
case 4:
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, HIGH);
break;
default :
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
break;
}
}
void Num_0()//数字0
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(h,HIGH);
}
void Num_1()//数字1
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(h,HIGH);
}
void Num_2()//数字2
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_3()//数字3
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_4()//数字4
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_5()//数字5
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_6()//数字6
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_7()//数字7
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(h,HIGH);
}
void Num_8()//数字8
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Num_9()//数字9
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(h,HIGH);
}
void Clear() //清屏
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(h,HIGH);
}
void pickNumber(unsigned char n)//选择亮数字的,将前面定义好的数字直接调用
{
switch(n)
{
case 0:Num_0();break;
case 1:Num_1();break;
case 2:Num_2();break;
case 3:Num_3();break;
case 4:Num_4();break;
case 5:Num_5();break;
case 6:Num_6();break;
case 7:Num_7();break;
case 8:Num_8();break;
case 9:Num_9();break;
default:Clear();break;
}
}
void Display(unsigned char x, unsigned char Number)//选择对应的位置和数字
{
WeiXuan(x);
pickNumber(Number);
delay(1);
Clear() ;
}
红绿灯+倒计时文章来源地址https://www.toymoban.com/news/detail-545981.html
文章来源:https://www.toymoban.com/news/detail-545981.html
到了这里,关于【Arduino】arduino交通灯(红绿灯+倒计时)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!