当前位置: 首页 > >

利用arduino UNO制作的一个带倒计时的交通信号灯系统

发布时间:

利用arduino UNO制作的一个带倒计时的交通信号灯系统

大一新生,刚开始学arduino,看的是程晨老师的《Arduino 电子设计实战指南 零基础篇》,有一道课后题是制作一个带倒计时的交通信号灯,利用实验室器材花了一个多小时时间做出来的。使用arduino uno开发板和共阳极数码管,其中红灯倒计时由9到0,绿灯倒计时由5到0,黄灯倒计时由2到0。
实物图


原理图

代码如下,过于粗糙,欢迎大家斧正。


int pinNum;
int redLed = 10;
int yellowLed = 11;
int greenLed = 12;
int smgtwo = 2;
int smgthree = 3;
int smgfour = 4;
int smgfive = 5;
int smgsix = 6;
int smgseven = 7;
int smgeight = 8;
int smgnine = 9;
void trafficLights(int red,int yellow,int green)
{
digitalWrite(redLed ,red);
digitalWrite(yellowLed ,yellow);
digitalWrite(greenLed ,green);
}
void smg(int two,int three,int four,int five,int six,int seven,int eight,int nine)
{
digitalWrite(smgtwo,two);
digitalWrite(smgthree,three);
digitalWrite(smgfour,four);
digitalWrite(smgfive,five);
digitalWrite(smgsix,six);
digitalWrite(smgseven,seven);
digitalWrite(smgeight,eight);
digitalWrite(smgnine,nine);
}
void setup()
{
for(pinNum=2;pinNum<=12;pinNum++)
{
pinMode(pinNum,OUTPUT);
}
}

void loop()
{
trafficLights(HIGH,LOW,LOW);
smg(HIGH,HIGH,HIGH,HIGH,LOW,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);
delay(1000);
smg(LOW,LOW,HIGH,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(HIGH,HIGH,HIGH,LOW,HIGH,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,HIGH,HIGH,LOW,LOW,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,HIGH,LOW,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(HIGH,LOW,HIGH,HIGH,LOW,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,LOW,HIGH,HIGH,HIGH,HIGH,LOW,LOW);
delay(1000);
smg(LOW,LOW,LOW,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(LOW,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);
delay(1000);

trafficLights(LOW,HIGH,LOW);
smg(HIGH,HIGH,HIGH,LOW,LOW,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,HIGH,LOW,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(HIGH,LOW,HIGH,HIGH,LOW,HIGH,HIGH,LOW);
delay(1000);
smg(HIGH,LOW,HIGH,HIGH,HIGH,HIGH,LOW,LOW);
delay(1000);
smg(LOW,LOW,LOW,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(LOW,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);
delay(1000);

trafficLights(LOW,LOW,HIGH);
smg(HIGH,LOW,HIGH,HIGH,HIGH,HIGH,LOW,LOW);
delay(1000);
smg(LOW,LOW,LOW,HIGH,LOW,LOW,HIGH,LOW);
delay(1000);
smg(LOW,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);
delay(1000);
}

这里自定义了两个函数,一个trafficLights用来控制红绿灯的亮灭,一个swg用来控制数码管显示的数字。
本人是大一新生某不知名邮电大学电子信息类专业,对硬件方面非常感兴趣,欢迎各位私聊交流。



友情链接: