0

I`m new here so just wanted to say hi!

I have been messing about with Arduino now for about 2 weeks, picking and trying different sketches for a greenhouse project I need.

Trying all sorts with LCD, temperature etc.

But what I actualy wanted was just a simple multiple timer say setting for every hour 9-6 activate for 2 mins to turn on a misting system for my plants, and that is now what I present in the following sketch, simple enough and it works, I have just removed some of the timings to keep the code compact on here.

Now the tricky bit, I want the misting system to come on in those hours chosen, but only if the Humidity is below a certain thresh hold, thats where I`m running into issues.

If anyone can look over the code and advise me where to put "if(h < humLowTrigger){"

I have tried before the times, included it in each line of the times and after the times and still not working.

So all help and knowledge greatly appreciated.

Cheers for now Alan

#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"

#define humLowTrigger 70
#define DHTPIN 7
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

RTC_DS1307 rtc;

void setup () {

Serial.begin(9600);
dht.begin();
Wire.begin();
rtc.begin();

pinMode( A2, OUTPUT);
digitalWrite(A2, LOW);
}

void loop () {

float h = dht.readHumidity();
float t = dht.readTemperature();

DateTime now = rtc.now();

if (now.hour() == 15 && now.minute() == 5) {
digitalWrite(A2, HIGH);
}
if (now.hour() == 15 && now.minute() == 8) {
digitalWrite(A2, LOW);
}
if (now.hour() == 15 && now.minute() == 10) {
digitalWrite(A2, HIGH);
}
if (now.hour() == 15 && now.minute() == 12) {
digitalWrite(A2, LOW);
}

Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %");
Serial.println();
Serial.println();
delay(2000);

}

2 Answers 2

1

If you want to do something when two or more conditions meets , use an and operator (&&), like this:

if (h < humLowTrigger && now.hour() == 15 && now.minute() == 5) {
... do something ...
}

If you have a lot of conditional statements, you can simplify:

if (h < humLowTrigger) {
   if (... other conditions 1 ...) {
     do something 1
     }
   ...
  if (... other condition n ...) {
     do something n.
     }
}

In your specific case:

if(h < humLowTrigger) {
  if (now.hour() == 15 && now.minute() == 5) {
    digitalWrite(A2, HIGH);
  }
  if (now.hour() == 15 && now.minute() == 8) {
    digitalWrite(A2, LOW);
  }
  if (now.hour() == 15 && now.minute() == 10) {
    digitalWrite(A2, HIGH);
  }
  if (now.hour() == 15 && now.minute() == 12) {
    digitalWrite(A2, LOW);
  }
}

Better version

But there is a problem with the previous code, beacuse the mister must turn off no matter what the humidity level is.

// Turn on only if humidity below threshold.
if (h < humLowTrigger && now.hour() == 15 && (now.minute() == 5 || now.minute() == 10)) {
    digitalWrite(A2, HIGH);
  }
}
// Turn off always.
if (now.hour() == 15 && (now.minute() == 8 || now.minute() == 12) {
    digitalWrite(A2, LOW);
   }
4
  • Yes, but if the humidity has gone up (which is likely), then you won't turn the mister off. :) Turning it off has to be unconditional.
    – Nick Gammon
    Commented Aug 30, 2017 at 8:43
  • @NickGammon. Standard disclaimer: I just answered the question "I want the misting system to come on in those hours chosen, but only if the Humidity is below a certain thresh hold". There are other issues with the code, but adding to the answer will only confuse the novice. Other issues like: he is comparing for the exact minute to turn on the mister ... what if he turn on the Arduino at 15:06? What if he spent the full 15:08 minute doing something else? Mister keeps running!
    – user31481
    Commented Aug 30, 2017 at 8:55
  • Hi guys thank you very much for the quick response, I did try out the modded code and it works as you knew it would ;) I also tried increasing the humidity whilst the output is high, and yes the output does not go low, that small issue is for another day when I have a better idea of what I`m doing ;)
    – Ozric
    Commented Aug 30, 2017 at 8:59
  • There are other issues with the code ... - ah yes, I was anticipating the next question. :)
    – Nick Gammon
    Commented Aug 30, 2017 at 11:47
0

In addition to LookAlterno's answer (which I upvoted), the last part can be reduced to:

if ((h < humLowTrigger) && (now.hour() == 15)) {
  if (now.minute() == 5) {
    digitalWrite(A2, HIGH);
  }
  if (now.minute() == 8) {
    digitalWrite(A2, LOW);
  }
  if (now.minute() == 10) {
    digitalWrite(A2, HIGH);
  }
  if (now.minute() == 12) {
    digitalWrite(A2, LOW);
  }
}
2
  • 1
    See my comment under Look Alterno's answer. You need to turn the mister off at the designated time, even if the humidity has risen.
    – Nick Gammon
    Commented Aug 30, 2017 at 8:45
  • Note this was changed on the code of LookAlterno before he updated it. Commented Aug 30, 2017 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.