# DS18S20 float temp converting to byte

Is it poseble to make an algoritme that converting a float temp to a byte.
from -10.0C to 90.0C
i need when a temperature is.
float -10.0C → byte is 1 or something
float -9.5C → byte is 2
float -9.0C → byte is 3

float 89.5C → byte is 254
float 90.0C → byte is 255 or what ever matses

void IOSendMSG(byte func, byte funcode, int huse, float tmp1, float tmp2){

if (tmp1 == -127.00){
Serial.print(“tmp1 NA:”);
Serial.println(tmp1);
}
if (tmp2 == -127.00){
Serial.print(“tmp2 NA:”);
Serial.println(tmp2);
}

new algoritme in here

Serial.print(“huse:”);
Serial.println(huse);
Serial.print(“tmp1:”);
Serial.println(tmp1);
Serial.print(“tmp2:”);
Serial.println(tmp2);

}

thx.

Try this:

``````void setup()
{
Serial.begin(115200);
for(float f=-10.0;f<=90.0;f+=0.5)
{
char buf[16];
dtostrf(f,4,2,buf);
Serial.print("temp: ");
Serial.print(buf);
Serial.print(" ");
Serial.println((byte)((f+10)*2.55));
}
}

void loop()
{
}
``````

That's just math. Computers are good at math. Unfortunately too many humans blow off that class in school. The programming would be easy as pie once you figure out the math you want.

It looks like you are trying to measure half degrees. So use the map() function but remember that it can't take a float. Multiply by 2 so that the halves are now whole numbers. Then use the constrain() function to take care of out-of-range inputs.

``````outputByte = map(constrain(tmp1*2, -10*2, 90*2), -10*2, 90*2, 1, 255);
``````

I leave all the "*2" in there so it's easy to think in degrees. The compiler will actually do that multiplcation before it uploads the code to the Arduino, so it will run very quickly without doing a lot of repetitive multiplications.

Of course -10 to 90 is only 200 half-degrees. You might want to pick a range that has exactly 254 half degrees.

thanks for the replays

it works perfect.
but if i want to convert it the other way also how do i do that.

eks.
1=-10.0C
2=-9.5C
3=-9.0C
4=-8.5C

void setup() {
Serial.begin(9600);
for(float tmp1=-10.0; tmp1<=90.0; tmp1+=0.5){
Serial.print(“tmp1:”);
Serial.println(tmp1);
byte outputByte = map(constrain(tmp12, -102, 902), -102, 90*2, 1, 201);
Serial.print(“outputByte:”);
Serial.println(outputByte);
}
}

if x is the float try

``````int i= round(x*2)+21;
``````

if x = -9.5 i will be 2 etc note if x=90 i will be 201 not 255 what if x =-9.7 what result would you expect? i equals 1 or 2

The output should be the same as the input half degrees interval. it should be reversering the map(constrain(tmp1*2, -10*2, 90*2), -10*2, 90*2, 1, 201); to half degrees from -10 to 90

eks. 1 =-10.0C 2 =-9.5C 3 =-9.0C 4 =-8.5C .... 200 = 89.5C 201 = 90C