Arduino uno 的全部代码 - #include <dht11.h>
- #define DHT11PIN A0
- dht11 DHT11;
- #include <DS1302.h>
- //接口定义
- //CE(DS1302 pin5) -> Arduino D5
- //IO(DS1302 pin6) -> Arduino D6
- //SCLK(DS1302 pin7) -> Arduino D7
- DS1302 rtc(5, 6, 7);
- Time otime;
- #include "LCD12864RSPI.h"
- //latchPin = 12;
- //clockPin = 10;
- //dataPin = 11;
- #define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
- // A1/15 确定按钮; A2/16 下移按钮; A3/17 上移按钮; A4/18 返回按钮;
- int clockledPin=13; //闹铃闪灯
- int clockaudioPin=3;//闹铃声音
- int bledPin=9; //背光灯
- // ⊙℃%:→▔年月日点分
- unsigned char str2[][2]={
- 0x20,0x20,0xA1,0xD1,0xA1,0xE6,0xA3,0xA5,0xA3,0xBA,0xA1,0xFA,0xA9,0xA5,0xC4,0xEA,0xD4,0xC2,0xC8,0xD5,0xB5,0xE3,0xB7,0xD6};
- // 周一周二周三周四周五周六周日设置日期时间闹铃
- unsigned char str4[][4]={
- 0x20,0x20,0x20,0x20,0xD6,0xDC,0xD2,0xBB,0xD6,0xDC,0xB6,0xFE,0xD6,0xDC,0xC8,0xFD,0xD6,0xDC,0xCB,0xC4,0xD6,0xDC,0xCE,0xE5,0xD6,0xDC,0xC1,0xF9,0xD6,0xDC,0xC8,0xD5,0xC9,0xE8,0xD6,0xC3,0xC8,0xD5,0xC6,0xDA,0xCA,0xB1,0xBC,0xE4,0xC4,0xD6,0xC1,0xE5};
- //关闭开启
- unsigned char cstr4[][4]={
- 0xB9,0xD8,0xB1,0xD5,0xBF,0xAA,0xC6,0xF4};
- int mode=1;//0 睡眠; 1 正常; 2 设置;
- int sx=0; //0 主菜单 1 日期 2 时间 3 闹铃
- int Isclock=1; //闹铃是否开启
- int cl_hr=6; //闹铃小时
- int cl_min=30; //闹铃分钟
- int light=10; //背光亮度
- int zr=0,zc=0;
- void setup()
- {
- Serial.begin(9600);
- rtc.write_protect(false);
- rtc.halt(false);
- LCDA.Initialise();
- delay(100);
- LCDA.CLEAR();
- for(int i=15;i<19;i++) {
- pinMode(i,INPUT);
- }
- pinMode(bledPin,OUTPUT);
- pinMode(clockaudioPin,OUTPUT);
- pinMode(clockledPin,OUTPUT);
- digitalWrite(bledPin,HIGH);
- digitalWrite(clockaudioPin,LOW);
- digitalWrite(clockledPin,LOW);
- delay(1000);
- }
- void loop()
- {
- switch(mode)
- {
- case 0:
- s_ls();
- break;
- case 2:
- s_set();
- break;
- default :
- s_nr();
- break;
- }
- }
- void s_set()
- {
- switch(sx)
- {
- case 1:
- s_m1();
- break;
- case 2:
- s_m2();
- break;
- case 3:
- s_m3();
- break;
- default :
- s_m0();
- break;
- }
- }
- void s_m0()
- {
- LCDA.CLEAR();
- delay(10);
- LCDA.DisplayString(0,0,(unsigned char *)(str4[8]),sizeof(str4[8]));
- LCDA.DisplayString(1,3,(unsigned char *)(str4[9]),sizeof(str4[9]));
- LCDA.DisplayString(2,3,(unsigned char *)(str4[10]),sizeof(str4[10]));
- LCDA.DisplayString(3,3,(unsigned char *)(str4[11]),sizeof(str4[11]));
- zr=1;
- zc=2;
- int btn=0;
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[5]),sizeof(str2[5]));
- while(1)
- {
- btn=c_btn();
- if(btn==1) {
- sx=zr;
- break;
- }
- else if(btn== 2){
- m_down();
- }
- else if(btn==3){
- m_up();
- }
- else if(btn==4){
- mode=1;
- break;
- }
- }
- }
- void s_m1()
- {
- LCDA.CLEAR();
- delay(10);
- LCDA.DisplayString(0,0,(unsigned char *)(str4[8]),sizeof(str4[8]));
- char tca1[17];
- otime = rtc.time();
- int yr=otime.yr;
- int mon=otime.mon;
- int date=otime.date;
- int day=otime.day;
- snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2[7], mon,str2[8],date,str4[day]);
- LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
- zr=2;
- zc=1;
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[6]),sizeof(str2[6]));
- int btn=0;
- while(1)
- {
- btn=c_btn();
- if(btn==1){
- otime = rtc.time();
- Time t(yr,mon,date,otime.hr,otime.min,otime.sec,day);
- rtc.time(t);
- m_right(2,1,7);
- }
- else if(btn== 2){
- switch(zc)
- {
- case 1:
- yr++;
- if(yr>2100)yr=2000;
- break;
- case 3:
- mon++;
- if(mon>12)mon=1;
- break;
- case 5:
- date++;
- if(date>31)date=1;
- break;
- case 7:
- day++;
- if(day>7)day=1;
- break;
- }
- snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2[7], mon,str2[8],date,str4[day]);
- LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
- }
- else if(btn==3){
- switch(zc)
- {
- case 1:
- yr--;
- if(yr<2000)yr=2100;
- break;
- case 3:
- mon--;
- if(mon<1)mon=12;
- break;
- case 5:
- date--;
- if(date<1)date=31;
- break;
- case 7:
- day--;
- if(day<1)day=7;
- break;
- }
- snprintf(tca1, sizeof(tca1), "%04d%.2s%02d%.2s%02d%.4s",yr,str2[7], mon,str2[8],date,str4[day]);
- LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
- }
- else if(btn==4){
- sx=0;
- break;
- }
- }
- }
- void s_m2()
- {
- LCDA.CLEAR();
- delay(10);
- LCDA.DisplayString(0,0,(unsigned char *)(str4[8]),sizeof(str4[8]));
- otime = rtc.time();
- int hr=otime.hr;
- int minu=otime.min;
- char tca2[9];
- snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",otime.hr,str2[10], otime.min,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- zr=2;
- zc=2;
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[6]),sizeof(str2[6]));
- int btn=0;
- while(1)
- {
- btn=c_btn();
- if(btn==1){
- otime = rtc.time();
- Time t(otime.yr,otime.mon,otime.date,hr,minu,0,otime.day);
- rtc.time(t);
- m_right(2,2,4);
- }
- else if(btn== 2){
- switch(zc)
- {
- case 2:
- hr++;
- if(hr>23)hr=0;
- break;
- case 4:
- minu++;
- if(minu>59)minu=0;
- break;
- }
- snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",hr,str2[10], minu,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- }
- else if(btn==3){
- switch(zc)
- {
- case 2:
- hr--;
- if(hr<0)hr=23;
- break;
- case 4:
- minu--;
- if(minu<0)minu=59;
- break;
- }
- snprintf(tca2, sizeof(tca2), "%02d%.2s%02d%.2s",hr,str2[10], minu,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- }
- else if(btn==4){
- sx=0;
- break;
- }
- }
- }
- void s_m3()
- {
- LCDA.CLEAR();
- delay(10);
- LCDA.DisplayString(0,0,(unsigned char *)(str4[8]),sizeof(str4[8]));
- char tca2[11];
- snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4[Isclock],cl_hr,str2[10],cl_min,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- zr=2;
- zc=2;
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[6]),sizeof(str2[6]));
- int btn=0;
- while(1)
- {
- btn=c_btn();
- if(btn==1){
- m_right(2,2,6);
- }
- else if(btn== 2){
- switch(zc)
- {
- case 2:
- Isclock++;
- if(Isclock>1)Isclock=0;
- break;
- case 4:
- cl_hr++;
- if(cl_hr>23)cl_hr=0;
- break;
- case 6:
- cl_min++;
- if(cl_min>59)cl_min=0;
- break;
- }
- snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4[Isclock],cl_hr,str2[10],cl_min,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- }
- else if(btn==3){
- switch(zc)
- {
- case 2:
- Isclock--;
- if(Isclock<0)Isclock=1;
- break;
- case 4:
- cl_hr--;
- if(cl_hr<0)cl_hr=23;
- break;
- case 6:
- cl_min--;
- if(cl_min<0)cl_min=59;
- break;
- }
- snprintf(tca2, sizeof(tca2), "%.4s%02d%.2s%02d%.2s",cstr4[Isclock],cl_hr,str2[10],cl_min,str2[11]);
- LCDA.DisplayString(1,2,(unsigned char *)tca2,sizeof(tca2));
- }
- else if(btn==4){
- sx=0;
- break;
- }
- }
- }
- void s_nr()
- {
- LCDA.CLEAR();
- delay(10);
- digitalWrite(bledPin,HIGH);
- int js=0,btn=0;
- while(1)
- {
- c_clock();
- btn=c_btn();
- if( btn==1){
- mode=2;
- break;
- }
- s_fdate();
- delay(100);
- js++;
- if(js>50)
- {
- mode=0;
- break;
- }
- }
- }
- void s_ls()
- {
- analogWrite(bledPin,light);
- int tr=0,tc=0,js=0;
- while(1)
- {
- if(c_clock()>0){
- mode=1;
- break;
- }
- if( c_btn()>0){
- mode=1;
- break;
- }
- if(js==20)
- {
- LCDA.CLEAR();
- delay(10);
- tr= (int)random(4);
- tc=(int)random(5);
- s_time(tr,tc);
- }
- else if(js==50)
- {
- s_wsd(tr,tc);
- js=0;
- }
- delay(100);
- js++;
- }
- }
- void m_up()
- {
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[0]),sizeof(str2[0]));
- zr--;
- if(zr<1){
- zr=3;
- }
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[5]),sizeof(str2[5]));
- delay(10);
- }
- void m_down(){
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[0]),sizeof(str2[0]));
- zr++;
- if(zr>3){
- zr=1;
- }
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[5]),sizeof(str2[5]));
- delay(10);
- }
- void m_right(int s,int sn,int en){
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[0]),sizeof(str2[0]));
- zc+=s;
- if(zc>en){
- zc=sn;
- }
- LCDA.DisplayString(zr,zc,(unsigned char *)(str2[6]),sizeof(str2[6]));
- delay(10);
- }
- int c_btn()
- {
- for(int i=15;i<19;i++) {
- if(digitalRead(i)==HIGH){
- delay(100);
- if(digitalRead(i)==HIGH)
- {
- return (i-14);
- }
- }
- }
- return 0;
- }
- int c_clock()
- {
- int r=0;
- if(Isclock==1)
- {
- if((cl_hr==otime.hr)&&(cl_min== otime.min))
- {
- r=1;
- digitalWrite(clockledPin,HIGH);
- tone(clockaudioPin,1209,100);
- tone(clockaudioPin,697,100);
- // tone(clockaudioPin,1336,100);
- // tone(clockaudioPin,697,100);
- delay(10);
- }
- else
- {
- digitalWrite(clockledPin,LOW);
- digitalWrite(clockaudioPin,LOW);
- }
- }
- return r;
- }
- void s_fdate()
- {
- LCDA.DisplayString(0,0,(unsigned char *)str2[Isclock],sizeof(str2[Isclock]));
- char tca1[18];
- otime = rtc.time();
- snprintf(tca1, sizeof(tca1), "%04d-%02d-%02d %s",otime.yr, otime.mon,otime.date,str4[otime.day]);
- LCDA.DisplayString(1,0,(unsigned char *)tca1,sizeof(tca1));
- char tca2[9];
- snprintf(tca2, sizeof(tca2), "%02d:%02d:%02d",otime.hr, otime.min,otime.sec);
- LCDA.DisplayString(2,2,(unsigned char *)tca2,sizeof(tca2));
- s_wsd(3,1);
- }
- void s_time(int r,int c)
- {
- char tca2[6];
- otime = rtc.time();
- snprintf(tca2, sizeof(tca2), "%02d:%02d",otime.hr, otime.min);
- LCDA.DisplayString(r,c,(unsigned char *)tca2,sizeof(tca2));
- }
- void s_wsd(int r,int c)
- {
- char tws[11];
- DHT11.read(DHT11PIN);
- snprintf(tws, sizeof(tws), "%2d%.2s %2d%.2s",(DHT11.temperature-2),str2[2],DHT11.humidity,str2[3]);
- LCDA.DisplayString(r,c,(unsigned char *)tws,sizeof(tws));
- }
复制代码
求优化! |