if + while loop DHT + humidifier

hello,

I’m having a problem writing my code for a kind of box wherein I want to keep the air humidity above a certain level.

I’ve connected a DHT22 humidity sensor and a hacked humidifier to my arduino. If I just want to keep humidity above 70%, everything runs well. I used the following code:

if (h < 70.00)
** {**
** digitalWrite(humidifier, HIGH);**
** }**
else
** {**
** digitalWrite(humidifier, LOW);**
** }**

But now I want not just to keep the humidity-level above 70%, but I want, if humidity sinks below 70%, to keep the humidifier going until the humidity-level reaches 75%, and then start again when the level drops under 70%. I noticed otherwise the humidifier is constantly switching on and off.

To obtain this, I tried:

if (h < 70.00)
** {**
** while (h < 75.00)**
** {**
** digitalWrite(humidifier, HIGH);**
** }**
** }**

But this last piece of code doesn’t work. (The display keeps showing the initial humidity-level and the humidifier keeps activated.)

I dont’t understand why. Can somebody help me please?

Thanks a lot!

fagus

PS full code attached (verstuiverPin = humidifier)

slime_molds_16092015_v6.ino (1.58 KB)

You don’t need the while. Just lose the else in the first code and make it two if statements. If humidity < 70 turn on. If humidity > 75 turn off. If the humidity is between those values, neither if will be true and the humidifier will either stay on or stay off as needed.

Why do you have two things ? the ‘air’ and the ‘humidifier’. What does ‘air’ (luchtPin) do ?

In the loop() you have a delay of 2 seconds. Keep that, and don’t use a while, just keep it running every 2 seconds.

You need to know if the humidifier is on or off. That is all you need :slight_smile:
Then you can make it turn on or off at every value.

Let’s add a boolean variable called ‘humidifier’.
You have to set it to true if the humidifier is turned on, and false if the humidifier is turned off.

boolean humidifer = false;   // start with the humidifier being off

void setup()
{
  ...
}

void loop()
{
  ...

  if ( h < 70.00 && !humidifer )           // less than 70% and humidifer is off ?
  {
    digitalWrite ( verstuiverPin, HIGH);   // turn it on
    humidifier = true;                           // remember that the humidifer is on
  }

  if ( h > 75.00 && humidifier )            // more than 75% and humidifier is on ?
  {
    digitalWrite ( verstuiverPin, LOW );   // turn if off
    humidifier = false;                          // remember that the humidifier is off
  }
  ...
}

[ADDED] PaulS in Reply #3 is right, the variable ‘humidifier’ is not needed.

Or, turn the humidifier on when the humidity is less than 70% and off when it is above 75%. Simple, really.

if(humidity < 70)
{
   // turn the thing on
}

if(humidity > 75)
{
   // turn it off
}
    while (h < 75.00)
    {
      digitalWrite(humidifier, HIGH);
    }

That doesn’t work because you aren’t reading new values for h in the while loop.

I would use something like Paul suggested.

It works! Thanks to all! (Ps luchtPin controls a small fan to keep the humid air from condensing) fagus

Hello Im doing sorto of the same project but foud out that the humidifier only needs to go on for 3 seconds and rest 10 in order for the humidity to spread inside the container where im planning on regulating humidity and temperature.
I found that you had to do a debounde but only got to the point where i can put the humidifier on for 5 seconds and off for other 5.

Here is the code.
Im currently working in the variable withouth the DHT loaded as i figured i would advance better going from simple to complex.
Here is the code ive been using:

int inPin = 2; // the pin number for input (for me a push button)
int ledPin = 13;

int current; // Current state of the button
// (LOW is pressed b/c i’m using the pullup resistors)
int count; // How long the button was held (secs)
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed

void setup() {
Serial.begin(9600); // Use serial for debugging
pinMode(ledPin, OUTPUT);
digitalWrite(inPin, HIGH); // Turn on 20k pullup resistors to simplify switch input
}

void loop() {
current = digitalRead(inPin);
// if the button changes to pressed, remember the time
if (current == LOW && previous == HIGH && millis() - firstTime > 200) {
firstTime = millis();
}
// Notice that we check if the the button has been held for at least 500 ms
// This is to debounce the input (basically make sure we get the steady state of the button)
if (current == LOW && ((millis() - firstTime) % 1000) < 20 && millis() - firstTime > 50){
ledblink(1, 5000, ledPin); // Each second the button is held blink the indicator led and
count++; // and add 1 to the counter
}
// reset the counter if the button is not pressed
if (current == HIGH) {
count = 0;
}
previous = current;
}
// Just a simple helper function to blink an led in various patterns
void ledblink(int times, int lengthms, int pinnum){
for (int x=0; x<times;x++) {
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}