//LCD1602倒计时
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2); //构造一个LiquidCrystal的类成员。使用数字IO ,12,11,5,4,3,2
unsigned long time;
unsigned long starttime;
int timeHour; //显示小时数
int timeMinute; //显示分钟数
int timeSecond; //显示秒数
int startHour=0; //初始小时数
int startMinute=1; //初始分钟数
int startSecond=40; //初始秒数
void setup()
{
Serial.begin(9600);
lcd.begin(16,2); //初始化LCD1602
lcd.print("Welcome to use!"); //液晶显示Welcome to use!
delay(1000); //延时1000ms
lcd.clear(); //液晶清屏
starttime=startHour*60*60+startMinute*60+startSecond;
lcd.setCursor(0,0); //设置液晶开始显示的指针位置
lcd.print("While accounting");
}
void loop(){
if (millis()<=starttime*1000){
time =starttime-(millis()/1000);
timeHour=time/(60*60);
timeMinute=(time-timeHour*60*60)/60;
timeSecond=time-timeHour*60*60-timeMinute*60;
lcd.setCursor(0,1); //设置液晶开始显示的指针位置
lcd.print("Time:");
lcd.print(Pressing_string(timeHour));
lcd.print(":");
lcd.print(Pressing_string(timeMinute));
lcd.print(":");
lcd.print(Pressing_string(timeSecond));
//delay(10);
}else{
lcd.setCursor(0,1); //设置液晶开始显示的指针位置
lcd.print("It's game over.");
}
}
//n->"0n",nn->"nn"
String Pressing_string(int myint){
String mystring;
if (myint<10 ){
mystring="0"+String(myint);
}else{
mystring=String(myint);
}
return mystring;
}
|