problems with changing float to char array dtostrf()

hi.

im having trouble changing float to char array

im currently trying to use SPI to transfer multiple float values from slave to master

right now my code gets float values from multiple sensors and changes it into char arrays wich can be sent to the master easily.

i recieve temperature, pressure, humidity, and luminosity data from sensors

when i attempt to change these values into char array, the first variable works fine(temperature)

but after that, the dtostrf() function (the one that changes float to char) doesn’t do its job

i thought it might be because of overflow or something so i increased the char length but that did nothing.

i tried putting flags everywhere so im pretty sure dtostrf() function is where everything went wrong.

so my questions are :

is there a problem i am not seeing?

is there a problem with dtostrf()?

is there a way to change float to char without using dtostrf()?

if none of these,

is there another way to send multiple float values from slave to master using SPI?

i

this is part of my code that gives me headaches

float temp = 0.0;
float temp_read = 0.0;
char tstr[6];

float pres = 0.0;
float pres_read = 0.0;
char pstr[20];

float hum = 0.0;
int hum_read = 0;
char hstr[7];

float irr = 0.0;
int irr_read = 0;
char istr[9] =;
double amp_ratio = 23.0;

temp_read += bmp.readTemperature(); //get value from sensor
temp = temp_read/tt; //average
dtostrf(temp, 6, 2, tstr); //change to char
delay(100);

pres_read += bmp.readPressure();
pres = pres_read/pt;

Serial.println(pres);

dtostrf(pres, 15, 2, pstr);
delay(100);

hum_read += analogRead(hum_pin);
hum = hum_read/ht;
hum = (hum5.0/1024.0 - 0.6)/2.2575.0 + 20.0;
dtostrf(hum, 7 , 2, hstr);
delay(100);

irr_read += analogRead(irr_pin);
irr = irr_read/it;
irr = (5.0/amp_ratio)1000.05.0/1024.0*(irr);
dtostrf(irr, 9, 2, istr);

Serial.print("t = ");
Serial.println((char*)tstr);
Serial.print("p = ");
Serial.println((char*)pstr);
Serial.print("h = ");
Serial.println((char*)hstr);
Serial.print("i = ");
Serial.println((char*)istr);

and i get this

t = 25.01
p =
h =
i = 0.00

also i’ve attached my whole code if you need them

energy_monitoring_0320_slave.ino (4.6 KB)

energy_monitoring_0320_master.ino (3.88 KB)

p l e a s e u s e [ c o d e ] t a g s

is there a problem with dtostrf()?

No there is no problem with dtostrf()

is there a way to change float to char without using dtostrf()?

Yes, e.g. check - Arduino/libraries/PrintString at master · RobTillaart/Arduino · GitHub

jonghaboy:
im having trouble changing float to char array
im currently trying to use SPI to transfer multiple float values from slave to master

Do the master and slave have the same format for float variables (size and endianness)? If so, why bother converting to ASCII? Just send them as binary.

You can do that even if the float formats are different between the two processors -- it just takes a little more thinking.

gfvalvo:
Do the master and slave have the same format for float variables (size and endianness)? If so, why bother converting to ASCII? Just send them as binary.

You can do that even if the float formats are different between the two processors -- it just takes a little more thinking.

Even if the binary format differs, as long are both IEEE754 format you only need a byte shuffle.

char istr[9] =;Didn't the compiler complain?

Your buffers are not large enough to hold the terminating null character:

char tstr[6];
dtostrf(temp, 6, 2, tstr); // need 6+1

char hstr[7];
dtostrf(hum, 7 , 2, hstr); // need 7+1

char istr[9];
dtostrf(irr, 9, 2, istr);  // need 9+1

Remember also that the width parameter is the minimum width and that the result may be longer if the integer part of the value is large.