#include <LiquidCrystal.h> //申明1602液晶的函数库
#define LCD_REFRESH_TIME 1000 // 每隔多久刷新一次屏幕(1000=1S)
//申明1602液晶的引脚所连接的Arduino数字端口,8线或4线数据模式,任选其一
//LiquidCrystal lcd(12,11,10,9,8,7,6,5,4,3,2); //8数据口模式连线声明
LiquidCrystal lcd(12,11,5,4,3,2); //4数据口模式连线声明
char arrey[] = "HuangJingYu chengxing232 qingdao 202310102045";
int Text_Number = 0; // 每2s增加一下数据
unsigned long LCD_Clear_Time = 0; // 记录上一次刷新时间
void setup()
{
Serial.begin(9600); //波特率初始化为9600 //定义9号引脚为输出模式
lcd.begin(16,2); //初始化1602液晶工作 //定义1602液晶显示范围为2行16列字符
lcd.home(); //把光标移回左上角,即从头开始输出
lcd.setCursor(0,1); //把光标定位在第1行,第0列
lcd.clear();
LCD_Clear_Time = millis(); // 上电记录一下时间
}
int second, minute,hour;
void loop() {
if (millis() - LCD_Clear_Time > LCD_REFRESH_TIME) { // 固定时间刷新,避免快速闪屏
LCD_Clear_Time = millis(); // 重新记录上一次清屏时间
Text_Number++;
String msg = formatTime(Text_Number);
Serial.println(msg);
lcd.clear(); // 清除屏幕所有内容,并且将游标回到原点
lcd.setCursor(0, 0);
lcd.print(msg);
}
}
String formatTime(int totalSeconds) {
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;
int seconds = totalSeconds % 60;
String formattedHours = (hours < 10) ? "0" + String(hours) : String(hours);
String formattedMinutes = (minutes < 10) ? "0" + String(minutes) : String(minutes);
String formattedSeconds = (seconds < 10) ? "0" + String(seconds) : String(seconds);
return formattedHours + ":" + formattedMinutes + ":" + formattedSeconds;
}