I have a relay that is operated by temperature or humidity reaches a certain value.
if (t > 32 || h > 70) {
digitalWrite(Relay1, LOW);
}
else if (t < 32 || h < 70) {
digitalWrite(Relay1, HIGH);
}
But i only want it to operate between a certain time, am i best to incorporate this into my void loop code above Or add/define a value between two times and then incorporate that into the void loop code?
kenwood120s:
Took 3 1/2 hours to get the whole story
No idea how the compiler will interpret this "&& now.hour 12:00:00-13:00:00".
It didn't like it,,, and sorry about that i do try to be a good noob but tooth ache has given my mind missed code,,, i have still ctrl t'd everything and put my code in code []'s thou ^^
kenwood120s:
I'd nest the temp and humidity if inside the time if like this, and treat the time to the same style of if as you have for the temp and humidity:
if (now.hour > 12 && now.hour <15) //or whatever the times need to be
I'm so stuck, been at this for hours. I've added a float as that's what i did with the temp/humidity. I've tried so many variations even the one you gave kenwood. Tried without 'else if p', tried without float, even gave an hour each side so i didn't get the hysteresis thing going on.
float p = now.hour();
if (p > 0 && p < 15)
{
if (t > 32 || h > 72)
{
digitalWrite(Relay1, LOW);
} else if (p < 23 && p > 16)
if (t < 28 || h < 68 )
digitalWrite(Relay1, HIGH);
}
}
Start with explaining clearly and explicitly what you're actually trying to do, like "from this time to that time, and when humidity and temperature is like that, switch on/off".
Get your formatting right (use ctrl-T in the IDE) and enclose every block in {} for extra visual help on what's going on. This code: Between 0:00 and 15:59 you set your relay LOW if t > 32 or h > 72, and within that time, if it's between 16:00 and 23:59 (which of course can't happen) and if t < 28 and h < 68 you switch the relay HIGH. So your relay may get switched LOW but can never get switched HIGH.
Using explicit variable names like "hour", "temperature" and "humidity" rather than "p", "h" and "t" also helps a lot to understand your own code.
kenwood120s:
Took 3 1/2 hours to get the whole story
No idea how the compiler will interpret this "&& now.hour 12:00:00-13:00:00".
I'd nest the temp and humidity if inside the time if like this, and treat the time to the same style of if as you have for the temp and humidity:
if (now.hour > 12 && now.hour <15) //or whatever the times need to be
{
if (t > 32 || h > 70)
{
//do relay stuff....
edit: changed || to && in code above
Kenwood i've had a bit of time to reflect on what you had responded too, and yes you are correct, i wasn't paying full attention to the error report. now.hour was missing the () either side. Correct code as follows, thank you very much.
if (now.hour() > 12 && now.hour() <19) //or whatever the times need to be