我正在使用 blynk 应用程序开发基于时间的应用程序。如果我的时间在设定的时间限制之间,继电器必须打开,否则继电器必须关闭。
我正在粘贴我的部分代码。如果我的时间设置在 1 小时范围内,则在下面的代码中它将起作用。如果时间大于 2 小时范围,它将不起作用。主要问题是比较时间小时和分钟。
void TimeZone1_Function()
{
if (TimeZone1 == 1)
{
// call with timer every 30 seconds or so
// Get RTC time
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
Serial.print("Current Time: ");
Serial.println(currentTime);
// Get Time Input Widget time
sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec);
Serial.print("Start Time: ");
Serial.println(startTime);
sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec);
Serial.print("Stop Time: ");
Serial.println(stopTime);
if (hour() >= SThour && hour() <= SPhour)
{
if (minute() >= STmin && minute() <= SPmin)
{
Serial.println("RELAY IS ON");
Serial.println("..........................");
digitalWrite(D5, HIGH); // Turn ON built-in LED
led22.on();
}
else
{
Serial.println("RELAY IS OFF"); Serial.println("..........................");
digitalWrite(D5, LOW); // Turn ON built-in LED
led22.off();
}
}
else
{
led22.off(); digitalWrite(D5, LOW);
Blynk.virtualWrite(V51, 0);
}
}
}
串行输出
Time Zone1 is selected
Current Time: 09:02:55
Start Time: 08:40:00
Stop Time: 10:40:00
RELAY IS OFF
…
AUTO mode is selected
Current Time: 09:03:06
Current Time: 09:03:07
Start Time: 09:03:00
Stop Time: 09:59:00
RELAY IS ON