amir2
May 20, 2020, 2:15pm
1
hi iv been trying to get my relay on off to work on AM times and PM times
for my AM START_HOUR1=6 AM to STOP_HOUR1=8 AM this works no problem
if ((now.hour() >= START_HOUR1 && now.hour() <= STOP_HOUR1))
but i would like to do a PM also START_HOUR2= 6 PM to STOP_HOUR2= 8 PM
if ((((now.hour() >= START_HOUR1 && now.hour() <= STOP_HOUR1 or now.hour() >= START_HOUR2 && now.hour() <= STOP_HOUR2))))
the if statement above will start my AM on off no problem but my PM starts ON but WONT turn off STOP_HOUR2= 8 PM
please help
What value / type are all the START_HOUR and STOP_HOUR variables?
24 hour times are better for this.
Even better, use "minutes past midnight" or "seconds past midnight" for timing intervals.
amir2
May 20, 2020, 2:31pm
4
#define BAT_VOLT1 46.00 // RELAY1 LOW BAT VOLTAGE RELAY OFF
#define Max_Watts1 1500 //CT1/ solar / RELAY1// RELAY ON/OFF
#define START_HOUR1 6 // RELAY1 ON AM 0-23 hour 6=6AM
#define STOP_HOUR1 8 // RELAY1 OFF AM 0-23 hour 8=8AM
#define START_HOUR2 18 // RELAY1 ON PM 0-23 hour 18=6PM
#define STOP_HOUR2 20 // RELAY1 OFF PM 0-23 hour 20=8PM
float h2 = dht2.readHumidity();
// RELAYS
if (true)
{
// RELAY 1 AM/PM ON OFF PIN D12
if ((Watts1 < Max_Watts1 && Voltage8 > BAT_VOLT1 && h2 < 70))
{
if ((now.hour() >= START_HOUR1 && now.hour() <= STOP_HOUR1))
//if ((((now.hour() >= START_HOUR1 && now.hour() <= STOP_HOUR1 or now.hour() >= START_HOUR2 && now.hour() <= STOP_HOUR2))))
{
setStatusOfRelay(1, true);
}
else
{
setStatusOfRelay(1, false);
}
}
else
{
setStatusOfRelay(1, false);
}
Serial.printing the hour would be interesting. At least to validate that you're getting 24 hour times.
amir2
May 20, 2020, 3:15pm
6
Fri 23:59:15 4-10-2020
Fri 23:59:23 4-10-2020
Fri 23:59:31 4-10-2020
Fri 23:59:39 4-10-2020
Fri 23:59:47 4-10-2020
Fri 23:59:55 4-10-2020
Sat 0:0:3 4-11-2020
Sat 0:0:12 4-11-2020
Sat 0:0:20 4-11-2020
Sat 0:0:28 4-11-2020
Sat 0:0:36 4-11-2020
Sat 0:0:44 4-11-2020
Time to post all your code, in code tags please.
amir2
May 20, 2020, 3:20pm
8
should this if statement below work?
if ((((now.hour() >= START_HOUR1 && now.hour() <= STOP_HOUR1 or now.hour() >= START_HOUR2 && now.hour() <= STOP_HOUR2))))
hammy
May 20, 2020, 3:25pm
9
You forgot to post the code correctly with tags
I’d never write code like that , it’s very hard to read and prone to errors
Split up stuff over a few lines and add comments so when you look at it next year it still makes sense
. Not to sure about the “or” in there anyway , don’t think that exists as an instruction.
hammy:
. Not to sure about the “or” in there anyway , don’t think that exists as an instruction.
Yes, it does... along with "and", "xor", "not", and bitwise equivalents like "bitand", "bitor", etc.
https://en.cppreference.com/w/cpp/keyword
amir2
May 20, 2020, 3:52pm
11
i thought if you use a bad instruction like (or,and,&&,ect...) you would get a compile error and i dont
amir2:
i thought if you use a bad instruction like (or,and,&&,ect...) you would get a compile error and i dont
It's great that you used "or", it's more readable. You should also change every "&&" to "and", or else change every "or" to "||" to be consistent.
amir2
May 20, 2020, 5:19pm
13
ok i changed the (&& to and) change (or to ||) now it works but dont understand why or how
#define START_HOUR1 5 // RELAY1 ON AM 0-23 hour 6=6AM
#define STOP_HOUR1 6 // RELAY1 OFF AM 0-23 hour 8=8AM
#define START_HOUR2 7 // RELAY1 ON PM 0-23 hour 18=6PM
#define STOP_HOUR2 9 // RELAY1 OFF PM 0-23 hour 20=8PM
Relay 1 ON | Relay 2 OFF | Relay 3 OFF | Relay 4 OFF | Relay 5 OFF | Relay 6 OFF | Wed , 9:53:11 , 5/20/2020
Relay 1 OFF | Relay 2 OFF | Relay 3 OFF | Relay 4 OFF | Relay 5 OFF | Relay 6 OFF | Wed , 10:0:2
the AM start stop works right START_HOUR1 ,STOP_HOUR1 but the PM START_HOUR2 ,STOP_HOUR2 doesn't stop until a hour after but i can live with that il just change my STOP_HOUR2 to 1 hour earlier
if ((((now.hour() >= START_HOUR1 and now.hour() <= STOP_HOUR1 || now.hour() >= START_HOUR2 and now.hour() <= STOP_HOUR2))))
hammy
May 20, 2020, 10:08pm
14
Didn’t know that !! I’ve only ever used the instruction set as per the Arduino reference page which gives the confusing AND , OR squiggles.
amir2:
ok i changed the (&& to and) change (or to ||) now it works but dont understand why or how
You did not understand me. You should use "and, or" or else "&&, ||" but not mix them together in the same line.