Decimals always 00 in example OneWire temp sensor DS18B20

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
ove.nystas
Posts: 3
Joined: 23 Oct 2016 22:42
Location: Sweden

Decimals always 00 in example OneWire temp sensor DS18B20

Post by ove.nystas »

I tried the example "OneWire temperature sensor DS18B20" and got the Z-Uno to be included with my Fibaro HC2 controller and it was delivering temperature values.

However I noticed that the decimal part of the temperature values in the controller was always zeroes but the serial monitor showed the correct decimal values. A temperature that shows for example 23.81 degC in the serial monitor showed up as 23.00 degC in the controller. I investigated this and found this line to be the problematic one.

Code: Select all

temp=int(temerature*100);
The variable temerature is in float. (It is also misspelled in the example. It should be temperature.)

Somehow when I compile this (using Arduino 1.6.5, Z-Uno 2.06 on Linux) it gets translated into:

Code: Select all

temp = (int)temerature * 100;
So temerature is first casted to int which loses the decimals, and then multiplied by 100.

A workaround for this problem is to add more parenthesis, like this:

Code: Select all

temp=int((temerature*100));
Then the casting to int is working as intended and the real decimals show up also in the controller.

But since the ZUNO_DS18B20 library has a function to get the temperature as an int and already multiplied by 100, called getTempC100(), it is better to write the code in the other way around like this:

Code: Select all

// obtaining readings from the sensor ds18b20
temp = ds1820.getTempC100(addr1);
// make descaled float value for serial print
float temperature = (float)temp / 100.0;
It would be good if the example could be updated to avoid other people running into the same problem when trying it out?

Do someone have an explanation of why the function-style casting of int(temperature*100) doesn't work as expected?
p0lyg0n1
Posts: 193
Joined: 04 Aug 2016 07:14

Re: Decimals always 00 in example OneWire temp sensor DS18B2

Post by p0lyg0n1 »

Thank you, added to uCxx issues. Looks like constructor cast working for first token only.
Post Reply