hello大家好,今天早上过马路时等红绿灯时,也想自己做一个玩玩,代码如下,由于作者水平有限,以下代码仅供参考,使用开发板为arduino uno
#define redLed A2
#define greeLed A0
#define yelloLed A1
int redDS=15;
int greenDS=15;
int yellowDS=5;
char signal;
int a = 9;
int b = 8;
int c = 7;
int d = 6;
int e = 5;
int f = 4;
int g = 3;
int dp = 2;
int d4 = 13;
int d3 = 12;
int d2 = 11;
int d1 = 10;
long n = 12030;
int x = 100;
int del = 55;
void setup()
{
Serial.begin(9600);
for(int a=2;a<=13;a++)
{
pinMode(a,OUTPUT);
}
for(int A0=2;A0<=A2;A0++)
{
pinMode(A0,OUTPUT);
}
}
void loop()
{
jiaotongdeng();
}
void WX(unsigned char x)//数码管位选
{
switch(x)
{
case 1:
digitalWrite(d1,LOW);
digitalWrite(d2,HIGH);
digitalWrite(d3,HIGH);
digitalWrite(d4,HIGH);
break;
case 2:
digitalWrite(d1,HIGH);
digitalWrite(d2,LOW);
digitalWrite(d3,HIGH);
digitalWrite(d4,HIGH);
break;
case 3:
digitalWrite(d1,HIGH);
digitalWrite(d2,HIGH);
digitalWrite(d3,LOW);
digitalWrite(d4,HIGH);
break;
case 4:
digitalWrite(d1,HIGH);
digitalWrite(d2,HIGH);
digitalWrite(d3,HIGH);
digitalWrite(d4,LOW);
break;
default:
digitalWrite(d1,HIGH);
digitalWrite(d2,HIGH);
digitalWrite(d3,HIGH);
digitalWrite(d4,HIGH);
break;
}
}
unsigned char number[10][8] =
{ //a b c d e f g dp
{1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 1, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 0, 1, 1, 0}
};
void Clear()//清屏函数
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
}
void DX(unsigned char x)//数码管段选
{
digitalWrite(a,number[x][0]); //a查询码值表
digitalWrite(b,number[x][1]);
digitalWrite(c,number[x][2]);
digitalWrite(d,number[x][3]);
digitalWrite(e,number[x][4]);
digitalWrite(f,number[x][5]);
digitalWrite(g,number[x][6]);
digitalWrite(dp,number[x][7]);
}
void display(unsigned char y,unsigned char Number)
{
WX(y);
DX(Number);
Clear();
}
void jiaotongdeng()
{
if(Serial.available()>0){
signal=Serial.read();
Serial.print(signal);
if(signal=='A'){//修改红灯定时时间0~99s
if(Serial.available()>0){
redDS=Serial.read()-48;
if(Serial.available()>0){
int zifu=Serial.read()-48;
redDS=redDS*10+zifu;
Serial.print("-->");
Serial.println(redDS);
}
}
}
if(signal=='B'){//修改绿灯定时时间0~99s
if(Serial.available()>0){
greenDS=Serial.read()-48;
Serial.println(greenDS);
if(Serial.available()>0){
int zifu=Serial.read()-48;
greenDS=greenDS*10+zifu;
Serial.println(greenDS);
}
}
}
if(signal=='C'){//修改黄灯定时时间0~99s
if(Serial.available()>0){
yellowDS=Serial.read()-48;
Serial.println(yellowDS);
if(Serial.available()>0){
int zifu=Serial.read()-48;
yellowDS=yellowDS*10+zifu;
Serial.println(yellowDS);
}
}
}
}
digitalWrite(redLed,HIGH);
for(int i=redDS;i>=0;i--){
for(int k=5000;k>0;k--){
display(1,i/10);
display(2, i%10);
}
}
digitalWrite(redLed,LOW);
digitalWrite(yelloLed,HIGH);
for(int i=yellowDS;i>=0;i--){
for(int k=5000;k>0;k--){
display(2,i/10);
display(3, i%10);
}
}
digitalWrite(yelloLed,LOW);
digitalWrite(greeLed,HIGH);
for(int i=greenDS;i>=0;i--){
for(int k=5000;k>0;k--){
display(3,i/10);
display(4, i%10);
}
}
digitalWrite(greeLed,LOW);
}最后有个不情之请,如果您会使用0x..十六进制写法改进我的代码,请发到评论区
|