So my project is including detecting the temperature in the room by using dth11 sensor. my idea is to add 3 LED lights were each light represent the temperature if its cold or hot or perfect.
the wiring and coding is done and it's working .However, i'm having a problem were the perfect temperature is always on no matter if the temperature is hot or cold. example: if the temperature is cold the cold LED will blink and the perfect temperature LED will be on.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN A0
#define DHTTYPE DHT11
#define LED_TOO_COLD 6
#define LED_PERFECT 8
#define LED_TOO_HOT 7
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
}
void loop() {
pinMode (6 , OUTPUT);
pinMode (7 , OUTPUT);
pinMode (8 , OUTPUT);
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
if (t < 22) {
Serial.println("Too cold!");
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay (2000);
}
if (t >26) {
Serial.println("Too hot!");
digitalWrite(7, HIGH);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay (2000);
} else {
Serial.println("Perfect temperature!");
digitalWrite(8, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
delay (2000);
}
}
