制作交通灯代码:南北方向绿灯30s,黄灯3s,东西方向绿灯15s,黄灯3s,利用数码管倒计时,红黄绿灯用LED显示
时间: 2023-07-14 09:13:11 浏览: 129
VHDL语言交通灯代码
以下是一个简单的交通灯控制程序,使用Arduino IDE编写,需要连接4个LED和一个数码管。其中,南北方向的绿灯使用LED1,东西方向的绿灯使用LED2,黄灯和红灯分别使用LED3和LED4,数码管显示倒计时。
```c
// 定义引脚
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
const int DIGIT1 = 6;
const int DIGIT2 = 7;
// 定义状态
const int STATE_NS_GREEN = 0;
const int STATE_NS_YELLOW = 1;
const int STATE_EW_GREEN = 2;
const int STATE_EW_YELLOW = 3;
// 定义时间参数
const int NS_GREEN_TIME = 30;
const int NS_YELLOW_TIME = 3;
const int EW_GREEN_TIME = 15;
const int EW_YELLOW_TIME = 3;
// 定义计时器
int timer = 0;
// 定义当前状态
int state = STATE_NS_GREEN;
void setup() {
// 初始化引脚
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(DIGIT1, OUTPUT);
pinMode(DIGIT2, OUTPUT);
}
void loop() {
// 状态机控制交通灯
switch (state) {
case STATE_NS_GREEN:
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
displayTime(NS_GREEN_TIME);
if (timer >= NS_GREEN_TIME) {
state = STATE_NS_YELLOW;
timer = 0;
}
break;
case STATE_NS_YELLOW:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
displayTime(NS_YELLOW_TIME);
if (timer >= NS_YELLOW_TIME) {
state = STATE_EW_GREEN;
timer = 0;
}
break;
case STATE_EW_GREEN:
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
displayTime(EW_GREEN_TIME);
if (timer >= EW_GREEN_TIME) {
state = STATE_EW_YELLOW;
timer = 0;
}
break;
case STATE_EW_YELLOW:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
displayTime(EW_YELLOW_TIME);
if (timer >= EW_YELLOW_TIME) {
state = STATE_NS_GREEN;
timer = 0;
}
break;
}
// 计时器加1
timer++;
// 等待1秒
delay(1000);
}
// 数码管显示倒计时
void displayTime(int seconds) {
int minute = seconds / 60;
int second = seconds % 60;
digitalWrite(DIGIT1, HIGH);
digitalWrite(DIGIT2, LOW);
displayDigit(minute);
delay(1000);
digitalWrite(DIGIT1, LOW);
digitalWrite(DIGIT2, HIGH);
displayDigit(second);
delay(1000);
}
// 数码管显示数字
void displayDigit(int digit) {
switch (digit) {
case 0:
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
break;
case 1:
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
break;
case 2:
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
break;
case 3:
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
break;
case 4:
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
break;
case 5:
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
break;
case 6:
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
break;
case 7:
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
break;
case 8:
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
break;
case 9:
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
break;
}
}
```
需要注意的是,此程序仅为示例,实际使用时需要根据实际需要进行修改和调整。例如,需要根据实际情况设置引脚和时间参数,以及添加必要的安全措施,如红灯闪烁等。
阅读全文