Serial.write a float value

Hello
I have a float variable with a value like -99,9
I think I will need 3 bytes to store this value.I also need to transmit it using a Serial.write
How can I decompose the float and then seen it this way

Why not just send all four bytes of the float?

Can I do it using Serial.write(floatVar,4) ?

HugoPT:
Can I do it using Serial.write(floatVar,4) ?

The second argument is for passing an array; you're not passing it an array.

Thanks for your help
I already try to just Serial.write(floatVariable) but it just print 00 on Serial Port.

I'm also trying on this way

  float temp = -11.7;
  byte * b = (byte *) &temp;
  Serial.write(b[3]);
  Serial.write(b[2]);
  Serial.write(b[1]);
  Serial.write(b[0]);

But with this I still missing something since I don get the right values of the four bytes
About the Serial.write(floatVariable) from what I found on the manual it just work for a single byte and I have a float which are 4 bytes.I belive this is the cause why I get 00 running that right?

Syntax

Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

Arduino Mega also supports: Serial1, Serial2, Serial3 (in place of Serial)

Parameters

val: a value to send as a single byte

str: a string to send as a series of bytes

buf: an array to send as a series of bytes

len: the length of the buffer

Create a byte pointer and assign the address of the float to it. Then the Serial.write(myBytePointer, 4) should work.

I already try that using this code

float temp = -11.7;   
byte * b = (byte *) &temp;
  
  Serial.write(b,4);

On the output I get 33 33 3B - C1
I don't recognize this values

-11.7 is a float.

When converted to binary it becomes:
11000001001110110011001100110011
This in hex is:
C1 3B 33 33

The float is sent in reverse, so you get:
33 33 3B C1

:smiley:

Interestingly, you can achieve the same thing with this:

typedef union {
  float floatingPoint;
  byte binary[4];
} binaryFloat;

void setup() {
  // put your setup code here, to run once:
  binaryFloat hi;
  hi.floatingPoint = -11.7;
  Serial.begin(115200);
  Serial.write(hi.binary,4);
}

Unions a so cool! Basically the 'byte binary[4]' and 'float floatingPoint' are joined and share the same memory space, so you can access either, and if you change one, you change the other. You can have so much fun changing the byte array, and seeing what its floating point equivalent is :smiley:

I will try that then.
Just another question how did you convert the -11.7 to binary?(what tool you had used)
I'm trying to discover that on the Calculator but if I put in decimal -11.7 and convert to binary I don’t get this value.

http://www.h-schmidt.net/FloatConverter/IEEE754.html

Type -11.7 into the Decimal box, and press enter :smiley:

HugoPT:
I'm trying to discover that on the Calculator but if I put in decimal -11.7 and convert to binary I don’t get this value.

The float type is encoded. So many bits represent digits and one for the sign and some bits for the exponent part. But the binary representation of a floating point variable is never going to be numerically equal to the binary representation of the actual number.

For example take the number 1.0. Numerically, the binary equivalent of 1 is 1. Written as a byte it looks like 00000001. But as a float, it gets encoded into 00111111100000000000000000000000.

Ok I got it.That strange I was tryning that on windows calc but I get the result ?1011,10110011
:roll_eyes:
Perhaps windows have a special calculator:D :roll_eyes:

For example take the number 1.0. Numerically, the binary equivalent of 1 is 1. Written as a byte it looks like 00000001. But as a float, it gets encoded into 00111111100000000000000000000000.

Sounds trouble for me then.Since after transmit it I need to rearrange it back on the receiver side.

For a quick and dirty solution, multiply it by 10000 before you send it, cast it to an int, send the characters as ASCII, and then divide by 10000 on the PC side to get back your original float. (Watch for overflows though...)

You could transmit it the opposite way. E.g. for the example code I gave

for (char i = 3; i >= 0; i++){
  Serial.write(hi.binary[i]);
}

But I think the recieving program, e.g. if it was written in C, would expect the bytes in the reverse order as they were sent in the order they were stored in the memory. You haven't said what you are using to recover it, but if it is C code, you could use the same union typedef in you recievers program, and read the incoming bytes to the binary[4] array, and then you would have it available as a float without any extra math.

TCWORLD:
Unions a so cool! Basically the 'byte binary[4]' and 'float floatingPoint' are joined and share the same memory space, so you can access either, and if you change one, you change the other. You can have so much fun changing the byte array, and seeing what its floating point equivalent is :smiley:

I've done something similar in COBOL, if you can believe it. You can reinterpret all or parts of a display value (backed by any number of binary formats) to do all sorts of unnatural things with it.