7835| 4
|
[入门] Arduino控制交互式LED交通信号灯 |
------------------------------------------------------ 小白刚入门,做了一个小小的交互式LED信号灯的实验 各位大神不要见笑,本来不想拿出来,纯属刷帖刷经验 ----------------------------------------------------------- 代码如下: //绑定各个引脚到各汽车信号灯 int carred = 12; int caryellow = 11; int cargreen = 10; //声明等待时长 int timedelay = 5000; //绑定引脚到行人型号灯 int pedred = 8; int button = 9; int pedgreen = 7; unsigned long changeTime; //记录按下按钮后时间的变量 void setup() { // 初始化各个引脚为输出模式 pinMode(carred,OUTPUT); pinMode(caryellow,OUTPUT); pinMode(cargreen,OUTPUT); pinMode(pedred,OUTPUT); pinMode(pedgreen,OUTPUT); //设置按键为输入模式 pinMode(button,INPUT); digitalWrite(cargreen,HIGH); digitalWrite(pedred,HIGH); } void loop() { //当按键按下去的时候为高电平信号并赋予变量state int state = digitalRead(button); //判断按键是否按下并且当系统启动时间距当前按键动作时间间隔大于5s时执行括号内的函数 if(state == HIGH &&(millis() - changeTime)> 5000){ changeLights(); } } void changeLights(){ digitalWrite(cargreen,LOW); digitalWrite(caryellow,HIGH); delay(2000); digitalWrite(caryellow,LOW); digitalWrite(carred,HIGH); delay(1000); digitalWrite(pedred,LOW); digitalWrite(pedgreen,HIGH); delay(timedelay); for(int x=0;x<10;x++) { digitalWrite(pedgreen,LOW); delay(250); digitalWrite(pedgreen,HIGH); delay(250); } digitalWrite(pedgreen,LOW); digitalWrite(pedred,HIGH); delay(500); digitalWrite(carred,LOW); digitalWrite(caryellow,HIGH); delay(2000); digitalWrite(caryellow,LOW); digitalWrite(cargreen,HIGH); changeTime = millis(); //记录这次按键动作的时间 } --------------------------------------------------- 连线图&效果图: 效果图 1 效果图 2 至于连线也不一定非得按教程上面连线,灵活一点。我没有扩展板,就一个uno 外加面板版 5个LED 若干导线... |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed