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);

These 4 lines is printing out this:

Temperature (°C): 23.0000000000

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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