在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例嵌入式开发 → 基于arduino的红绿灯程序,包含原理图

基于arduino的红绿灯程序,包含原理图

嵌入式开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:0.07M
  • 下载次数:15
  • 浏览次数:311
  • 发布时间:2020-10-21
  • 实例类别:嵌入式开发
  • 发 布 人:airhejoin
  • 文件格式:.rar
  • 所需积分:6
 相关标签: arduino 红绿灯程序 ino 程序

实例介绍

【实例简介】基于arduino平台,用c语言设计的红绿灯程序,工程文件格式是ino,直接用arduino ide打开就可以用的

压缩包包含工程代码和原理图

【实例截图】

from clipboardfrom clipboardfrom clipboard

【核心代码】


#define GREEN_TIME 10
#define YELLOW_TIME 3
#define URGENCY_TIME 20
const int DongIn   = A0;  // Analog input pin that the potentiometer is attached to
const int BeiIn   = A1;  // Analog input pin that the potentiometer is attached to
const int XiIn    = A2;  // Analog input pin that the potentiometer is attached to
const int NanIn   = A3;  // Analog input pin that the potentiometer is attached to

const int DongGreen = 2;  // Analog input pin that the potentiometer is attached to
const int DongYellow = 3;  // Analog input pin that the potentiometer is attached to
const int DongRed = 4;  // Analog input pin that the potentiometer is attached to

const int BeiGreen = 5;  // Analog input pin that the potentiometer is attached to
const int BeiYellow = 6;  // Analog input pin that the potentiometer is attached to
const int BeiRed = 7;  // Analog input pin that the potentiometer is attached to

const int XiGreen = 8;  // Analog input pin that the potentiometer is attached to
const int XiYellow = 9;  // Analog input pin that the potentiometer is attached to
const int XiRed = 10;  // Analog input pin that the potentiometer is attached to

const int NanGreen = 11;  // Analog input pin that the potentiometer is attached to
const int NanYellow = 12;  // Analog input pin that the potentiometer is attached to
const int NanRed = 13;  // Analog input pin that the potentiometer is attached to

#define STA_OFF HIGH
#define STA_ON LOW
#define DIFFERNCE 30


int AdcValue[4]= {0};        // value read from the pot

int alarm_status=0;
int led_time=0,led_count=0;
int alarm_time=0,alarm_count=0,alarm_delay=0;
int Counter=0;
int i=0;
void setup() {
  // initialize serial communications at 9600 bps:
  for(i=2;i<14;i )
  {
    pinMode(i, OUTPUT);
    LedSwitch(i,STA_OFF);
  }
  Serial.begin(9600);

}

