0

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

3

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;
2
  • 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
    Commented Jan 20, 2015 at 15:21
  • 1
    For extra reading: en.wikipedia.org/wiki/Comma_operator
    – Omer
    Commented Jan 20, 2015 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.