基于Esp8266的点阵时钟-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: zong0001

[项目] 基于Esp8266的点阵时钟

[复制链接]
 楼主| 发表于 2019-3-23 21:58 | 显示全部楼层
代码的后半部分,1楼太长了。因为是一边摸索这个点阵一边编写,功能是逐渐添加的,越来越多,所以有些冗余的代码或变量,也没有去掉。另外编程的习惯不是很好,可读性较差,希望谅解。
[mw_shl_code=arduino,true]void wenshidu(){ //读取温度湿度
int j;
unsigned int loopCnt;
int chr[40] = {0};//创建数字数组,用来存放40个bit
unsigned long time1;
bgn:
// delay(2000);
//设置2号接口模式为:输出
//输出低电平20ms(>18ms)
//输出高电平40μs
pinMode(DHT12pin,OUTPUT);
digitalWrite(DHT12pin,LOW);
delay(20);
digitalWrite(DHT12pin,HIGH);
delayMicroseconds(40);
digitalWrite(DHT12pin,LOW);
//设置2号接口模式:输入
pinMode(DHT12pin,INPUT);
//高电平响应信号
loopCnt=10000;
while(digitalRead(DHT12pin) != HIGH)
{
if(loopCnt-- == 0)
{
//如果长时间不返回高电平,输出个提示,重头开始。
// Serial.println("HIGH");
goto bgn;
}
}
//低电平响应信号
loopCnt=30000;
while(digitalRead(DHT12pin) != LOW)
{
if(loopCnt-- == 0)
{
//如果长时间不返回低电平,输出个提示,重头开始。
// Serial.println("LOW");
goto bgn;
}
}
//开始读取bit1-40的数值
for(int i=0;i<40;i++)
{
while(digitalRead(DHT12pin) == LOW)
{}
//当出现高电平时,记下时间“time”
time1 = micros();
while(digitalRead(DHT12pin) == HIGH)
{}
//当出现低电平,记下时间,再减去刚才储存的time
//得出的值若大于50μs,则为‘1’,否则为‘0’
//并储存到数组里去
if (micros() - time1 >50)
{
chr=1;
}else{
chr=0;
}
}

//湿度,8位的bit,转换为数值
unsigned int humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
unsigned int humi2=chr[8]*128+chr[9]*64+chr[10]*32+chr[11]*16+chr[12]*8+chr[13]*4+chr[14]*2+chr[15];

//温度,8位的bit,转换为数值
unsigned int tempx=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
unsigned int temp2=chr[24]*128+chr[25]*64+chr[26]*32+chr[27]*16+chr[28]*8+chr[29]*4+chr[30]*2+chr[31];
//校对码,8位的bit,转换为数值
//tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
//输出:温度、湿度、校对码
//Serial.print("temp:");
//Serial.print(tempx);
// Serial.print("humi:");
// Serial.println(humi);
//Serial.print(".");
//Serial.println(temp2);
Temp = tempx*10+temp2;
Hum = humi*10+humi2;
// Serial.print("humi2:");
// Serial.println(humi2);
zm[8] = Temp/100;
uint8_t t = Temp%100;
zm[9] = t/10;
zm[10] = t%10;
zm[13] = Hum/100;
zm[14] = Hum/10%10;


}

void TimingISR() //时间
{
second ++;
if(second == 60)
{
minute ++;
if(minute == 60)
{
hour ++;
if(hour == 24){
hour = 0;
}
minute = 0;
}
second = 0;
}
if (hour + minute+second == 0){
httpkey = 0;
tohttps();
}
zm[0] = hour / 10;
zm[1] = hour % 10;
zm[3] = minute / 10;
zm[4] = minute % 10;
zm[6] = second / 10;
zm[7] = second % 10;
for(int i=0;i<8;i++){
zmnew = zm;
}
zmnew[2] = 10;
zmnew[5] = 10;
timenow = hour*720+minute*12+second/5;
if (second%5 == 0){
todingshiqi();

}
readA0 = analogRead(A0);
//getlight(); //I2c亮度读取
if (minute%30 ==0 && second ==0 && baoshi ==0){
baoshi = 1; //测试,一刻钟嘀一下
baoshiMillis = millis();

}

}
void tobell(){ //蜂鸣器
if(webbell ==0){
bell[0] =0;
}
if(bell[0] != bell[1]){
//pcf8574(bell[0]);
digitalWrite(bellpin,(bell[0]));
bell[1] = bell[0];
}


}

