我是靠谱客的博主 时尚毛巾,最近开发中收集的这篇文章主要介绍【Arduino】arduino交通灯(红绿灯+倒计时),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#define LEDred A5//红灯引脚定义
#define LEDyellow A4//绿灯引脚定义
#define LEDgreen A3//黄灯灯引脚定义

 //设置阴极接口
 int a = 13;
 int b = 2;
 int c = 3;
 int d = 4;
 int e = 5;
 int f = 6;
 int g = 7;
 int h =8;
 //设置阳极接口
 int d1 = 9;
 int d2 = 10;
 int d3 = 11;
 int d4 = 12;


 void setup()
 {
   pinMode(d1, OUTPUT);
   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(h, OUTPUT);
   pinMode(LEDred, OUTPUT);
   pinMode(LEDyellow, OUTPUT);
   pinMode(LEDgreen, OUTPUT);
 }
void loop()
{
  digitalWrite(LEDred,HIGH);//点亮红灯
  for(int i=14;i>=0;i--)//点亮15秒
  {
    for(int k=500;k>0;k--){//切换秒数的快慢 k的初值越大 切换就越慢 k的初值越小 切换的越快
      //两位的数字分别设置
    Display(1,i/10);//十位
    Display(2, i%10);//个位
    }     
  }
  digitalWrite(LEDred,LOW);//熄灭红灯
  digitalWrite(LEDyellow,HIGH);//点亮黄灯
  for(int i=4;i>=0;i--)//点亮5秒
  {
    for(int k=500;k>0;k--){
    Display(1,i/10);//十位
    Display(2, i%10);//个位
    }
  }
  digitalWrite(LEDyellow,LOW);//熄灭黄灯
  digitalWrite(LEDgreen,HIGH);//点亮绿灯
  for(int i=14;i>=0;i--)//点亮15秒
  {
    for(int k=500;k>0;k--)
    {
    Display(1,i/10);
    Display(2, i%10);  
    }
  }
  digitalWrite(LEDgreen,LOW);//熄灭绿灯
}
//
void WeiXuan(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()//数字0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(h,HIGH);
}
void Num_1()//数字1
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(h,HIGH);
}
void Num_2()//数字2
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_3()//数字3
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_4()//数字4
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_5()//数字5
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_6()//数字6
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_7()//数字7
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(h,HIGH);
}
void Num_8()//数字8
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(h,HIGH);
}
void Num_9()//数字9
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(h,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(h,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)//选择对应的位置和数字
{
  WeiXuan(x);
  pickNumber(Number);
  delay(1);
  Clear() ; 
}

红绿灯+倒计时

最后

以上就是时尚毛巾为你收集整理的【Arduino】arduino交通灯(红绿灯+倒计时)的全部内容,希望文章能够帮你解决【Arduino】arduino交通灯(红绿灯+倒计时)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部