Is this correct writing a if statement like that ? ( to do something between 8 and 12 hour with RTC library included)
DateTime now = RTC.now();
if( new.hour >= 8 && new.hour <= 11 ){
//something
}
Is this correct writing a if statement like that ? ( to do something between 8 and 12 hour with RTC library included)
DateTime now = RTC.now();
if( new.hour >= 8 && new.hour <= 11 ){
//something
}
It depends what "new" is. Did you mean "now"? If it is in loop(), it will be performed repeatedly, over and over until the time no longer fits the condition.
First, you defined a DateTime object named now, not new. Second, by "in between", do you mean it includes 8 and 12, or excluding those limits? Third, you're checking against 11, not 12 as you suggested.
If you read your RTC every 1 second, this will happen on the hour:
if( new.hour >= 8 && new.hour <= 11 && new.second == 0)
LarryD:
If you read your RTC every 1 second, this will happen on the hour:if( new.hour >= 8 && new.hour <= 11 && new.second == 0)
Um, no. Every minute.
Eagle eye ![]()
if( new.hour >= 8 && new.hour <= 11 && new.minute == 0 && new.second == 0)
aarg:
It depends what "new" is. Did you mean "now"? If it is in loop(), it will be performed repeatedly, over and over until the time no longer fits the condition.
Sorry. I wrote wrong!you were correct, it should be now.hour() instead. I intend keeping a solenoid signal high between 8am and 12h.
But I don´t know how to code if statements hour intervals when the limit hour is not round.
econjack:
First, you defined a DateTime object named now, not new. Second, by "in between", do you mean it includes 8 and 12, or excluding those limits? Third, you're checking against 11, not 12 as you suggested.
Econjack, I mean including these limits. Sorry for my english, but the idea is to let the user set the beginning time and then, the shut off time. Ex.: the user needs to feed a solenoid for 4 hours straight. He would set it from 8am to 12.
What did you think about the solution posted in reply #5?
aarg:
What did you think about the solution posted in reply #5?
LarryD:
Eagle eyeif( now.hour() >= 8 && now.hour() <= 11 && now.minute() == 0 && now.second() == 0)
I think the solution above only is tue when it is 8:00:0, 9:00:0, 10:00:0 and 11:00:0 due to operator order, isn´t it?
because
tue= true
I need to keep a solenoid pin high during certain time while keep doing other things
byte hourMarked;
//...
void loop()
{
DateTime now = RTC.now();
if (now.hour > hourMarked)
{
hourMarked = now.hour;
if (now.hour == 8)
{ // turn on relay }
if (now.hour == 12)
{ // turn off relay }
}
//...
}