#include <Wire.h> // Arduino IDE 內建
#include <LiquidCrystal_I2C.h>
#include <DS1307RTC.h> // 引入 DS1307 RTC 库
#include "Timer.h"
#include "DHT.h"
#define DHTPIN 9
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
Timer T;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte myChars[8][8]={
{0x1C,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1C }, //R|
{0x7, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0x7 }, //L|
{0x1F,0x1F,0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, //T-
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1F,0x1F}, //D-
{0x1F,0x1F,0x0, 0x0, 0x0, 0x0, 0x1F,0x1F }, //B-
{0x07,0x07,0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, //TR-shot
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x07,0x07}, //DR_shot
{0x1c,0x1c,0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, //TL-shot
//{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1c,0x1c}, //dL_shot
};
byte numberspell[10][6]={
{0x01,0x02,0x00,0x01,0x03,0x00}, //0
{0xfe,0x01,0xfe,0xfe,0x01,0xfe}, //1
{0x05,0x04,0x00,0x01,0x03,0x03}, //2
{0x05,0x04,0x00,0x03,0x03,0x00}, //3
{0x01,0x03,0x00,0xfe,0xfe,0x00}, //4
{0x01,0x04,0x07,0x06,0x03,0x00}, //5
{0x01,0x04,0x07,0x01,0x03,0x00}, //6
{0x02,0x02,0x00,0xfe,0xfe,0x00}, //7
{0x01,0x04,0x00,0x01,0x03,0x00}, //8
{0x01,0x04,0x00,0x06,0x03,0x00} //9
};
byte mycharA[8];
int x,hourt,hourd,mint,mind;
void mychar()
{
for(int j=0;j<8;j++)
{
for(int i=0;i<8;i++)
{
mycharA[i]=myChars[j][i];
}
lcd.createChar(j,mycharA);
}
}
void displaynumber(int num, int address)
{
for(int a=0;a<6;a++)
{
lcd.setCursor(address+a%3,a/3);
lcd.write(numberspell[num][a]);
}
}
void setup()
{
t.update();
lcd.init();
lcd.backlight();
lcd.begin(16,2);
dht.begin(); //初始化DHT
Serial.begin(9600);
t1.every(5000,displaytemp);
t2.every(5000,displaytime);
mychar();
setSyncProvider(RTC.get); // 同步 RTC
if(timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
}
void loop()
{
tmElements_t tm; // 定义時間元素結構體
if(tm.Second%3==0)
{delay(100);
float h = dht.readHumidity(); //取得濕度
float t = dht.readTemperature(); //取得溫度C
}
if (RTC.read(tm)) { // 讀取時間
hourt=tm.Hour/10;
hourd=tm.Hour%10;
mint=tm.Minute/10;
mind=tm.Minute%10;
displaynumber(hourt,0);
displaynumber(hourd,4);
displaynumber(mint,9);
displaynumber(mind,13);
if(tm.Second%2==0)
{ lcd.setCursor(7,0);
lcd.write(0b00101110);
lcd.setCursor(7,1);
lcd.write(0b00101110);
}
else
{ lcd.setCursor(7,0);
lcd.write(0b11111110);
lcd.setCursor(7,1);
lcd.write(0b11111110);
}
}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("error RTC");
lcd.clear();
}
}