Arduino時間顯示C語言,監視窗顯示,不用時鐘模組。買了時鐘模組RS1302、RS1307、RS3231卻都是一樣,不能顯示時間,GOOGLE一下找到了時間顯示,原本是顯示在LiquidCrystal模組,改顯示在監視窗顯示時間,測試成功,下次要弄一個4個LED數字顯示器。
#include <Wire.h>
//Arduino 時間顯示在視窗
void setup() {
Serial.begin(9600);
Serial.println("現在時間是:");
}
//thhis is a list of int variables used in this clock program
int s=0;
int sec=0;
int hrs=0;
int minutes=0;
int initialHours = 00;//設定開始的幾點
int initialMins = 00;//設定開始的幾點
int initialSecs = 0;//設定開始的幾秒
//this method is for seconds
int seconds()
{
s = initialHours*3600;
s = s+(initialMins*60);
s = s+initialSecs;
s = s+(millis()/1000);
return s;
}
//this method is for hours
int hours()
{
hrs = seconds();
hrs = hrs/3600;
hrs = hrs%24;
return hrs;
}
//this method is for minutes
int mins()
{
minutes = seconds();
minutes = minutes/60;
minutes = minutes%60;
return minutes;
}
int secs()
{
sec = seconds();
sec = sec%60;
return sec;
}
//開始執行時間顯示
void loop(){
printDigits(hours());
Serial.print("時");
sep();
printDigits(mins());
Serial.print("分");
sep();
printDigits(secs());
Serial.println("秒");
delay(1000);
}
//單獨一個字顯示十進位
void printDigits(byte digits){
if(digits < 10)
Serial.print(' ');
Serial.print(digits);
}
char sep()
{
s = millis()/1000;
if(s%2==0)
{
Serial.print(":");
}
else {
Serial.print(":");
}
}