Adding a action between two times, but relying on other variables.

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?

something like

  if (t > 32 || h > 70) {
    if (millis() - lastOn > waitTime) {
      digitalWrite(Relay1, LOW);
    }
  }
  else if (t < 32 || h < 70) {
    digitalWrite(Relay1, HIGH);
    lastOn = millis();
  }

?

I'm not sure what you mean by "between two times": do you mean times of day, like only operate between 0800 and 1700 or what?

If you want time of day, you'll have to add a real time clock (or if you're connected to the Internet, use NTP).

kenwood120s:
I’m not sure what you mean by “between two times”: do you mean times of day, like only operate between 0800 and 1700 or what?

Yes correct i feel this would be easier to adjust then millisec

wvmarle:
If you want time of day, you’ll have to add a real time clock (or if you’re connected to the Internet, use NTP).

Indeed rest of sketch, at the bottom is my attempted fail. Regards

#include "DHT.h"                                        // Libary for the DHT22
#include "Relay.h"                                      // Libary for the Relay
#define DHTPIN 2                                        // what digital pin DHT22 is connected to
#define DHTTYPE DHT22                                   // DHT type = 22
DHT dht(DHTPIN, DHTTYPE);                               // Init DHT sensor.
#define Relay1 7                                        // Define relay 1 to pin#

#include <Wire.h>                                       //DS3231
#include "RTClib.h"                                     //DS3231
RTC_DS3231 rtc;                                         //DS3231
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //DS3231


void setup()
{
  Serial.begin(9600);                                   // Setup Serial connection speed (was 115200)
  dht.begin();                                          // Init DHT22
  digitalWrite(Relay1, HIGH);                           // set the pin HIGH first, stops on/off at boot
  pinMode(Relay1, OUTPUT);                              // then make it an output

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");// following line sets the RTC to the date & time this sketch was compiled
  }

}

void loop()
{
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  float h = dht.readHumidity();                         // DHT22 deciaml point
  float t = dht.readTemperature();                      // DHT22 deciaml point
  Serial.print("Humidity: ");                           // DHT22
  Serial.print(h);                                      // DHT22
  Serial.print(" %\t");                                 // DHT22
  Serial.print("Temperature: ");                        // DHT22
  Serial.print(t);                                      // DHT22
  Serial.println(" *C ");                               // DHT22

  delay (1000);                                           // Wait one second before repeating

  
 if (t > 32 || h > 70 && now.hour 12:00:00-13:00:00 ) {
    digitalWrite(Relay1, LOW);
  }
  else if (t < 32 || h < 70 && now.hour 13:00:01-11:59:59) {
    digitalWrite(Relay1, HIGH);
  }
 

}

Took 3 1/2 hours to get the whole story :wink:

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

Related to this thread.

kenwood120s:
Took 3 1/2 hours to get the whole story :wink:

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

{
   if (t > 32 || h > 70)
     {
         //do relay stuff…

Thank you will try this out.

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 :wink:

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