Reputation: 3
I am newbie to arduino and c. I am working on a project in which I have a DHT11 and nodemcu esp8266. In this project I want to control an AC . Condition I want is Ac must turn on when temperature goes above 32.00 degree and stay on until temp goes below 30.00. After going below 30.00 degree AC should turn off and turn on only when temperature goes above 32.00 degree.
I am successfully turning ac on when it goes above 32.00 but it never turn off even if temp goes below 30.00. On resetting nodemcu it turns off.
I think my while loop is not breaking . Pasting my code below please help.
void loop() {
float t = dht.readTemperature();
if (t > 32.00) {
while (t > 30.00) {
float t = dht.readTemperature();
digitalWrite(r1,HIGH);
Serial.print(t);
Serial.println("Ac_on");
delay(1000);
}
}
else {
float t = dht.readTemperature();
digitalWrite(r1,LOW);
Serial.print(t);
Serial.println("Ac_off");
delay(1000);
}
}
Upvotes: 0
Views: 48
Reputation: 409136
You have two very different and distinct variables with the name t
. First the one defined outside the loop, and then the one defined inside the loop. The loop condition can only "see" the one defined outside the loop.
Variables defined inside an inner scope shadows the variables of the same name in an outer scope, and they are (as mentioned) different and distinct.
The solution is to assign to the variable t
inside the loop, not define a brand new variable:
t = dht.readTemperature();
On a different note, you probably don't need to refetch the temperature in the else
case.
Upvotes: 1