我的 NTP时钟项目将在显示时间时做不同的事情,其中一项任务是在特定时间运行代码。我的完整代码在底部。我显示的时间格式如下“
tiME:00:00:00”,所以我尝试使用:
代码:
全选if (Time == \"TIME:19:52:00\") {
Serial.println(\"Timer Event Activated !!\"); //No output
}
此代码失败,没有串行输出。这是变量类型不匹配。我将时间数组转换为字符串,然后进行时间比较。
代码:
全选void loop() {
timeClient.update();
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
Time[12] = second_ % 10 + 48;
Time[11] = second_ / 10 + 48;
Time[9] = minute_ % 10 + 48;
Time[8] = minute_ / 10 + 48;
Time[6] = hour_ % 10 + 48;
Time[5] = hour_ / 10 + 48;
Date[5] = month_ / 10 + 48;
Date[6] = month_ % 10 + 48;
Date[8] = day_ / 10 + 48;
Date[9] = day_ % 10 + 48;
Date[13] = (year_ / 10) % 10 + 48;
Date[14] = year_ % 10 % 10 + 48;
// Send time and date to serial monitor
Serial.println(Time);
Serial.println(Date);
//----------------------- Timed Event ---------------------------------
String strTime = String(Time);
if (strTime == \"TIME:21:19:00\") {
Serial.println(\"Timer Event Activated !!\");
}
// Display time and date on the 16x2 LCD
lcd.setCursor(0, 0);
lcd.print(Time);
lcd.setCursor(0, 1);
lcd.print(Date);
last_second = second_;
}
delay(200);
}