oleost:
( The crossing of midnight make it harder for me to use if timeOn is less time && time is more than timeOff. )
…
Any pointers ?
Seconds since midnight is the way to go.
The solution to this is the same as the thing we do when counting milliseconds, to bullet-proof against rollover. Subtract the start time from the current time, do a modulus, then compare to the length of the interval.
The tricky thing is that with millis(), the modulus operator is automatically taken care of because it's all 32 bit. In this case, it needs to be done mod 86400. The other tricky thing is that the C++ '%' operator is not a modulus operator, it is a remainder operator and will do the wrong thing with negative numbrs. To fix this, you force the number to a positive value by adding the number of seconds in a day and then taking the remainder. This works correctly whether the start is behind or in front of the end.
so:
const long SECS_PER_DAY = 24L*60L*60L;
const long START1 = 15L * 60L * 60L; // 3PM
const long END1 = 18L * 60L * 60L; // 6PM
const long INTERVAL1 = (END1 - START1 + SECS_PER_DAY) % SECS_PER_DAY; // 3 hours.
const long START2 = 15L * 60L * 60L; // 3PM
const long END2 = 7L * 60L * 60L; // 7AM
const long INTERVAL2 = (END2 - START2 + SECS_PER_DAY) % SECS_PER_DAY; // 16 hours.
…
long current_time = /* get the time of day in seconds from the clock */ ;
booelan is_in_interval_1 = ((current_time - START1 + SECS_PER_DAY) % SECS_PER_DAY) < INTERVAL1;
booelan is_in_interval_2 = ((current_time - START2 + SECS_PER_DAY) % SECS_PER_DAY) < INTERVAL2;
…