fehlfarbe / arduino-motorfocus

Arduino motorfocus with moonlite protocol for telescopes
MIT License
48 stars 13 forks source link

DS18B20 temperature sensor no "negative values" #28

Closed nettylink closed 8 months ago

nettylink commented 8 months ago

Hi,

I'm using the arduino-motorfocus with N.I.N.A. and have noticed, that negative temperatures are not shown correctly. With negative temperatures always 0.00° are shown.

The current code always returns "0#" on negative temperature readings of the DS18B20.

I've change the following code from:

    byte t_int = (byte)temperature << 1;
    t_int += round(temperature - (byte)temperature);
    Serial.print(t_int, HEX);
    Serial.print('#');

to:

    int t_int = (int) temperature;
    int tmp = (t_int * 2);
    char tempString[8];
    sprintf(tempString, "%04X", (int) tmp);
    Serial.print(tempString);
    Serial.print('#');

I guess there are better solutions, but with this code negative temperatures are shown correctly.

fehlfarbe commented 8 months ago

Thanks! I tested several values and simplified the code a bit :)

nettylink commented 7 months ago

Hi,

thank you very much for the new code. I've also tested a bit more.

When duplicating the temperature as integer, we get a 1 degree resulution.

int16_t t_int = (int16_t)(temperature*2);

When duplicating the temperature as a float, we get a 0.5 degree temp resulution.

float d_temperature = (float)(temperature*2);
int16_t t_int = (int16_t)(d_temperature);
Here are few test values. temperature (from DS18B20) tempString (old) tempString (old as int) in N.I.N.A. (old) tempString (new) tempString (new as int) in N.I.N.A. (new)
4.49 0008 8 4.00 0008 8 4.00
4.51 0008 8 4.00 0009 9 4.50
-4.49 FFFFFFF8 -8 -4.00 FFFFFFF8 -8 -4.00
-4.51 FFFFFFF8 -8 -4.00 FFFFFFF7 -9 -4.50
fehlfarbe commented 7 months ago

Ok that's weird because the temperature is duplicated as float in the parenthesis and afterwards casted to integer so it should be the same :thinking:

(int16_t)(temperature*2) == (int16_t)((float)(temperature*2))