基于arduino平台,用c语言设计的红绿灯程序,工程文件格式是ino,直接用arduino ide打开就可以用的压缩包包含工程代码和原理图
#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);
}
评论