cbirchy87:
Hi,I am looking into playing around with IF and Else. However I seem to be having issues. Below is my code. I want the built in LED to be on when temp is 21 or above and low if below. The LED does turn on when its above 21 however doesn't seem to turn off.
Is it the delay() thats causing the issue?
float tempC;
int reading;
int tempPin = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
analogReference(INTERNAL);
Serial.begin(9600);
}
void loop()
{
reading = analogRead(tempPin);
tempC = reading / 9.31;
Serial.println(tempC);
delay(10000);
if(tempC>=21)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if(tempC<=20)
{
digitalWrite(LED_BUILTIN, HIGH);
}
}

Couple things I see is that with your if - else
right now you are ignoring a whole degree in there where nothing will happen between 20 and 21
You should be able to write it like this to get the whole range:
if(tempC>=21)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
digitalWrite(LED_BUILTIN, HIGH);
}
By leaving out the second if anything from negative to 21 degrees will be the if condition and then anything above 21 such as 21.000001 will be else.
and yes as someone else said the If and Else are doing the same thing so you will never know if it is changing.