void todingshiqi(){
//showmode
unsigned int i=0;
while (timelist[i*2] >0){
if (timenow == timelist[i*2+1]){
httpkey = timelist[i*2];
refsec = 0;
tohttps();
}
i ++;
}

}
void toled(){ //主输出,各种模式都是开始测试的
ledLine++;
if (ledLine ==16)
ledLine = 0;
if (showmode ==2)
outchar(ledLine,(second/5%2)*8);
if (showmode ==1)
outzimu(ledLine);
if (showmode ==0) //关闭模式
outzero();
if (showmode ==6)
outtest(ledLine);
if (showmode == 7)
outshowled(ledLine);
if (showmode == 8)
outlogo(ledLine,(mm/125)%8);
if (showmode ==5)
outverybig(ledLine);
if (showmode ==3){
outtianqi(ledLine,second/15);
}
if (showmode ==9)
outchar(ledLine,yinlihanzi); //显示阴历
if (showmode ==10){
if (second%2 ==1)
outtixing(ledLine,0); //定时测试
else
outzero();
}
if (showmode ==4){ //这个是主要模式,循环
int t = second/infotime;
switch (t){
case 0:
case 1:
case 2:
case 3:
outtianqi(ledLine,t);
break;
case 4:
case 5:
outtest(ledLine);
break;
case 6:
outchar(ledLine,yinlihanzi); //显示阴历
break;
default:
outverybig(ledLine);
}

}

outLabcd(ledLine);
if (webdelay ==0)
delayMicroseconds(readA0/10); //根据光线调整亮度
else
delayMicroseconds(webdelay);
digitalWrite(en,HIGH);




}
void char8to6(){
buff[0] = su[0]<<2|su[1]>>4;
buff[1] = su[1]<<4|su[2]>>2;
buff[2] = su[2]<<6|su[3];
buff[3] = su[4]<<2|su[5]>>4;
buff[4] = su[5]<<4|su[6]>>2;
buff[5] = su[6]<<6|su[7];
buff[6] = su[8]<<2|su[9]>>4;
buff[7] = su[9]<<4;

}
void outLabcd(byte Labcd){
if(Labcd>15)
Labcd = Labcd-16;
digitalWrite(ld,Labcd&8);
digitalWrite(lc,Labcd&4);
digitalWrite(lb,Labcd&2);
digitalWrite(la,Labcd&1);
digitalWrite(en,LOW);
}
void outshowled(int outx){
for (int i=0;i<16;i++)
showled = hour;
for (int i=24;i<32;i++)
showled = constrain(minute - 30,0,30);
for (int i=32;i<40;i++)
showled = constrain(minute,0,30);
for (int i=48;i<56;i++)
showled = constrain(second-30,0,30);
for (int i=56;i<64;i++)
showled = constrain(second,0,30);
//showled
for(int i = 0;i<8;i++){
buff = 0;
buff2 = 0;
}
for (int n = 0;n<64;n++){
if(showled[n] >= (32-outx))
buff[n/8] = buff[n/8] | 1 << 7-n%8;
if(showled[n] >= (16-outx))
buff2[n/8] = buff2[n/8] | 1 << 7-n%8;
}

digitalWrite(latchPin, LOW);
for(int n = 0;n<8;n++){
OUTDATA(dataPin,dataPin2, clockPin,buff[n],buff2[n],8);
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);

}
void outlogo(int outx,int t){
digitalWrite(latchPin, LOW);
for (int n =0 ;n<8;n++){

OUTDATA(dataPin,dataPin2, clockPin,logo[t][outx/2][outx%2*8+n],logo[t][outx/2+8][outx%2*8+n],8);

}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}
void outchar(int outx,int t){
digitalWrite(latchPin, LOW);
for (int n =0 ;n<4;n++){
for (int m=0;m<2;m++){
OUTDATA(dataPin,dataPin2, clockPin,hz[n+t][outx*2+m],hz[n+4+t][outx*2+m],8);
}
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}
void outverybig(int outx){ //verybigdigital
digitalWrite(latchPin, LOW);
for (int n =0 ;n<4;n++){
int te = n;
if (n>1)
te++;
for (int m=0;m<2;m++){

R1[n*2+m] = verybig[zm[te]*2][outx*2+m];
R2[n*2+m] = verybig[zm[te]*2+1][outx*2+m];
}
}
bigchuli();
if (mm>500)
tomaohao(outx);
for(int n = 0;n<8;n++){
OUTDATA(dataPin,dataPin2, clockPin,buff[n],buff2[n],8);
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}
void tomaohao(int x){ //增加冒号
if (x>7&&x<12){
buff[3] =buff[3]| 0x01;
buff[4] =buff[4]| 0x80;
}
if (x>3&&x<8){
buff2[3] =buff2[3]| 0x01;
buff2[4] =buff2[4]| 0x80;
}
}
void bigchuli(){ //大字符处理

}
void outtianqi(int outx,int t){
for (int n =0 ;n<10;n++){
if (outx >7 ){
su[n] = zimu[wendu[t*10+n]][outx-8];
}
}
char8to6();
if (outx<8){
for(int i=0;i<8;i++){
buff = zimu[44][outx];
}
}

byte newt = t*4;
digitalWrite(latchPin, LOW);
for (int n =0 ;n<4;n++){
for (int m=0;m<2;m++){
OUTDATA(dataPin,dataPin2, clockPin,hz[hztq[n+newt]][outx*2+m],buff[n*2+m],8);
}
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}
void outtixing(int outx,int t){
for(int i=0;i<8;i++){
if (dingshikey >i)
hztx = i;
else
hztx = 21; //空白
}
digitalWrite(latchPin, LOW);
for (int n =0 ;n<4;n++){
for (int m=0;m<2;m++){
OUTDATA(dataPin,dataPin2, clockPin,hz[hztx[n]][outx*2+m],hz[hztx[n+4]][outx*2+m],8);
}
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}

void outtest(int outx){
digitalWrite(latchPin, LOW);
for (int n =0 ;n<10;n++){
if (outx < 8){
su[n] = zimu[webdatebuff[n]][outx];
}else{
if (n ==2)
su[2] = smalldigital[zm[n+8]][outx-8];
else
su[n] = zimu[zm[n+8]][outx-8];
}
}

char8to6();
if(outx>7){
buff[6] = weekdata[week][outx*2-16];
buff[7] = weekdata[week][outx*2-15];
}
for (int n =0 ;n<8;n++){
OUTDATA(dataPin,dataPin2, clockPin,buff[n],newzi[zmnew[n]][outx],8);
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);


}
void outzimu(int outx){
digitalWrite(latchPin, LOW);
for (int n =0 ;n<8;n++){
if (outx > 7){
su[n] = zimu[zm[n]][outx-8];
}else{
if (n ==2)
su[2] = smalldigital[zm[n+8]][outx];
else
su[n] = zimu[zm[n+8]][outx];
}
}
char8to6();
for (int n =0 ;n<8;n++){
byte outH1 = zimu[datenow[n]][outx];
if (outx > 7){
outH1 = zimu[datenow[n+3]][outx-8];
//byte outL1 = zimu[zm[n]][outx-8];
// byte outL1 = buff[n];
}
if (n>1)
outH1 = hanzi[7][outx*2+(n-2)];
if (n>3)
outH1 = hanzi[8][outx*2+(n-4)];
if (n>5){
outH1 = hanzi[week][outx*2+(n-6)];
buff[n] = hanzi[week][outx*2+(n-6)];
}
if (outx > 7)
OUTDATA(dataPin,dataPin2, clockPin,outH1,buff[n],8);
else
OUTDATA(dataPin,dataPin2, clockPin,outH1,buff[n],8);
}/*
for (int n =0 ;n<8;n++){
byte outH1 = zimu[datenow[n]][outx];
if (outx > 7)
outH1 = zimu[datenow[n+3]][outx-8];
byte outL1 = zimu[zm[n]][outx-8];
if (n>1)
outH1 = hanzi[7][outx*2+(n-2)];
if (n>3)
outH1 = hanzi[8][outx*2+(n-4)];
if (n>5)
outH1 = hanzi[week][outx*2+(n-6)];
if (outx > 7)
OUTDATA(dataPin,dataPin2, clockPin,outH1,outL1);
else
OUTDATA(dataPin,dataPin2, clockPin,outH1,0);
}*/
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);
}
void outzero(){ //清空
digitalWrite(latchPin, LOW);
for (int m=0;m<16;m++){
OUTDATA(dataPin,dataPin2, clockPin,0,0,8);
}
digitalWrite(en,HIGH);
digitalWrite(latchPin, HIGH);

}
void OUTDATA(uint8_t x,uint8_t y, uint8_t clockp, uint8_t val, uint8_t val_2,uint8_t repchar){
uint8_t i;
for (i = 0; i < repchar; i++){
digitalWrite(x, !(val & ( 1<< (repchar-i-1) )));
digitalWrite(y, !(val_2 & (1<< (repchar-i-1) )));
digitalWrite(clockp, HIGH);
digitalWrite(clockp, LOW);
}
}

void tohttps(){
/*
这里服务器地址暂时隐藏了,提交环境数据,获取配置信息
主要是连接自建的web服务器,Linux PHP+mysql
配置信息是以逗号分隔的数字,代表特定含义。

*/
HTTPClient http;
Serial.println("[HTTP] begin...\n");
String links = "http://[mywebserver]?sn=";
links.concat(sn);
links.concat("&p=");
links.concat(Temp);
links.concat(",");
links.concat(Hum);
links.concat(",");
links.concat(httpkey);
links.concat(",");
links.concat(readA0);

http.begin(links); //HTTP httpkey
Serial.println(links);
int httpCode = http.GET();
Serial.println(httpCode);

if(httpCode > 0) {
if(httpCode == HTTP_CODE_OK) {
weberror = 0;
String payload = http.getString();
Serial.println(payload);
int j=0;
for(int i=0;i<15;i++)
{
webtime =payload-48;

}


int i = 16; j=0;int k =0;
hz[k][j] = 0;
while(payload != '!'){
if (payload == ',')
{j++;
if(j == 32){
j = 0;k++;
}
hz[k][j] = 0;
}
else
{hz[k][j] = hz[k][j]*10+(payload-48);}
i++;
//Serial.print(i);
}

i++;
j = 0;temp[j]=0;
while(payload != '!'){
if (payload == ',')
{j++;temp[j]=0;}
else
{temp[j] = temp[j]*10+(payload-48);}
i++;
}
refmax = temp[0]; //同步时间
showmode = temp[1]; //显示模式
webdelay = temp[2]; //亮度
infotime = constrain(temp[3],1,10); //信息显示秒数
dingshikey = temp[4]; //定时显示字数
tixingtime = temp[5]; //提醒显示时间
webbell = temp[6]; //喇叭开关

/*读取天气预报*/
i++;
j = 0;temp[j]=0;
while(payload != '!'){
if (payload == ',')
{j++;temp[j]=0;}
else
{temp[j] = temp[j]*10+(payload-48);}
i++;
}
tiantiyubao(); //天气预报数据转换
/*读取定时数据timelist*/
for(int x=0;x<64;x++){
timelist[x] = 0;
}
i++;
j = 0;
while(payload != '!'){
if (payload == ',')
{j++;}
else
{timelist[j] = timelist[j]*10+(payload-48);}
i++;
}


/*日期的显示*/
for (int i=0;i<4;i++){
webdatebuff = webtime;
}
webdatebuff[4] = 38; //横道
webdatebuff[5] = webtime[4];
webdatebuff[6] = webtime[5];
webdatebuff[7] = 38; //横道
webdatebuff[8] = webtime[6];
webdatebuff[9] = webtime[7];


hour = webtime[8]*10+webtime[9];
minute = webtime[10]*10+webtime[11];
second = webtime[12]*10+webtime[13];
datenow[0] = webtime[4];
datenow[1] = webtime[5];
datenow[3] = webtime[6];
datenow[4] = webtime[7];
week = webtime[14];
// datenow[6] = webtime[6];
// datenow[7] = webtime[7];
previousMillis = millis();
outtime = 0;
httpkey = 0;

}else{ //访问失败后调用
weberror = 1;
refmax = 30; //30秒以后重试


}
http.end();
}else{
weberror = 1;
refmax = 30; //30秒以后重试

}
}
void tiantiyubao(){
int j = 0;
for(int i=0;i<4;i++){
yubao = temp; //天气的汉字数量
if (yubao ==1){
hztq[i*4+1] = 21;
hztq[i*4+2] = j;
hztq[i*4+3] = 21;
}
if (yubao ==2){
hztq[i*4+1] = 21;
hztq[i*4+2] = j;
hztq[i*4+3] = j+1;
}
if (yubao ==3){
hztq[i*4+1] = j;
hztq[i*4+2] = j+1;
hztq[i*4+3] = j+2;
}
j = j+yubao;
yinlihanzi = j;
//byte hztq[12] = {16,19,0,1,17,19,2,3,18,19,4,5}; //三天天气汉字

}
for(int i=4;i<12;i++){
yubao = temp-40; //天气的温度数值
Serial.println(yubao);
}
for (int i=0;i<40;i++){
wendu = 37;
}
towendu(yubao[4],yubao[5],0);
towendu(yubao[6],yubao[7],1);
towendu(yubao[8],yubao[9],2);
towendu(yubao[10],yubao[11],3);
//byte wendu[30] ={37,38,4,39,37,42,37,1,1,39}; //温度显示字符
}
void towendu(int L,int H,int n){

int zz = 0;
wendu[n*10] = 37;
zz++;
if (L < 0){
wendu[n*10+zz] = 43;zz++;L = -L;
}
if(L > 9){
wendu[n*10+zz] = L/10;zz++;
}
wendu[n*10+zz] = L%10;zz++;
wendu[n*10+zz] = 39;zz++;
wendu[n*10+zz] = 37;zz++;
wendu[n*10+zz] = 42;zz++;
wendu[n*10+zz] = 37;zz++;
if (H < 0){
wendu[n*10+zz] = 43;zz++;H = -H;
}
if(H > 9){
wendu[n*10+zz] = H/10;zz++;
}
wendu[n*10+zz] = H%10;zz++;
wendu[n*10+zz] = 39;zz++;

}[/mw_shl_code]
 楼主| 发表于 2019-3-26 19:00 | 显示全部楼层
焊接教程来了



发表于 2019-3-27 09:58 | 显示全部楼层

必须点赞!!!
发表于 2019-6-19 15:23 | 显示全部楼层
请教esp8266和LED屏是用串口通信吗?
 楼主| 发表于 2019-6-26 21:40 | 显示全部楼层
gzlpd 发表于 2019-6-19 15:23
请教esp8266和LED屏是用串口通信吗?

不是串口,就是地址线abcd 颜色数据,有效线,时钟那些。595芯片接口
发表于 2019-10-18 10:06 | 显示全部楼层
你好  板子 程序卖吗   自己想做一个 玩玩  我有双色屏  可以支持吗
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 01:10 , Processed in 0.077993 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表