Ive been working on a humidity controller using DHT11 sensors, everything is wired and works fine apart from my code.
I can make sense of the IF statement, my code turns on relays when the humidity is < 60% and turns them off when it reaches over 60 %
The code works fine under 60%, but when the sensor reaches over 60% it runs the < 60 % statement once then goes back to > 60%.
I know its just something in the way the IF statement is setup but I think ive been looking at it too long and it dosent make sense anymore.
#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int pinDHT11 = 2; // Declaring digital pin no 2 as the dht11 data pin
int DHTpower = 3; //
int exfan = 4; // exhauast fan
int inletv = 7; // inlet valve
int exhaustv = 5; // exhaust valve
int infan = 6; // inlet fan
SimpleDHT11 dht11;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16x2 display
void setup() {
pinMode(inletv, OUTPUT);
pinMode(infan, OUTPUT);
pinMode(exfan, OUTPUT);
pinMode(exhaustv, OUTPUT);
pinMode(DHTpower, OUTPUT);
digitalWrite(DHTpower, LOW);
digitalWrite(infan, LOW);
digitalWrite(exfan, LOW);
digitalWrite(inletv, LOW);
digitalWrite(exhaustv, LOW);
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("The Greeen");
lcd.setCursor(0, 1);
lcd.print("Ninja");
delay(2000); // Display the startup message for 2 seconds
lcd.clear(); // Clear the LCD screen
}
void loop() {
delay(1000);
RHcheck(); //check Humidity Level
delay(2000); //wait
while (Serial.available() > 0)
lcd.clear(); // Clear the LCD screen
}
void RHcheck() { //Check Humidity Level Function
digitalWrite(DHTpower, HIGH); //On Humidity Sensor
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Check Humidity");
delay(1000);
lcd.clear();
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
//This bit will tell our Arduino what to do if there is some sort of an error at getting readings from our sensor
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("No reading , err="); Serial.println(err);delay(100);
}
lcd.print((int)temperature); lcd.print(" C, ");
delay(1000);
lcd.setCursor(0,0);
lcd.print((int)humidity); lcd.println(" % Humidity");
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
if ((int)humidity > 60 ){ // Ok Humidity - Do nothing
digitalWrite(DHTpower, LOW);
delay(50);
lcd.print("Fans OFF");
delay(1000);
lcd.clear();
digitalWrite(inletv, LOW);
digitalWrite(exhaustv, LOW);
delay(2000);
digitalWrite(infan, LOW);
digitalWrite(exfan, LOW);
}else if((int)humidity < 60 );{ // Low Humidity
digitalWrite(DHTpower, LOW);
delay(50);
lcd.print("Fans On");
delay(1000);
lcd.clear();
lcd.print((int)humidity); lcd.println(" % Humidity");
lcd.setCursor(0,1);
lcd.print((int)temperature); lcd.println(" Celcius");
delay(1000);
digitalWrite(inletv, HIGH);
digitalWrite(exhaustv, HIGH);
delay(5000); // give valves time to open
digitalWrite(infan, HIGH);
digitalWrite(exfan, HIGH);
lcd.clear();
}
}