Turn off Fan after 10 minutes.

Hello all,

I have made a sketch which turns on a fan at a setted humidity value
and turns it off at a second setted value. Now i would like to keep it running for about
10 minutes if it gets under the setted value. I found something on google but i don't get it working in my sketch.

This is the sketch i'm using.

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define fan 4

int maxHum = 60;

DHT dht;

void setup() {
  pinMode(fan, OUTPUT);
  Serial.begin(9600); 
  dht.setup(2); // data pin 2
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.getHumidity();
  // Read temperature as Celsius
  float t = dht.getTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if(h > maxHum ) {
      digitalWrite(fan, HIGH);
  } if(h < maxHum -2) { 
     digitalWrite(fan, LOW);
  }
  
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");

}

Have a look at millis. It's like a clock. So when the threshold is reached, watch "the clock" an turn the fan off once 10 minutes passed.

I also looked at millis, but somehow i don't get it, i mean i don't see how i should
implement it in my sketch.

Save the millis() value at the time that the start action (turning on the fan) happens. Then, each time through loop(), check whether the required waiting period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly (turn off the fan) If not, then go round loop() again.

#include "DHT.h"
/* Let's do this C++ style
#define DHTPIN 2     // what pin we're connected to
#define fan 4
*/
const byte DthPin = 2;
const byte FanPin = 4;

const unsigned int MaxHum = 60;
const unsigned long FanRunTime = 10 * 60 * 1000UL;

unsigned long fanOnTime;
DHT dht;

void setup() {
  pinMode(FanPin, OUTPUT);
  Serial.begin(9600);
  dht.setup(DthPin); // data pin 2
}

void loop() {
  // Wait a few seconds between measurements.
  // NOTE: you probably want to do this wil millis as well ;)
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.getHumidity();
  // Read temperature as Celsius
  float temperature = dht.getTemperature();
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  if(humidity > MaxHum ) {
      digitalWrite(FanPin, HIGH);
      
      //Let's watch the clock to see what time it is now
      fanOnTime = millis();
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  
  //Now let's check if we need to turn off the fan
  //We only do that when the fan is running
  //And get the time difference by wathcing the clock again
  if(digitalRead(FanPin) && millis() - fanOnTime >= FanRunTime){
    digitalWrite(FanPin, LOW);
  }
}

I thought i got this sketch up and running. I tried it on a breadboard and it seemed to work. Bit now if i use the sketch and the fan is switched on the fan won't go off somehow it keeps hanging.

Bit now if i use the sketch and the fan is switched on the fan won't go off somehow it keeps hanging.

You need some of these, obviously: .........

What does "it keeps hanging" mean? Remember, only YOU can see what is actually happening.

if the humidity is greater than threshold, then turn the fan on if necessary and (unconditionally) make a note of the time

if the humidity is less than the threshold and the fan is not running, do nothing

If the humidity is less than the threshold AND the fan is running AND its been ten minutes since we last made note of the time, then turn the fan off.

You need to make a note of the time unconditionally, because if the humidity happens to peak again while the fan is in its 10-minute interval, you want to reset the 10-minute counter from that point.

to make a note of the time, put millis() in a [b]static[/b] variable.
To check if 10 minuts has elapsed, subtract the recorded time from millis() and check for >= 600000L

That sounds to be the solution,
But i have no idea to implement that in the sketch. Could someone add it to the existing sketch please?

Maybe, but then it's time to post what you have now :slight_smile:

I now use the sketch that septillion wrote for me.

And when the fan "hangs", what does Serial monitor tell you? Because I have no idea where this simple sketch can fail...

You can even expand the output a bit

#include "DHT.h"
/* Let's do this C++ style
#define DHTPIN 2     // what pin we're connected to
#define fan 4
*/
const byte DthPin = 2;
const byte FanPin = 4;

const unsigned int MaxHum = 60;
const unsigned long FanRunTime = 10 * 60 * 1000UL;

unsigned long fanOnTime;
DHT dht;

void setup() {
  pinMode(FanPin, OUTPUT);
  Serial.begin(9600);
  dht.setup(DthPin); // data pin 2
}

void loop() {
  // Wait a few seconds between measurements.
  // NOTE: you probably want to do this wil millis as well ;)
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.getHumidity();
  // Read temperature as Celsius
  float temperature = dht.getTemperature();
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  if(humidity > MaxHum ) {
      digitalWrite(FanPin, HIGH);
      
      //Let's watch the clock to see what time it is now
      fanOnTime = millis();
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\tFan: ");
  Serial.print((digitalRead(FanPin) ? "on" : "off"));
  Serial.print("\tfanRunTime: ");
  Serial.print(FanRunTime);
  Serial.print("\tMillis: ");
  Serial.print(millis());
  
  
  
  //Now let's check if we need to turn off the fan
  //We only do that when the fan is running
  //And get the time difference by wathcing the clock again
  if(digitalRead(FanPin) && millis() - fanOnTime >= FanRunTime){
    Serial.print(" Fan turned OFF!!");
    digitalWrite(FanPin, LOW);
  }
  Serial.println();
}

derksuh:
That sounds to be the solution,
But i have no idea to implement that in the sketch. Could someone add it to the existing sketch please?

I got a better idea: you do it.