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.
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.
I would also say the delay(10000) is in the wrong place. Take reading, wait 10 sec, turn on/off LED. Would it not be better to take reading, turn LED on/off, wait 10 sec?
bsohn:
right now you are ignoring a whole degree in there where nothing will happen between 20 and 21
I assumed the two if() statements were to give some Hysteresis in the ON/OFF control otherwise you could get the LED flickering on/off around the 21degree temperature set point
horace:
I assumed the two if() statements were to give some Hysteresis in the ON/OFF control otherwise you could get the LED flickering on/off around the 21degree temperature set point
That is true so that would work if that is what is intended.. and you could tighten it up if you wanted less than a degree of hysteresis.
Just about if-else, and one tip – while coding use the AutoFormat tool (ctrl-t) often as it checks syntax. It’s easier to keep code straight a little at a time than write up a load of code and then start debugging all the problems at once.
if(tempC>=21)
{
digitalWrite(LED_BUILTIN, HIGH);
}
else if(tempC<=20)
{
digitalWrite(LED_BUILTIN, HIGH); // so no change?
}
Your code has no hole but the if(tempC<=20) isn’t needed since everything not >= 21 must be < 21 (<= 20).
The compiler very likely does not code the second if().
Practice pruning and it won’t be long before you won’t have to… much… you will be able to read code much easier.
The else if is for when you want to chain if()'s.
Here the if-else-if-else… chain slices possible values for tempC from the top down. Once everything >= 21 are taken, everything >= 18 can only be 18, 19 or 20. Every cut[/] takes away from what’s left and the last else gets that. Here’s the routine in pseudo-snippet: ```
GoForSmoke:
Your code has no hole but the if(tempC<=20) isn’t needed since everything not >= 21 must be < 21 (<= 20).
The compiler very likely does not code the second if().
What if tempC is between 20 and 21, such as 20.5? It’s a float variable, after all.
The OP can tackle that (unless someone wants to provide the answer). I don't care to look it up, AFAIK there is an issue. I just cast the ADC integer reading as a float and move on with life.
adwsystems:
Along those lines, what is the result of the adc conversion formula tempC = reading / 9.31; that is float = int / float?
I avoid problems like this so I don’t have to remember all the nuances.
a float.
and then
if ( float < 21 )
I’d use the read to work with directly. It’s faster and easier, takes less code and RAM.
Turning it into more digits does not improve the accuracy. Only convert to show to humans.
PS: AVR datasheet, ADC Absolute Accuracy is +/- 2 LSB’s. The read can be off +/- 2.