if something || (or) something not working , also need a timer &&??

   if (t < 25 || h < 70) {
    digitalWrite(Relay1, HIGH);
  }
  else if (t > 25 || h > 70) {
    digitalWrite(Relay1, LOW);
  }

So when i do this it is only making Relay1 LOW if both values (t&h) are above > greater then, here is the rest of the code.

#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 < 25 || h < 70) {
    digitalWrite(Relay1, HIGH);
  }
  else if (t > 25 || h > 70) {
    digitalWrite(Relay1, LOW);
  }

}

I also need a 'in between' time, as in a timer, would this be a && in the same if sentence?

Kind regards

if t < 25 or h < 70, then the condition is true and the else is not executed

Juraj:
if t < 25 or h < 70, then the condition is true and the else is not executed

Sorry possible should have said my Relay is back to front, low is high and high is low.

ok i changed my if else statement around and it works

 if (t > 25 || h > 70) {
    digitalWrite(Relay1, LOW);
  }
  else if (t < 25 || h < 70) {
    digitalWrite(Relay1, HIGH);
  }

So, what are your intentions if t==25 AND h==70?

Trust me, || and && and if--else work as advertized.

gfvalvo:
So, what are your intentions if t==25 AND h==70?

Also, since this looks to me like some kind environmental control, you should think about introducing some hysteresis to prevent cycling too rapidly.

gfvalvo:
So, what are your intentions if t==25 AND h==70?

Intentions would be for both to write low, which they do, i have checked, i know this was going to be my next port of call, but it worked. Should it not have i ask the same.

MarkT:
Trust me, || and && and if--else work as advertized.

Indeed they do, but only once i had switched them over.

Regards

gfvalvo:
Also, since this looks to me like some kind environmental control, you should think about introducing some hysteresis to prevent cycling too rapidly.

Yes good point, could that not be introduced as a delay (5000)?

chefslot:
Intentions would be for both to write low, which they do, i have checked

Not with this code it won't:

if (t > 25 || h > 70) {
    digitalWrite(Relay1, LOW);
  }
  else if (t < 25 || h < 70) {
    digitalWrite(Relay1, HIGH);
  }

If t equals 25 and h equals 70, then neither branch will be taken. The relay output will remain unchanged from its present value.

chefslot:
Yes good point, could that not be introduced as a delay (5000)?

Poor coding technique at best.

Related to this thread

gfvalvo:
If t equals 25 and h equals 70, then neither branch will be taken. The relay output will remain unchanged from its present value.
Poor coding technique at best.

But it did, thou be it not both thresholds hit exactly at the same millisecond, but if one has met threshold then the following it still works. Like you say poor coding. Also i'm not asking you to do it for me, but more asking for a hint in the right direction, so if you could point me in the right direction that would be greatly appreciated, also with the delay.

kenwood120s:
Related to this thread

Yes very much so, the same project, i added in the OP wasn't to sure if it needed to go into a second thread or weather two items of interest was ok in one. Thank you kenwood i appreciate the help.

The way you wrote your conditional statements actually already implemented a hysteresis -- of sorts. I'd expand on that.

const uint8_t tempHysteresis = 1;
const uint8_t humHysteresis = 5;
.
.
.
.
.

  if (t > (25+tempHysteresis) || h > (70+humHysteresis)) {
    digitalWrite(Relay1, LOW);
  }
  else if (t < (25-tempHysteresis) || h < (70-humHysteresis)) {
    digitalWrite(Relay1, HIGH);
  }

Regarding cross posting in different threads -- that practice is definitely discouraged in these forums.

chefslot:
could [hysterisis] not be introduced as a delay (5000)?

Hysteresis isn't a time delay, it's using two different temperatures for on and off. So if the thing your're switching is a fan, you might switch it on at 25 degrees as it warms up and needs cooling. But if the off temperature is also 25, it will switch off immediately. So you only switch it off at say 23 on the way down.

So say it starts at 22. All good, but it's warming up. Passes 23, 24. Gets to 25, fan on. Might get to 26 before fan does any good. Starts cooling, 25, fan stays on, passes 24, still on. Gets to 23, fan off. Rinse and repeat.

gfvalvo:
The way you wrote your conditional statements actually already implemented a hysteresis -- of sorts. I'd expand on that.

kenwood120s:
So say it starts at 22. All good, but it's warming up. Passes 23, 24. Gets to 25, fan on. Might get to 26 before fan does any good. Starts cooling, 25, fan stays on, passes 24, still on. Gets to 23, fan off. Rinse and repeat.

So this is what you mean of sorts, i've given myself a 4# space gap, it might want to be less or more i'm not to sure yet. Will the code not get confused when between a #, say 30 as it doesn't have an action or a piece of code for this or will it just keep going until another action/criteria has been met, i presume the latter. But i'm now thinking what if the temp jumped from 24 to 30, am i right in thinking that it will not to anything until it then hits 32. Sorry just trying to cover all bases here and is it the case of lesser of two evils without writting lots and lots of code. The project is just for a vivarium, the numbers i'm using atm is just for easy testing.

if (t > 32 || h > 72 ) {
      digitalWrite(Relay1, LOW);
    }
    else if (t < 28 || h < 68 ) {
      digitalWrite(Relay1, HIGH);
    }

gfvalvo:
Regarding cross posting in different threads -- that practice is definitely discouraged in these forums.

Yes i take a slap on the wrists for that one, after i looked at then changing the title but there is no edit for this.

Regards

chefslot:
... Will the code not get confused when between a #, say 30 as it doesn't have an action or a piece of code for this or will it just keep going until another action/criteria has been met, i presume the latter. But i'm now thinking what if the temp jumped from 24 to 30, am i right in thinking that it will not to anything until it then hits 32.
...

