概述
【Arduino】交通灯控制设计 四位数码管,4位数字倒计时,带蜂鸣器播放音乐,串口输入改变数码管数字
- 前言
- 一、实验器材
- 二、连线示意图
- 三、代码如下
- 2.实物连接图如下:
- 总结
【Arduino】交通灯控制设计 四位数码管,4位数字倒计时,带蜂鸣器播放音乐,串口输入改变数码管数字
)
前言
一、实验器材
UNO控制板:1块
共阳极四位数码管:1个
面包板:1块
面包板跳线:若干
220Ω电阻:4个
1K电阻:3个
红色led灯-1
绿色led灯-1
黄色led灯-1
二、连线示意图
注意:A5、A4、A3为红黄绿灯引脚端口,A0为蜂鸣器端口
三、代码如下
#define redLed A5//红灯引脚定义
#define yellowLed A4//黄灯引脚定义
#define greenLed A3//绿灯灯引脚定义
int speakerPin = A0; //蜂鸣器引脚定义
int numred=1;//红灯定时时间
int numgreen=2;//绿灯定时时间
int numyellow=3;//黄灯定时时间
char signal;
//设置阴极接口
int a = 2; //从2号位开始连接的
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
//设置阳极接口
int d4 = 13;
int d3 = 12;
int d2 = 11;
int d1 = 10;
//设置变量
long n = 0;
int x = 100;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) { //蜂鸣器音色
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void redchange(){
if(signal=='A'){//修改红灯定时时间0~99s
if(Serial.available()>0){
numred=Serial.read()-48;
if(Serial.available()>0){
int zifu=Serial.read()-48;
numred=numred*10+zifu;
Serial.print("-->");
Serial.println(numred);
}
}
}
}
void greenchange(){
if(signal=='B'){//修改绿灯定时时间0~99s
if(Serial.available()>0){
numgreen=Serial.read()-48;
Serial.println(numgreen);
if(Serial.available()>0){
int zifu=Serial.read()-48;
numgreen=numgreen*10+zifu;
Serial.print("-->");
Serial.println(numgreen);
}
}
}
}
void yellowchange(){
if(signal=='C'){//修改黄灯定时时间0~99s
if(Serial.available()>0){
numyellow=Serial.read()-48;
Serial.println(numyellow);
if(Serial.available()>0){
int zifu=Serial.read()-48;
numyellow=numyellow*10+zifu;
Serial.print("-->");
Serial.println(numyellow);
}
}
}
}
void fengmingqiplay(){
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
void ledchange(){
if(Serial.available()>0){
signal=Serial.read();
Serial.print(signal);
redchange();
greenchange();
yellowchange();
}
}
void setup()
{
Serial.begin(9600); //设置串口波特率
while (!Serial){}
pinMode(d1, OUTPUT); //把对应的LED都设置成输出
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(p, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
Serial.println("请您选择你要修改的灯的颜色");
Serial.println("【A是绿灯】");
Serial.println("【B是红灯】");
Serial.println("【C是黄灯】");
pinMode(speakerPin, OUTPUT);
}
String inString="";
void loop(){
attachInterrupt(digitalPinToInterrupt(0), ledchange, FALLING );
ledchange();
interrupts();
digitalWrite(redLed,HIGH);
for(int i=numred;i>=0;i--){
for(int k=250;k>0;k--){
Display(4,i%10);//调整个位的数字变化
Display(3,i/10%10);//调整十位的数字变化
Display(2,i%1000/100);//调整百位的数字变化
Display(1,i/1000); //调整千位的数字变化
}
}
digitalWrite(redLed,LOW);
digitalWrite(yellowLed,HIGH);
for(int i=numyellow;i>=0;i--){
for(int k=250;k>0;k--){
Display(4,i%10);//调整个位的数字变化
Display(3,i/10%10);//调整十位的数字变化
Display(2,i%1000/100);//调整百位的数字变化
Display(1,i/1000); //调整千位的数字变化
}
}
digitalWrite(yellowLed,LOW);
//fengmingqiplay();
digitalWrite(greenLed,HIGH);
for(int i=numgreen;i>=0;i--){
for(int k=250;k>0;k--){
Display(4,i%10);//调整个位的数字变化
Display(3,i/10%10);//调整十位的数字变化
Display(2,i%1000/100);//调整百位的数字变化
Display(1,i/1000); //调整千位的数字变化
}
}
digitalWrite(greenLed,LOW);
}
///
void shumaguan(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()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(p,HIGH);
}
void Num_1()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(p,HIGH);
}
void Num_2()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_3()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_4()
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_5()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_6()
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_7()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(p,HIGH);
}
void Num_8()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p,HIGH);
}
void Num_9()
{
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(p,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(p,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)
{
shumaguan(x);
pickNumber(Number);
delay(1);
Clear() ;
}
2.实物连接图如下:
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了四位数码管的使用,一位数码管的控制,通过程序来使数码管点亮或熄灭,但是遇到多位数字,我们又该怎么显示就有所不同,所以,四位数码管的控制,四位数码管并不是简单的1+1+1+1的样子。
参考链接
1、https://blog.csdn.net/weixin_43319452/article/details/109361825
2、https://www.arduino.cn/forum.php?mod=viewthread&tid=106891&highlight=%E6%95%B0%E7%A0%81%E7%AE%A1
3、https://blog.csdn.net/weixin_33916256/article/details/91891142?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-5.pc_relevant_default&spm=1001.2101.3001.4242.4&utm_relevant_index=6
最后
以上就是香蕉小鸭子为你收集整理的【Arduino】交通灯控制设计 四位数码管,4位数字倒计时,带蜂鸣器播放音乐,串口输入改变数码管数字前言一、实验器材二、连线示意图总结的全部内容,希望文章能够帮你解决【Arduino】交通灯控制设计 四位数码管,4位数字倒计时,带蜂鸣器播放音乐,串口输入改变数码管数字前言一、实验器材二、连线示意图总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复