int a=2;//定义数字接口2 连接a 段数码管
int b=3;// 定义数字接口3 连接b 段数码管
int c=4;// 定义数字接口4 连接c 段数码管
int d=5;// 定义数字接口5 连接d 段数码管
int e=6;// 定义数字接口6 连接e 段数码管
int f=7;// 定义数字接口7 连接f 段数码管
int g=8;// 定义数字接口8 连接g 段数码管
int dp=9;// 定义数字接口9 连接dp 段数码管
int Greenpin=12;//定义红灯引脚
int yellowpin=13;//定义黄灯引脚
int Redpin=A0;//定义红灯引脚
int Yellowpin=A1;//定义黄灯2引脚
int HR1=A2;//定义1号路红灯人行横道引脚
int GR1=A3;//定义1号路绿灯灯人行横道引脚
void digital_0(void) //显示数字0
{
unsigned char i;
digitalWrite(g,HIGH);
digitalWrite(dp,HIGH);
for(i=2;i<=7;i++)
digitalWrite(i,LOW);
}
void digital_1(void) //显示数字1
{
unsigned char i;
digitalWrite(b,LOW);//给数字接口3 引脚高电平,点亮b段
digitalWrite(c,LOW);//点亮c 段
for(i=5;i<=9;i++)//熄灭其余段
digitalWrite(i,HIGH);
digitalWrite(a,HIGH);
}
void digital_2(void) //显示数字2
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(g,LOW);
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
digitalWrite(dp,HIGH);
}
void digital_3(void) //显示数字3
{
unsigned char i;
for( i=2;i<=5;i++)//熄灭其余段
digitalWrite(i,LOW);
digitalWrite(g,LOW);
digitalWrite(f,HIGH);
digitalWrite(e,HIGH);
digitalWrite(dp,HIGH);
}
void digital_4(void) //显示数字4
{
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(a,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(dp,HIGH);
}
void digital_5(void) //显示数字5
{
digitalWrite(a, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(b,HIGH);
digitalWrite(e,HIGH);
digitalWrite(dp,HIGH);
}
void digital_6(void) //显示数字6
{
unsigned char i;
for(i=4;i<=8;i++)
digitalWrite(i,LOW);
digitalWrite(b,HIGH);
digitalWrite(dp,HIGH);
}
void digital_7(void) //显示数字7
{
unsigned char i;
for(i=2;i<=4;i++)
digitalWrite(i,LOW);
for(i=5;i<=9;i++)
digitalWrite(i,HIGH);
}
void digital_8(void) //显示数字8
{
for( int i=2;i<=8;i++)
digitalWrite(i,LOW);
digitalWrite(dp,HIGH);
}
void digital_9(void) //显示数字9
{
unsigned char i;
for(i=2;i<=5;i++)
digitalWrite(i,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(e,HIGH);
digitalWrite(dp,HIGH);
}
void setup()
{
int i;//定义变量
pinMode (Redpin,OUTPUT);
pinMode (Yellowpin,OUTPUT);
pinMode (HR1,OUTPUT);
pinMode (GR1,OUTPUT);
for(i=2;i<=13;i++)
pinMode(i,OUTPUT);//设置2~11 引脚为输出模式
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
}
void loop() {
for( int a=0;a<=15;a++)
{
switch( a)
{
case 0:
for(int t=0;t<200;t++)
{
digitalWrite(10,HIGH);//十位15
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_5() ; //5
digitalWrite(Greenpin,HIGH); //15s倒计时开始的同时点亮(1号路绿灯)和另一条马路的(2号路红灯)
digitalWrite(HR1,HIGH);//一号路汽车亮绿灯所有一号路人行道红灯点亮(同时2号路绿灯熄灭)
delay(2) ;}
break;
case 1:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位14
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_4() ; //4
delay(2) ;}
break;
case 2:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位13
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_3() ; //3
delay(2) ;}
break;
case 3:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位12
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_2() ; //2
delay(2) ;}
break;
case 4:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位11
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_1() ; //1
delay(2) ;}
break;
case 5:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位10
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_0() ; //0
delay(2) ;}
break;
case 6:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位09
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_9() ; //9
delay(2) ;}
break;
case 7:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位08
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_8() ; //8
delay(2) ;}
break;
case 8:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位07
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_7() ; //7
delay(2);}
break;
case 9:
for(int t=0;t<200;t++)
{ digitalWrite(10,HIGH);//十位06
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_6() ; //6
delay(2);}
break;
case 10:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位05
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_5() ; //5
delay(1);}
break;
case 11:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位04
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_4(); //4
delay(1);}
break;
case 12:
for(int t=0;t<250;t++){
digitalWrite(10,HIGH);//十位03
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_3(); //3
delay(1);}
break;
case 13:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位02
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_2(); //2
delay(1);}
break;
case 14:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位01
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_1(); //1
delay(1);}
break;
case 15:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位00
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_0(); //0
digitalWrite(Greenpin,LOW);//倒计时结束的同时(绿灯)熄灭及对应的(红灯)
digitalWrite(HR1,LOW);//一号路人行道红灯熄灭同时2号路绿灯熄灭
delay(1);}
break;
} //switch
} //for(i=0;)
//黄灯闪烁3秒
digitalWrite(yellowpin,HIGH);
delay(1000);
digitalWrite(yellowpin,LOW);
delay(1000);
digitalWrite(yellowpin,HIGH);
delay(1000);
digitalWrite(yellowpin,LOW);
delay(1000);
digitalWrite(yellowpin,HIGH);
delay(1000);
digitalWrite(yellowpin,LOW);
//2号路的情况
for( int a=0;a<=15;a++)
{
switch( a)
{
case 0:
for(int t=0;t<200;t++)
{
digitalWrite(10,HIGH);//十位15
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_5() ; //5
digitalWrite(Redpin,HIGH); //15s倒计时开始的同时点亮(1号路红灯)和另一条马路的(2号路绿灯)
digitalWrite(GR1,HIGH);//1号路人行到的绿的点亮(同时2号路的人行道红灯点亮)
delay(2) ;}
break;
case 1:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位14
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_4() ; //4
delay(2) ;}
break;
case 2:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位13
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_3() ; //3
delay(2) ;}
break;
case 3:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位12
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_2() ; //2
delay(2) ;}
break;
case 4:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位11
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_1() ; //1
delay(2) ;}
break;
case 5:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位10
digitalWrite(11,LOW);
digital_1(); //1
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_0() ; //0
delay(2) ;}
break;
case 6:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位09
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_9() ; //9
delay(2) ;}
break;
case 7:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位08
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_8() ; //8
delay(2) ;}
break;
case 8:
for(int t=0;t<200;t++)
{digitalWrite(10,HIGH);//十位07
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_7() ; //7
delay(2);}
break;
case 9:
for(int t=0;t<200;t++)
{ digitalWrite(10,HIGH);//十位06
digitalWrite(11,LOW);
digital_0(); //0
delay(2);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_6() ; //6
delay(2);}
break;
case 10:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位05
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_5() ; //5
delay(1);}
break;
case 11:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位04
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_4(); //4
delay(1);}
break;
case 12:
for(int t=0;t<250;t++){
digitalWrite(10,HIGH);//十位03
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_3(); //3
delay(1);}
break;
case 13:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位02
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_2(); //2
delay(1);}
break;
case 14:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位01
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_1(); //1
delay(1);}
break;
case 15:
for(int t=0;t<400;t++){
digitalWrite(10,HIGH);//十位00
digitalWrite(11,LOW);
digital_0(); //0
delay(1);
digitalWrite(10,LOW);//个位
digitalWrite(11,HIGH);
digital_0(); //0
digitalWrite(Redpin,LOW);//倒计时结束的同时(红灯)熄灭及对应的(绿灯)
digitalWrite(GR1Q,LOW);
delay(1);}
break;
} //switch
} //for(i=0;)
//黄灯闪烁3秒
digitalWrite(Yellowpin,HIGH);
delay(1000);
digitalWrite(Yellowpin,LOW);
delay(1000);
digitalWrite(Yellowpin,HIGH);
delay(1000);
digitalWrite(Yellowpin,LOW);
delay(1000);
digitalWrite(Yellowpin,HIGH);
delay(1000);
digitalWrite(Yellowpin,LOW);
}//loop
|