How to Send float value from arduino to redbear shield

Hi Team,

unsigned char buf[100] = {0};
unsigned char len = 0;

float value = 1.117;

len = sprintf(buff, “%d”, value);
ble_write_bytes(buff, len);

Expected output: 1.117
Actual output = 1

How to get 1.117 as output?

The normal Arduino IDE does not support the use of floats in sprintf()

In any case, is %d the format specifier for a float ?

One way is to use the dtostrf() function to convert the float to a string and use %s parameter in the sprintf().

The snprintf() function is safer to use as it protects from inadvertent buffer overruns.

groundFungus:
One way is to use the dtostrf() function to convert the float to a string and use %s parameter in the sprintf().

The snprintf() function is safer to use as it protects from inadvertent buffer overruns.

Hi groundFungus,

Thanks, It's working.

dtostrf(value, 4, 4, strA); //dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

ble_write_bytes((unsigned char *)strA, strlen(strA) + 1);