Dallas Temperature Library celsius as non floating number?

Is it possible to directly retrieve degrees C by index as non floating number from the DS18B20? I can't figure it out.

Instead of 22.34 > 2234

The nice part of getting libraries as "source code" is that you can fix them to do what you need if you are motivated.

You could always COPY the standard onewire related library files to your project folder and modify them as needed to eliminate floating point math... but this is an exercise left to you to perform. (modifying the "convert float" routines in the Dallas temp library)

search and you will find them in the main cpp file

So it’s not possible with the library as it is?
The thing is i did read its best to avoid float on the Arduino.

Project folder?
I have another library i modified for the Teensy so i can use the second SPI but i did not find out how to duplicate a library and give it another name.

So i can do:
#include <RFM69.h>
on a Arduino and
#include <RFM69_SPI1.h>
on a Teensy.

At the moment i duplicated the whole Arduino IDE and have to watch out which one i use for what board.

The thing is i did read its best to avoid float on the Arduino.

Where that makes sense. Obviously, in the case of reading the temperature, it does not.

How you USE the temperature may make sense using a floating point value, or it may make more sense as an int. I seriously doubt that you are getting 0.01 degree resolution with any temperature sensor connected to an Arduino. Or that you need it.

But at the moment i read it as float and then convert it to an int so it would be better to directly get it as int.

What has the resolution to do with it? Even with just 0.5 degree resolution it is floating point?

MrGlasspoole: The thing is i did read its best to avoid float on the Arduino.

Are, so THAT's what this is about - I was scared to ask.

Using floats might be frowned upon by the purists and speed freax but, if that is your problem, you shouldn't be using DS18B20s anyway. Here in the real world, I submit you can ignore that stuff until it is clear you have a problem. Don't hold your breath while waiting, and the idea of editing the library is absurd. The sensor gives you a float, so just read it and use it, like everybody else does.

What has the resolution to do with it?

Well, if the resolution - the step size between unique values - is 5 degrees, does it really make sense to report the temperature to 2 decimal places? To 1? As a float, at all?

How about you quit changing the subject, and tell us what the Arduino is going to do with the temperature? There are times where avoiding floats makes sense, and times where it doesn't.

This is, as near as I can tell, based on what you have shown so far, one of the latter.

Sparkfun are speed freaks :slight_smile: ?
https://learn.sparkfun.com/tutorials/data-types-in-arduino

The sensor outputs a float? A decimal number?
Then what is all this i don’t understand anyway:

// convert the data to actual temperature

unsigned int raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
    // count remain gives full 12 bit resolution
    raw = (raw & 0xFFF0) + 12 - data[6];
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
     // default is 12 bit resolution, 750 ms conversion time
}

@PaulS, it is related to this thread and there also it was recommended to not use float:

I figured out now how to make that float an int.
Then i was reading that float is evil on Arduino…
And thought if it is possible to directly get it as int from the sensor…

But it seems like from Nick’s answer the sensor does give floats…

Sparkfun are speed freaks :) ?

Not in the shipping department.

The sensor outputs a float?

That was YOUR assertion, not ours.

That (snippet of) code is getting TWO bytes from the device, and converting them to an int.

Nick_Pyner: The sensor gives you a float, so just read it and use it, like everybody else does.

MrGlasspoole:

Nick was basing his statement on your assertion that the library returned a float. Since your assertion that the library returned a float was erroneous, you can stop digging now. Your hole is deep enough.

A decimal number is a float…
getTempCByIndex outputs xx.xx but is not a float…

Seems like I’m to stupid for that stuff or my english is to bad.

These methods return integers:

int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, uint8_t* scratchPad)
int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress)

Seems like I’m to stupid for that stuff or my english is to bad.

The problem is that you are posting snippets, out of context.

The snippet you last posted IS getting an int from the sensor. But, that int is NOT the temperature. The int is converted to a temperature, as a float, by rawToCelcius(). The functions that get temperatures all call rawToCelcius() to convert that integer obtained from the device to a temperature.

You could, if you wanted to, modify the library to make all the functions return ints. But, the rawToCelcius() method still needs to perform floating point math, so you will have gained nothing by making the library truncate the value to an int before returning it vs. just storing the temperature in an int (prior to or after multiplying it by 100). In fact, you will loose precision if you make the getTempC() function return an int. Since it currently returns a value like 45.22, which you multiply by 100 to get 4522, forcing it to return an int will give you 45 - a 0.5% loss of precision in this case.

It occurred to me under the shower last night that, while you still need the wire library, it is possible to use DS18B20s on one wire but without the DallasTemperature library and there is actually a programme around that demonstrates that. I don’t supposes there is any point in discussing any further about why you would want to go along such a path to absurdity, but at least it allows you access to the raw data from the DS18B20 - something that nobody else needs - and mess about with ints to your heart’s content, without having to undo Miles Burton’s hard work. I believe this programme is included in the wire library. Not only is it about the most stupid programme ever written, but it actually admits it by reminding you that the Dallas Temperature library is available, and with a thinly veiled implication that using it is a good idea.

Similarly, but even more silly, if you Google DS18B20 Sheepdog, you can run multiple DS18B20s to the same effect, but without even the wire library. Just what everybody needs…

@Paul ok, now it makes sense. Thanks for the explanation! If every tutorial/example were like this it would be a lot easier to understand what is going on.

@pwillard, i tried what you said about the "project folder" and it seems like i have to put all the files from a library directly there? http://arduino.stackexchange.com/questions/8651/loading-local-libraries/9575 That means in my case 10 files. And every time there is an update to a library i have to remember where i have that library beside the standard library folder?