Arduino智能硬件项目:智能红绿灯
本帖最后由 SimpleChen 于 2024-10-7 12:11 编辑所需器材:(将文字拷贝至创客商城即可找到,文末附有网页)
[*]DFRduino UNO R3(含USB线|传感器扩展板)*1
[*]Gravity: 数字食人鱼红色LED发光模块(Arduino兼容)*2
[*]Gravity: 数字食人鱼黄色LED发光模块(Arduino兼容)*2
[*]Gravity: 数字食人鱼绿色LED发光模块(Arduino兼容)*2
[*]Gravity: 数字大按钮模块 黄色*1
[*]Gravity: 数字蜂鸣器模块(Arduino兼容)*1
接线图:(为了使图片更加简洁,本图舍去了VCC和GND线的连接,并且本图中所有连线相交处均不连接,接线图在文末)
上传模式ArduinoC代码:(Mind+程序在文末)/*!
* MindPlus
* uno
*
*/
// 动态变量
volatile float mind_n_switchValue;
// 函数声明
void DF_alarm();
// 主程序开始
void setup() {
}
void loop() {
mind_n_switchValue = digitalRead(2);
if ((mind_n_switchValue==1)) {
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(11, LOW);
digitalWrite(9, HIGH);
}
else {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(9, LOW);
digitalWrite(11, HIGH);
delay(5000);
for (int index = 0; index < 10; index++) {
digitalWrite(11, HIGH);
DF_alarm();
digitalWrite(11, LOW);
DF_alarm();
}
}
}
// 自定义函数
void DF_alarm() {
for (int index = 0; index < 50; index++) {
digitalWrite(12, HIGH);
delay(1);
digitalWrite(12, LOW);
delay(1);
}
}
Python模式代码:(Mind+程序在文末)#-*- coding: UTF-8 -*-
# MindPlus
# Python
import time
from pinpong.board import Pin
from pinpong.board import Board
# 自定义函数
def alarm():
for i in range(1, 50, 1):
buzzerPin.write_digital(1)
time.sleep(0.001)
buzzerPin.write_digital(0)
time.sleep(0.001)
Board("uno").begin()
global greenLedPin1
greenLedPin1 = 6
global yellowLedPin1
yellowLedPin1 = 5
global redLedPin1
redLedPin1 = 4
global greenLedPin2
greenLedPin2 = 11
global redLedPin2
redLedPin2 = 9
global switchPin
switchPin = 2
global buzzerPin
buzzerPin = 12
global switchValue
switchValue = 0
greenLedPin1 = Pin(greenLedPin1, Pin.OUT)
yellowLedPin1 = Pin(yellowLedPin1, Pin.OUT)
redLedPin1 = Pin(redLedPin1, Pin.OUT)
greenLedPin2 = Pin(greenLedPin2, Pin.OUT)
redLedPin2 = Pin(redLedPin2, Pin.OUT)
buzzerPin = Pin(buzzerPin, Pin.OUT)
switchPin = Pin(switchPin, Pin.IN)
while True:
switchValue = switchPin.read_digital()
if (switchValue == 1):
greenLedPin1.write_digital(1)
yellowLedPin1.write_digital(0)
redLedPin1.write_digital(0)
greenLedPin2.write_digital(0)
redLedPin2.write_digital(1)
else:
greenLedPin1.write_digital(0)
yellowLedPin1.write_digital(1)
time.sleep(1)
yellowLedPin1.write_digital(0)
redLedPin1.write_digital(1)
redLedPin2.write_digital(0)
greenLedPin2.write_digital(1)
time.sleep(5)
for i in range(1, 10, 1):
greenLedPin2.write_digital(1)
alarm()
greenLedPin2.write_digital(0)
alarm()
运行效果:
上传程序后,主路的绿色LED灯和人行道的红色LED灯亮,当按键按下后,主路绿灯灭,黄灯亮,延时一段时间后,主路红灯亮,人行道红灯灭,绿灯亮,再延时一段时间,人行道绿灯闪烁,蜂鸣器发声,然后恢复到主路通行状态绿灯亮。
页:
[1]