#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// 设置 I2C 地址为 0x27,LCD 显示屏大小为 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
int s = 0;
int m = 0;
int h = 0;
int d = 1;
int month = 1;
int year = 2024;
const int s_sw = 12;
const int m_sw = 11;
const int h_sw = 10;
const int d_sw = 9;
const int month_sw = 8;
const int year_sw = 7;
const int reset_sw = 6;
int s_in;
int m_in;
int h_in;
int d_in;
int month_in;
int year_in;
int reset_in;
unsigned long previousMillis = 0; // 记录上次事件发生的时间
const long interval = 1000; // 定义事件间隔为 1000 毫秒(1 秒)
void setup()
{
//pin mode set
pinMode(s_sw, INPUT_PULLUP);
pinMode(m_sw, INPUT_PULLUP);
pinMode(h_sw, INPUT_PULLUP);
pinMode(d_sw, INPUT_PULLUP);
pinMode(month_sw, INPUT_PULLUP);
pinMode(year_sw, INPUT_PULLUP);
pinMode(reset_sw, INPUT_PULLUP);
// 初始化 LCD
lcd.init();
// 打开背光
lcd.backlight();
logo();
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
s = s + 1;
timecount();
distime();
}
//digital read
s_in = digitalRead(s_sw);
m_in = digitalRead(m_sw);
h_in = digitalRead(h_sw);
d_in = digitalRead(d_sw);
month_in = digitalRead(month_sw);
year_in = digitalRead(year_sw);
reset_in = digitalRead(reset_sw);
button();
date();
}
void logo()
{
// 在 LCD 上显示 "Hello, World!"
lcd.setCursor(2,0); // 设置光标到第一行第一个位置
lcd.print("time keeper");
delay(500);
lcd.setCursor(4,1);
lcd.print("by ACK");
delay(1000);
lcd.clear();
lcd.setCursor(4,1);
lcd.print(":");
lcd.setCursor(11,1);
lcd.print(":");
lcd.setCursor(4,0);
lcd.print("/");
lcd.setCursor(7,0);
lcd.print("/");
}
void distime()
{
lcd.setCursor(4,1);
lcd.print(":");
lcd.setCursor(11,1);
lcd.print(":");
lcd.setCursor(4,0);
lcd.print("/");
lcd.setCursor(7,0);
lcd.print("/");
if(s < 10)
{
lcd.setCursor(13,1);
lcd.print(" ");
lcd.setCursor(14,1);
lcd.print(s);
}
if(s >= 10)
{
lcd.setCursor(13,1);
lcd.print(s);
}
if(m < 10)
{
lcd.setCursor(7,1);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print(m);
}
if(m >= 10)
{
lcd.setCursor(7,1);
lcd.print(m);
}
if(h < 10)
{
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(h);
}
if(h >= 10)
{
lcd.setCursor(1,1);
lcd.print(h);
}
lcd.setCursor(10,0);
lcd.print(" ");
if(d < 10)
{
lcd.setCursor(8,0);
lcd.print("0");
lcd.setCursor(9,0);
lcd.print(d);
}
if(d >= 10)
{
lcd.setCursor(8,0);
lcd.print(d);
}
if(month < 10)
{
lcd.setCursor(5,0);
lcd.print("0");
lcd.setCursor(6,0);
lcd.print(month);
}
if(month >= 10)
{
lcd.setCursor(5,0);
lcd.print(month);
}
lcd.setCursor(0,0);
lcd.print(year);
date_dis_clear();
}
void timecount()
{
if(s > 59)
{
s = 0;
m = m + 1;
}
if(m > 59)
{
m = 0;
h = h + 1;
}
if(h > 23)
{
h = 0;
d = d + 1;
}
if(month == 1 and d > 31)
{
d = 1;
month = month + 1;
}
if(year % 4 == 0 and month == 2 and d > 29)
{
d = 1;
month = month + 1;
}
if(year % 4 != 0 and month == 2 and d > 28)
{
d = 1;
month = month + 1;
}
if(month == 3 and d > 31)
{
d = 1;
month = month + 1;
}
if(month == 4 and d > 30)
{
d = 1;
month = month + 1;
}
if(month == 5 and d > 31)
{
d = 1;
month = month + 1;
}
if(month == 6 and d > 30)
{
d = 1;
month = month + 1;
}
if(month == 7 and d > 31)
{
d = 1;
month = month + 1;
}
if(month == 8 and d > 31)
{
d = 1;
month = month + 1;
}
if(month == 9 and d > 30)
{
d = 1;
month = month + 1;
}
if(month == 10 and d > 31)
{
d = 1;
month = month + 1;
}
if(month == 11 and d > 30)
{
d = 1;
month = month + 1;
}
if(month == 12 and d > 31)
{
d = 1;
month = 1;
year = year + 1;
}
if(month > 12)
{
month = 1;
}
if(year > 9999)
{
year = 2000;
}
}
void button()
{
if(s_in == LOW)
{
delay(50);
while(digitalRead(s_sw) == LOW);
s = s + 1;
delay(50);
}
if(m_in == LOW)
{
delay(50);
while(digitalRead(m_sw) == LOW);
m = m + 1;
delay(50);
}
if(h_in == LOW)
{
delay(50);
while(digitalRead(h_sw) == LOW);
h = h + 1;
delay(50);
}
if(d_in == LOW)
{
delay(50);
while(digitalRead(d_sw) == LOW);
d = d + 1;
delay(50);
}
if(month_in == LOW)
{
delay(50);
while(digitalRead(month_sw) == LOW);
month = month + 1;
delay(50);
}
if(year_in == LOW)
{
delay(50);
while(digitalRead(year_sw) == LOW);
year = year + 1;
delay(50);
}
if(reset_in == LOW)
{
delay(50);
while(digitalRead(reset_sw) == LOW);
lcd.clear();
s = 0;
m = 0;
h = 0;
d = 1;
month = 1;
year = 2024;
logo();
}
}
void date()
{
if(month == 1 and d == 1 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Happy New Year");
}
if(month == 2 and d == 28 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Peace Day");
}
if(month == 3 and d == 8 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Women's Day");
}
if(month == 3 and d == 12 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Sun Yat-sen Day");
}
if(month == 3 and d == 14 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("AntiAggression D");
}
if(month == 4 and d == 4 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Children Day");
}
if(month == 5 and d == 1 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Labor Day");
}
if(month == 9 and d == 3 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Military Day");
}
if(month == 9 and d == 28 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Teacher's Day");
}
if(month == 10 and d == 10 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("National Day");
}
if(month == 10 and d == 24 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("United Nations D");
}
if(month == 10 and d == 25 and s == 30)
{
lcd.setCursor(0,0);
lcd.print("Liberation Day");
}
if(month == 1 and d == 1 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Happy New Year");
}
if(month == 2 and d == 28 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Peace Day");
}
if(month == 3 and d == 8 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Women's Day");
}
if(month == 3 and d == 12 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Sun Yat-sen Day");
}
if(month == 3 and d == 14 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("AntiAggression D");
}
if(month == 4 and d == 4 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Children Day");
}
if(month == 5 and d == 1 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Labor Day");
}
if(month == 9 and d == 3 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Military Day");
}
if(month == 9 and d == 28 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Teacher's Day");
}
if(month == 10 and d == 10 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("National Day");
}
if(month == 10 and d == 24 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("United Nations D");
}
if(month == 10 and d == 25 and s == 31)
{
lcd.setCursor(0,0);
lcd.print("Liberation Day");
}
if(month == 1 and d == 1 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Happy New Year");
}
if(month == 2 and d == 28 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Peace Day");
}
if(month == 3 and d == 8 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Women's Day");
}
if(month == 3 and d == 12 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Sun Yat-sen Day");
}
if(month == 3 and d == 14 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("AntiAggression D");
}
if(month == 4 and d == 4 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Children Day");
}
if(month == 5 and d == 1 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Labor Day");
}
if(month == 9 and d == 3 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Military Day");
}
if(month == 9 and d == 28 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Teacher's Day");
}
if(month == 10 and d == 10 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("National Day");
}
if(month == 10 and d == 24 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("United Nations D");
}
if(month == 10 and d == 25 and s == 32)
{
lcd.setCursor(0,0);
lcd.print("Liberation Day");
}
if(month == 1 and d == 1 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Founding Day");
}
if(month == 2 and d == 28 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Peace Day");
}
if(month == 3 and d == 8 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Women's Day");
}
if(month == 3 and d == 12 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Sun Yat-sen Day");
}
if(month == 3 and d == 14 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("AntiAggression D");
}
if(month == 4 and d == 4 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Children Day");
}
if(month == 5 and d == 1 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Labor Day");
}
if(month == 9 and d == 3 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Military Day");
}
if(month == 9 and d == 28 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Teacher's Day");
}
if(month == 10 and d == 10 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("National Day");
}
if(month == 10 and d == 24 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("United Nations D");
}
if(month == 10 and d == 25 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Liberation Day");
}
if(month == 1 and d == 1 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Happy New Year");
}
if(month == 2 and d == 28 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Peace Day");
}
if(month == 3 and d == 8 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Women's Day");
}
if(month == 3 and d == 12 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Sun Yat-sen Day");
}
if(month == 3 and d == 14 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("AntiAggression D");
}
if(month == 4 and d == 4 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Children Day");
}
if(month == 5 and d == 1 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Labor Day");
}
if(month == 9 and d == 3 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Military Day");
}
if(month == 9 and d == 28 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Teacher's Day");
}
if(month == 10 and d == 10 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("National Day");
}
if(month == 10 and d == 24 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("United Nations D");
}
if(month == 10 and d == 25 and s == 33)
{
lcd.setCursor(0,0);
lcd.print("Liberation Day");
}
}
void date_dis_clear()
{
if(s == 30 or s==31 or s==32 or s==33)
{
if(month == 1 and d == 1)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 2 and d == 28)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 3 and d == 8)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 3 and d == 12)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 3 and d == 14)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 4 and d == 4)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 5 and d == 1)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 9 and d == 3)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 9 and d == 28)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 10 and d == 10)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 10 and d == 24)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
if(month == 10 and d == 25)
{
lcd.setCursor(0,0);
lcd.print(" ");
}
}
}