IF statement confusion

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();  
  }
}

Remove the ';'

you sir, are a bloody champion. Thank You

After a while it gets really easy.
It is either a missing = or a superfluous ;
Unless it is something else and then things get difficult...

it amazes me every time i learn something about these things.

After it prints the temp and humidty to the LCD both lines have what look like two = stacked on top of each other

lcd.print((int)humidity); lcd.println(" % Humidity");

none of the other print to lcd does that?

any idea?

Can you post a picture?

Replace println with print.
I think you see the line carriage return character.
But lcd does not have a carriage return.

No idea what that really means but it worked. Thank you :slight_smile:

If you use println you tell the Serial monitor that you want the next thing on a new line.
The println function then adds 2 unprintable characters at the end of the string ( LF and CR).
The first tells the Serial monitor to go to a new line. The second to go to the beginning of that line. In ASCIl tables you can find these characters at the beginning of the set... there is also a character that causes a ping... don't know if that works with arduino IDE...
Apparently those characters are printed on your lcd as the double =.

You should mark the hint of the helper (@build_1971 ) as the solution and not your own post. It's a way to say 'thank you' :wink:.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.