1

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

}

enter image description here

1 Answer 1

1

firstly, move your pinMode() settings to setup section.

in your code, when the temperature is 21 active pin will be 6 and 8. you should rewrite your code in next ways:

1:

if (t < 22) {
  ... its cold temp ...
} else if (t > 26) {
  ... its hot temp ...
} else {
  ... it's nor temp ...
}

2:

if (t < 22) {
   ... its cold temp ...
}
if (t > 26) {
   ... it's hot temp ...
} 
if (t >= 22 && t <= 26) {
   ... it's nor temp ...
}

and my small advice for u, rewrite in this way, it is more readable

digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);

digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);

digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);

visually changing ENUMS (like LOW or HIGH) more noticeable than changing numbers (6/7/8)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.