Temperature Reading

Hello guys,

I am new with arduino. Currently I am currently working with my Room Temperature using LM36 with display in 16x2 LCD.

Can someone help me with my codes?

  1. How can I display the reading of my LM36 without decimals?

Here is my code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int LED = 10;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("MMTI Temperature");
pinMode(LED, OUTPUT);
}

void loop() {
//Temperature reading area.
int sensorValue = analogRead(A0);
float volt = (sensorValue/1020.0) * 4.9;
float tempC = (volt -0.5) * 100;
Serial.println(tempC);
lcd.setCursor(5, 1);
lcd.print(tempC);
lcd.print("C");
delay(500);

//Conditional area.
if(tempC == 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1 ) {
digitalWrite(LED, HIGH);
}
else if(tempC != 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1 ) {
digitalWrite(LED, LOW);

}

//-----------------------------

}

try casting the float to an int which will remove the fractional component

lcd.print((int)tempC, DEC);

what is the function of following statement

tempC == 21.1, 23.1, 25.1, 27.1, 31.1, 33.1, 35.1

horace:
try casting the float to an int which will remove the fractional component

lcd.print((int)tempC, DEC);

Thank you it works. :slight_smile: :slight_smile: :slight_smile:

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.

Hint For the first question:

Serial.println(1.23456, 0) gives "1"
Serial.println(1.23456, 2) gives "1.23"
Serial.println(1.23456, 4) gives "1.2346"

Your if are indeed to be checked, the == operator in c++ does not work this way ('you are only comparing with the last value of the list here)

J-M-L:
Hint For the first question:

Serial.println(1.23456, 0) gives "1"
Serial.println(1.23456, 2) gives "1.23"
Serial.println(1.23456, 4) gives "1.2346"

Your if are indeed to be checked, the == operatot in c++ does not work this way ('you are only comparing with the last value of the list here)

Hello sir J-M-L,

First thank you another knowledge in programming.

This is what I did sir,

if(tempC == 26 ) {
digitalWrite(LED, HIGH);
}
else if(tempC != 26) {
digitalWrite(LED, LOW);
}

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?

Sorry new in programming.

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

Sorry sir I am confused where to code the intx=3;

Here what I did sir.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int LED = 10;
int x = 3;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("MMTI Temperature");
pinMode(LED, OUTPUT);
}

void loop() {
//My Temperature reading area.
int sensorValue = analogRead(A0);
float volt = (sensorValue/1020.0) * 4.9;
float tempC = (volt -0.5) * 100;
Serial.println((int)tempC, DEC);
lcd.setCursor(6, 1);
lcd.print((int)tempC, DEC);
lcd.print("^C");
delay(500);

//My Conditional area.
if(x % 2) printf("odd");

else printf("even");

if(tempC > 26 ) {
  digitalWrite(LED, HIGH);
}
else if(tempC < 26) {
  digitalWrite(LED, LOW);

}

//-----------------------------

}

Is it right? or something wrong with this.

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.

    float tempC=27.1 ;
     if((int)tempC % 2) printf("odd"); else printf("even");

tempC is cast to an int, then divided by 2 and the remainder determinaes the result of the expression - in the above case prints "odd"

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)

I've follow all your recommendation & suggestion sir it works fine now. Here is code.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int LED = 10;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("MMTI Temperature");
pinMode(LED, OUTPUT);
}

void loop() {
//Temperature reading area.
int sensorValue = analogRead(A0);
float volt = (sensorValue/1020.0) * 4.9;
float tempC = (volt -0.5) * 100;
Serial.println(tempC, 0);
lcd.setCursor(6, 1);
lcd.print(tempC, 0);
lcd.print("^C");
delay(500);

//Conditional area.
if(tempC > 26 ) {
  digitalWrite(LED, HIGH);
}
else if(tempC < 26) {
  digitalWrite(LED, LOW);

}

//-----------------------------

}

The result of this code obviously if tempC reads greater than 26 time to turn ON LED. When tempC reads less than 26 time to turn OFF LED. Right?

But now what I want is to make it like this. If the tempC reads odd numbers

21, 23, 25, 27, 29, 31, 33, & 35

LED Well turn ON

But if tempC reads even numbers

22, 24, 26, 28, 30, 32

LED well turn OFF

Is it possible to do this?

THANK YOU :wink:

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

Not answering to cross posting indeed