I want to grab each byte of a float and send it using LoRa. C++ is overprotective of its variable types and gets real irritated when you (that is, me) try to disassemble, send them, and then reconstruct numbers (ints, floats, doubles). Is there a function (like highByte()) that can pluck the 3rd or 4th byte out of a float?
I'm working with is the MPU6050, which has in its library support for:
x = RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
where the variables are all integers. Obviously, "x" isn't an integer, it's declared as a double in the example code, but this code forces it to be a float.
I've considered making "x" an integer by doing something like this:
x = init() (RAD_TO_DEG * (atan2(-yAng, -zAng)+PI) * 100) ;
then converting back to a float at the receiver with a simple:
x = x / 100.0 ;
My second question is, will the function init() force "x" to be an integer variable representing hundredths of degrees?
If you have questions about code, please post ALL the code, using code tags.
But for your first question, using most of the common radio libraries, there is no need to disassemble a float to send it. Something like this works with RadioHead, for example
radio.send((uint8_t *)&floatval, 4); //send four consecutive bytes
Since sensor data is almost always integer valued, send integers. To send an integer representing a float in 1/100 degree use
int x = 100.0*floatval:
Make sure that x does not exceed 32676 on an AVR based Arduino.
Thanks, I have struggled with unions before, I guess I should spend some time on the concept.
As far as the init() function, all I can say is I've seen some code that implied there was a function that would convert the number to the right into a different type, eg, float to integer or vice versa. Maybe what I had seen was
x = (init) floatval ;
What jremington posted above looks straightforward (int x = 100.0 * floatval ;). I'm going to give that a try.
There are excellent C/C++ references on line; always a good idea to check, especially when it comes to operator precedence, which can be very confusing.
what Lora board are you using?
put your floats and other data in a structure and transmit the complete structure
for example have a look at decimals-strings-and-lora