I am trying to store temperature data in a variable, so that I can use it for making a JSON object, but the problem is, that when I try to store the temperature in a variable, it prints out the wrong value. I'm using a DHT11 temperature/humidity sensor and a library for calculating the temperature.
Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, DEC);
int temp1 = ((float)DHT11.temperature, DEC);
Serial.println(temp1);
These 4 lines is printing out this:
Temperature (°C): 23.0000000000
10
The first one is correct, but the second line should also be saying "23". I have tried only loading the data in once with the library, as in outcommenting one line, but that does not work. It's like I have some syntax wrong, but I can't figure out what.
1 Answer 1
This line:
int temp1 = ((float)DHT11.temperature, DEC);
casts DHT11.temperature
to an float
, throws away the value, then assigns DEC
to temp1
. That is not what you want.
int temp1 = DHT11.temperature;
-
Thank you so much. This fixed it straight away. I knew it was just some stupid syntax that I was doing wrong, but I just couldn't figure out what. Thnx.Djensen– Djensen2015年01月20日 15:21:46 +00:00Commented Jan 20, 2015 at 15:21
-
1For extra reading: en.wikipedia.org/wiki/Comma_operatorOmer– Omer2015年01月20日 23:59:21 +00:00Commented Jan 20, 2015 at 23:59
Explore related questions
See similar questions with these tags.