horace:
try casting the float to an int which will remove the fractional component
lcd.print((int)tempC, DEC);
Thank you it works.
what is the function of following statement
tempC == 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1
Good question sir. If my LM36 reads the following temperature 21, 23 etc. (all odd numbers) I want to turn my LED ON. But if LM36 reads Temperature 22, 24, etc. (all even numbers) I want it to turn my LED OFF.
Is it possible to do this sir?
Thank for your response. Big help for newbie like me.
But I am confused because what I want is to let my LED turn ON only if LM36 will give me ODD numbers like 21,23, etc. And it will turn on only if EVEN numbers. How to do this?
cast tempC to an int and use the % (modulus) operator to test if the result is even or odd
e.g.
int x=3;
if(x % 2) printf("odd"); else printf("even");
prints "odd"
in the case of
tempC == 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1
you are using the , (comma) operator where tempC is compared with 21.1 and the overall reult of the expression is 35.1 which is true when used in a if statement
horace:
cast tempC to an int and use the % (modulus) operator to test if the result is even or odd C - Operators
e.g.
int x=3;
if(x % 2) printf("odd"); else printf("even");
prints "odd"
in the case of
tempC == 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1
you are using the , (comma) operator where tempC is compared with 21.1 and the overall reult of the expression is 35.1 which is true when used in a if statement
the % operator gives you the remainder of a integer division
therefore the remainder of a division by 2 will determine if the first operand is even or odd
e.g.
Please read how to use the forum and use code tags for posting code. You can fix your posts by editing them and adding [code]....[/code] around your lines of code)
Note that now that instead of checking for exact values you check for a range (> 26) there is no need to convert to integer anymore (careful you check for >26 and <26 but nothing will happen at exactly 26, the second if in the else is not necessary, this way you capture the opposite condition)
I would recommend to uselcd.print(tempC, 0);as converting to an int is truncating, not rounding, so if your temperature is 25,9997°, your code shows 25 whereas you are much closer to 26°. The second form for the display will round up the number at the right number of digital places (here i say 0 decimal data, so rounding up to the closest integer) and will display 26
Odd or even don't make much sense with floating point data, so of course there it's good to convert but then again you are truncating and you need to wonder if this is what you want to display or not
(That being said it all depends on the resolution and accuracy of your sensor, some can be off by 1°C easily so there is limited value in over-engineering that result)
Word of wisdom---DON'T crosspost. Keep the thread in one spot, don't post it again in another area. It's too hard to follow and disrespectful to those that are trying to help you.
As for your question, it was recommended to use the modulo operator to find it as odd/even. He has it to print odd or even, but you can always change that to an int and use it from there