void loop() {
  int id=0,max_value=0;
  AdcValue[0]=   analogRead(DongIn);
  AdcValue[1]=    analogRead(BeiIn);
  AdcValue[2]=     analogRead(XiIn);
  AdcValue[3]=    analogRead(NanIn);
  #if 1
  Serial.print(AdcValue[0]);
  Serial.print(' ');
  Serial.print(AdcValue[1]);
  Serial.print(' ');
  Serial.print(AdcValue[2]);
  Serial.print(' ');
  Serial.println(AdcValue[3]);
  #endif
  #if 1
  id=GetMax(AdcValue[0],AdcValue[1],AdcValue[2],AdcValue[3]);
  max_value=AdcValue[id-1];
  AdcValue[id-1]=0;//将最大变为0,求然后再求最大值,求出来的最大值就是第二大的数,
  id=GetMax(AdcValue[0],AdcValue[1],AdcValue[2],AdcValue[3]);
  if(max_value>(AdcValue[id-1] DIFFERNCE))//对比最大的数和第二大的数,如果差值大于DIFFERNCE,代表有警报声,进入紧急状态
  {
    if(alarm_count==0)
    {
      alarm_status=1;
      alarm_count=URGENCY_TIME*10;
      alarm_time=URGENCY_TIME*10; 
      //Serial.println("urgency on");     
    }
  }
  else
  {
    if(alarm_count)
    {
      alarm_count--;
      if(alarm_count==0)
      {
        alarm_status=0;
        //Serial.println("urgency off");
      }
    }
  }
#endif
  Led_Process();
  delay(100);
}
int GetMax(int a,int b,int c,int d)
{
  int temp=1,m=0;
  m=a;
  if(b>m)
  {
    m=b;
    temp=2;
  }
  if(c>m)
  {
    m=c;
    temp=3;
  }
  if(d>m)
  {
    m=d;
    temp=4;
  }
  return temp;
}
void Led_Process(void)//100ms调用一次
{
  if(alarm_status==0)
  {
    if( led_count>led_time)
    {
      //Serial.println("alarm=0");
      led_count=0;
      //Counter=1;
      switch(Counter)
      {
        case 0:
          LedSwitch(XiRed,STA_ON);     LedSwitch(BeiRed,STA_ON);
          LedSwitch(NanGreen,STA_OFF);  LedSwitch(NanYellow,STA_OFF);   LedSwitch(NanRed,STA_ON);
          LedSwitch(DongGreen,STA_ON);  LedSwitch(DongYellow,STA_OFF);  LedSwitch(DongRed,STA_OFF);
          led_time=GREEN_TIME*10;
          Counter ;
          break;
        case 1:
          LedSwitch(DongGreen,STA_OFF); LedSwitch(DongYellow,STA_ON);   LedSwitch(DongRed,STA_OFF);
          led_time=YELLOW_TIME*10;
          Counter ;
          break;
        case 2:
          LedSwitch(DongGreen,STA_OFF); LedSwitch(DongYellow,STA_OFF);  LedSwitch(DongRed,STA_ON);
          LedSwitch(BeiGreen,STA_ON);   LedSwitch(BeiYellow,STA_OFF);   LedSwitch(BeiRed,STA_OFF);
          led_time=GREEN_TIME*10;
          Counter ;
          break;
        case 3:
          LedSwitch(BeiGreen,STA_OFF);  LedSwitch(BeiYellow,STA_ON);   LedSwitch(BeiRed,STA_OFF);
          led_time=YELLOW_TIME*10;
          Counter ;
          break;
        case 4:
          LedSwitch(BeiGreen,STA_OFF);  LedSwitch(BeiYellow,STA_OFF);  LedSwitch(BeiRed,STA_ON);
          LedSwitch(XiGreen,STA_ON);    LedSwitch(XiYellow,STA_OFF);    LedSwitch(XiRed,STA_OFF);
          led_time=GREEN_TIME*10;
          Counter ;
          break;
        case 5:
          LedSwitch(XiGreen,STA_OFF);   LedSwitch(XiYellow,STA_ON);   LedSwitch(XiRed,STA_OFF);
          led_time=YELLOW_TIME*10;
          Counter ;
          break;
        case 6:
          LedSwitch(XiGreen,STA_OFF);   LedSwitch(XiYellow,STA_OFF);  LedSwitch(XiRed,STA_ON);
          LedSwitch(NanGreen,STA_ON);   LedSwitch(NanYellow,STA_OFF);  LedSwitch(NanRed,STA_OFF);
          led_time=GREEN_TIME*10;
          Counter ;
          break;
       case 7:
          LedSwitch(NanGreen,STA_OFF);  LedSwitch(NanYellow,STA_ON);   LedSwitch(NanRed,STA_OFF);
          led_time=YELLOW_TIME*10;
          Counter=0;
          break;      
      }
    }
  }
  else
  {//紧急模式
    //if( alarm_count>alarm_time)
    {
      //Serial.println("alarm=1");
      
      LedSwitch(DongGreen,STA_OFF);  LedSwitch(DongYellow,STA_OFF);  LedSwitch(DongRed,STA_ON);
      LedSwitch(BeiGreen,STA_OFF);  LedSwitch(BeiYellow,STA_OFF);  LedSwitch(BeiRed,STA_ON);
      LedSwitch(XiGreen,STA_OFF);    LedSwitch(XiYellow,STA_OFF);    LedSwitch(XiRed,STA_ON);
      LedSwitch(NanGreen,STA_OFF);  LedSwitch(NanYellow,STA_OFF);   LedSwitch(NanRed,STA_ON);
      
      Counter=0;
      led_count=0;
      led_time=0;      
    }
  }
}
void LedSwitch(int id,int sta)
{
  digitalWrite(id, sta);
}

实例下载地址

基于arduino的红绿灯程序,包含原理图

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警