Hi. I'm fairly new to arduino and i'm trying to create simple humidity monitor that turns on/off relay
problem is dht sensor in do while loop displays same humidity all the time!
any help?
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN 8
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht = DHT(DHTPIN, DHTTYPE);
const int RELAY_PIN = 7;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("H:"); Serial.print(h); Serial.print(" | T:"); Serial.println(t);
if (h>75) {
int x = 0;
do {
Serial.println(h);
delay(2000);
x = dht.readHumidity();
} while (x < 66);
delay(1000);
digitalWrite(RELAY_PIN, LOW);
Serial.println("ON");
}
if (h<65) {
int y = 0;
do {
Serial.println(h);
delay(2000);
y = dht.readHumidity();
} while (y < 75);
digitalWrite(RELAY_PIN, HIGH);
Serial.println("OFF");
}
}
float h = dht.readHumidity();
//then later in loop()
if (h > 75)
{
int x = 0;
do
{
Serial.println(h);
delay(2000);
x = dht.readHumidity();
}
while (x < 66);
Am I reading this correctly ?
If the humidity is greater than 75 then read and display the humidity while it is less than 66 and in any case print the original humidity not the value just read
TomGeorge:
Hi,
Stop locking yourself in do..while..
Use if statements and use the values you have recorded at the top of your loop.
What do you want the code to do?
Just turn a relay on if the humidity is above a set point and off below a set point?
If so then code it.
Pseudo Code;
void loop
{
read humidity;
Serialprint humidity;
if humidity > setpoint
{
Turn ON relay;
Serialprint RelayON;
}
if humidity < setpoint
{
Turn OFF relay;
Serialprint RelayOFF;
}
}
Simple, though you may have to include some hysteresis to stop relay chatter.
Tom... :)
I want to turn on relay if humidity rises above 75% and keep on till humidity reaches 65% and 10 more minutes after that and in those 10 minutes If by any chance humidity irises again above 75% keep relay on tikk 65% and 10 minutes after that.
basicly trying to control dumb inline vents to keep humidity away from bathing area... kids can't remember to turn it on
void loop()
{
boolean relayIsOn = false;
static unsigned long timerStart = 0; // Timer is off
float percentRH = dht.getHumidity();
if (percentRH >= 75.0)
{
digitalWrite(RelayPin, LOW); // Active Low relay ON
relayIsOn = true;
timerStart = 0; // Disable the timer until the humidity gets down to 65%
}
// If the relay is on but the timer has not started, start it
// when the humidity gets down to 65%
if (relayIsOn && timerStart == 0 && percentRH <= 65.0)
{
// Humidity is down to 65%. Start the 10 minute timer
timerStart = millis();
}
// If the relay is on and the humidity got down to 65% so the
// timer was started, turn off the relay after the timer runs 10 minutes.
if (relayIsOn && timerStart != 0 && millis() - timerStart > 600000ul)
{
// Ten minutes is up. Relay off.
digitalWrite(RelayPin, HIGH); // Active Low relay off
relayIsOn = false;
timerStart = 0;
}
}