No confusion at all. You are right in your assumption. If no criteria is met, relay status will stay as it was.

I do see a possible unexpected behavior.

If t is 25 and h is 73, t wants to activate relay and h want to deactivate relay. Your h>72 statement comes first and is true and relay deactivates. If else is skipped.

Now if h falls to 72, if statement is false and then if else is executed. There t<28 is true and relay activates.
If your next reading of h is 73, relay will immediately deactivate.

One solution to prevent frequent on/off is to use a counter and only activate/deactivate relay after x amount of subsequent readings.
Written in notepad, might contain spelling errors and other faults.

byte relayLowCounter=0;  //declare global, use int if more than ~4 minutes is needed.
byte relayHighCounter=0;

...
if (t > 32 || h > 72 ) {
      //digitalWrite(Relay1, LOW);
      relayLowCounter++;
      relayHighCounter=0;
    }
    else if (t < 28 || h < 68 ) {
      //digitalWrite(Relay1, HIGH);
      relayHighCounter++;
      relayLowCounter=0;
    }
if (relayLowCounter>60){                    // 60 for ~1 minute
      digitalWrite(Relay1, LOW);
    }
else if (relayHighCounter>60){
      digitalWrite(Relay1, HIGH);
    }

Gabriel_swe:
No confusion at all. You are right in your assumption. If no criteria is met, relay status will stay as it was.

I do see a possible unexpected behavior.

If t is 25 and h is 73, t wants to activate relay and h want to deactivate relay. Your h>72 statement comes first and is true and relay deactivates. If else is skipped.

Now if h falls to 72, if statement is false and then if else is executed. There t<28 is true and relay activates.
If your next reading of h is 73, relay will immediately deactivate.

One solution to prevent frequent on/off is to use a counter and only activate/deactivate relay after x amount of subsequent readings.
Written in notepad, might contain spelling errors and other faults.

byte relayLowCounter=0;  //declare global, use int if more than ~4 minutes is needed.

byte relayHighCounter=0;

...
if (t > 32 || h > 72 ) {
      //digitalWrite(Relay1, LOW);
      relayLowCounter++;
      relayHighCounter=0;
    }
    else if (t < 28 || h < 68 ) {
      //digitalWrite(Relay1, HIGH);
      relayHighCounter++;
      relayLowCounter=0;
    }
if (relayLowCounter>60){                    // 60 for ~1 minute
      digitalWrite(Relay1, LOW);
    }
else if (relayHighCounter>60){
      digitalWrite(Relay1, HIGH);
    }

Hi, thank you, this was brought up in a previous post, this is why i gave a little gap between them. I will look more into it when my headache has gone, but my low is really high and my high is really low on the relay that i have, thou i think in what your saying it wont matter which way around which is, is that correct? Regards

Gabriel_swe:
I do see a possible unexpected behavior.

If t is 25 and h is 73, t wants to activate relay and h want to deactivate relay. Your h>72 statement comes first and is true and relay deactivates. If else is skipped.

Now if h falls to 72, if statement is false and then if else is executed. There t<28 is true and relay activates.
If your next reading of h is 73, relay will immediately deactivate.

One solution to prevent frequent on/off is to use a counter and only activate/deactivate relay after x amount of subsequent readings.

Well it worked great, now do i still need a gap in the values of t & h, where it was

if (t > 32 || h > 72 ) {
      //digitalWrite(Relay1, LOW);
      relayLowCounter++;
      relayHighCounter=0;
    }
    else if (t < 28 || h < 68 ) {
      //digitalWrite(Relay1, HIGH);
      relayHighCounter++;
      relayLowCounter=0;
    }
if (relayLowCounter>60){                    // 60 for ~1 minute
      digitalWrite(Relay1, LOW);
    }
else if (relayHighCounter>60){
      digitalWrite(Relay1, HIGH);
    }

could these values be the same like this now?

if (t > 30 || h > 70 ) {
      //digitalWrite(Relay1, LOW);
      relayLowCounter++;
      relayHighCounter=0;
    }
    else if (t < 30 || h < 70 ) {
      //digitalWrite(Relay1, HIGH);
      relayHighCounter++;
      relayLowCounter=0;
    }
if (relayLowCounter>60){                    // 60 for ~1 minute
      digitalWrite(Relay1, LOW);
    }
else if (relayHighCounter>60){
      digitalWrite(Relay1, HIGH);
    }

so i've changed it too <30 or >30 or am i best still leaving it like <28 or > 32?

One more thing, if i was to add another relay and used the same

byte relayLowCounter=0;

am i right in thinking the '60' stated for length of time will also have to be the same for if i was to add a Relay2, and if i wanted it different would i need to do this

byte relayLowCounter1=0;

Note i added a 1 at the end of byte relayLowCounter?

Regards

I assume you are controlling a fan and h is humidity and t is temperature.
If you should have a gap between your values or not is something you will find out with trial and error. I would expect a small air volume and powerful fan need a smaller gap and maybe shorter time.

For another relay I think a second counter is needed.

Gabriel_swe:
I assume you are controlling a fan and h is humidity and t is temperature.
If you should have a gap between your values or not is something you will find out with trial and error. I would expect a small air volume and powerful fan need a smaller gap and maybe shorter time.

For another relay I think a second counter is needed.

Yes a fan for a vivarium, another relay for a fogger. The volume is quite small in relation to volume of air moved. So have reduced counter seconds down to 15 , using a small pc fan 60mm might even go smaller but will possible prefer to run at a lower speed. Trail and error is going to be everything before I put anything in there. Might also need a heater but if I can find a happy medium with a heater then control temp by venting I should get good air circulation and safe having a heater on the sketch too. But then if the fogger will be on to increase humidity.... Going to have to play with room temps too and how this effects with other perimeters. Many thanks all that has helped this noobie.