Using Lora I wish to send a 3 decimal point number (eg 12.345)to another Lora unit;
have never used " float" before and am a bit confused.
The receiving Lora must receive it as a decimal point number.
I don't know how an Arduino communicates with a lora module. Based on serial, you could use Serial.print(someFloatVariable,3)
.
You can also just send the float in binary format without loosing precision using Serial.write((byte*)&someFloatVariable, sizeof(float))
. In that case you leave it up to the receiver to do what needs to be done. Be aware that if the receiver does not use the same underlaying byte sequence for floats as the sender, you'll have to cater for that.
The 3 decimal point requirement is a bit false, because this is exactly the same format as any other places up to the maximum the format will supply.
This tells you more about floating point numbers than you need to know.
The Arduino uses four bytes for both the float and the double types of float.
I could send a whole integer and reconvert to a decimal at the receiving end.
EG transmit 2.5 as 2500 then divide it by 1000 at receiving end to get back to 2.5 but
I am not sure of how to use "float".
float aaa;
float temp ;
void setup() {
Serial.begin(9600);
}
void loop() {
aaa = 2500/1000;
temp =24*aaa;
Serial.print(" Should be 2.5 = "); Serial.print (aaa); Serial.print(" Should be 60 = "); Serial.println (temp);
}
You should use floating point constants, so the above code should be:-
void loop() {
aaa = 2500.0/1000.0;
temp =24.0*aaa;
Also note that floating point numbers are no longer integers so they only represent an approximation to the number. So instead of getting 60.0 you are likely to get 59.99999. That is why you should never use them with a == in an if statement.
so -> if(temp == 60.0) will often fail.
Sorry I found the error of "floating point constants" just after I had posted how the number I receive from TX is an integer and not a floating point.
I seem to have accidentally solved the problem by
temp = 2500*1.00;
aaa = temp/1000;
float aaa;
float temp ;
void setup() {
Serial.begin(9600);
}
void loop() {
// *******Integer received from TX is 2500 not 2500.00*********
// ******************* WHAT I GET from TX ******************************
temp = 2500 * 1.00;
aaa = temp/1000;
temp =24*aaa;
Serial.print(" Should be 2.5 = "); Serial.print (aaa); Serial.print(" Should be 60 = "); Serial.println (temp);
// ******************** WHAT WORKS *******
// aaa = 2500.00/1000;
//temp =24*aaa;
//Serial.print(" Should be 2.5 = "); Serial.print (aaa); Serial.print(" Should be 60 = "); Serial.println (temp);
}
Thank you again for your help
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.