system
January 26, 2014, 8:39pm
1
I have one problem ... I use 18b20 sensors its works ok ,but his sensitivity is 0,01 ...he show me 15,56 ... 15.87 ...but I need something like 15,5 ... 16,0 .... just 0,5 diference how to do that ....
part of my code
#include <DallasTemperature.h>
#include <OneWire.h>
OneWire oneWire(9); //pin 9
DallasTemperature sensors(& oneWire);
sensors.requestTemperatures();
float Senzor1;
Senzor1 = sensors.getTempCByIndex(0);
Serial.print("Senzor1: ");
Serial.println(Senzor1);
thank you
MarkT
January 26, 2014, 8:53pm
2
You want to round to the nearest multiple of 0.5.
So first convert to an integral multiple:
int half_degrees = (int) (Senzor1 * 2.0 + 0.5) ; // 0.5 for unbiased rounding
Senzor1 = half_degrees * 0.5 ;
Or combine in one line
Senzor1 = 0.5 * (int) (Senzor1 * 2.0 + 0.5) ;
system
January 26, 2014, 9:14pm
3
Thank you ,I will try ...
Or you can set the sensor to 9-bit precision which will give you half a degree accuracy.
Also, you should print the result like this:
Serial.println(Senzor1,1);
so that it only prints one fractional digit.
Pete