Binary-coded decimal

Hello how do I correctly convert a double value into my byte array? I would like to use BCD for this. Thanks for your support!

float temperature = 24.12;
float humidity = 60.12;
byte message[5];

void setup() {
  Serial.begin(9600);
  message[0] = 12; //sensor id 12
}

void loop() {
  float temperature1 = floor(temperature);
  float temperature2 = (temperature - temperature1) * 100;
  Serial.println(temperature1, 0);
  Serial.println(temperature2, 0);  
  message[1] //?? temperature1
  message[2] //?? temperature2

  float humidity1 = floor(humidity);
  float humidity2 = (humidity - humidity1) * 100;
  message[3] //?? humidity1
  message[4] //?? humidity2
}

Search the forum for BCD.

There was a thread a few weeks ago.

this variables should be int, not float

and after that convert it to BCD, assuming you mean a 4 bits per digit:

message[1]  = ((temperature1/10) <<4) | (temperature1 %10);

Take the conversion of temperature2 as your homework;

Do you want byte packed BCD, or not? Why BCD? What is the requirement for it?

Perhaps 4 BITS per digit????

You say double but code has float. Humidity can't normally be measured to the 1/100 of a percent. So it's a waste to encode it that way.

oops...
corrected

I use lora for data transfer to another module and therefore need as small a package as possible

Yes, you're right, a two-digit number without decimal places would probably be sufficient here

Then why not send it in binary?

Then why a floating point type at all? A byte would be more than sufficient.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.