So I have connected the LCD display to work with the DHT11 sensor and it is currently working good and reading correctly. However I can't get the if and else statement to work with the actual temperature readings from DHT11 to activate a LED. The temperature readings are in xx.xx decimal format, so I don't know if that is the problem or anything. But here is the part of my if and else statement. It is currently 16.00 degrees Celsius in my room, but the serial monitor keeps saying that the fan is off, when it should be on. Also when I switch the less than to greater than, it says the fan is on but it should be the other way around?
if ("t" < 18){
digitalWrite(Gate,HIGH); //Turns FAN on
digitalWrite(8,HIGH); // Turns LED on
Serial.begin(9600);
Serial.print ("FAN is on");
delay(1000);
}
else{
digitalWrite(Gate,LOW); //Turns fan off
digitalWrite(8,LOW); //Turns LED off
Serial.begin(9600);
Serial.print ("FAN is off");
delay(1000);
}
}
Full Code:
#include <dht.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
dht DHT;
#define DHT11_PIN 7
#define Gate 10
void setup(){
lcd.begin(16, 2); //Turns LCD on
pinMode(Gate, OUTPUT); // Sets PWM pin of the gate to output
digitalWrite(Gate,LOW); // Sets gate LOW or fan off when first turn on
digitalWrite(8,LOW); //Sets LED low when first turn on
pinMode(8,OUTPUT);
Serial.begin(9600);
}
void loop()
{
int chk = DHT.read11(DHT11_PIN);
float t = DHT.temperature;
float h = DHT.humidity;
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Temp.: "); // Prints string "Temp." on the LCD
lcd.print(t); // Prints the temperature value from the sensor
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Humi.: ");
lcd.print(h);
lcd.print(" %");
delay(2000);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(1000);
if ("t" < 18){
digitalWrite(Gate,HIGH); //Turns FAN on
digitalWrite(8,HIGH); // Turns LED on
Serial.begin(9600);
Serial.print ("FAN is on");
delay(1000);
}
else{
digitalWrite(Gate,LOW); //Turns fan off
digitalWrite(8,LOW); //Turns LED off
Serial.begin(9600);
Serial.print ("FAN is off");
delay(1000);
}
}
Example of Serial Monitor:
Temperature = 16.00
Humidity = 21.00
FAN is off,Temperature = 16.00
Humidity = 21.00
FAN is off,Temperature = 16.00
Humidity = 21.00