I am having a very tough time converting float to string.
I am getting some data from GPS. The coordinates are in float.
I want something like
String gpsString;
//Imma gonna call this function in the loop
void checkGPS() {
//Assuming we already have the coordinates
float flat, flong.
//I want to do something like this
gpsString =""; //make sure the string is empty if its not
gpsString += flat;
gpsString += " ";
gpsString += flong.
Serial.println(gpsString);
}
I have tried a couple of things like without success. Perhaps I did it wrongly, but I could
A String is an object supported by the String library. Using them is likely to fragment memory usage which with the limited resources available on the Arduino can cause problems.
A string is an array of chars terminated by a null. Using them does not fragment memory because they are not continually created and destroyed.
To add to the fun the OP has tried to use a String with the sprintf() function sprintf(gpsString, "Lat: %f", flat);That won't work because the sprintf() function expects a char array as its target.
To add to the fun the Arduino implementation of sprintf() does not support the floating point format (%f)
So, what to do ?
I suppose the first question is why do you want to convert the float in the first place ?
econjack:
I have to agree with ULHeliBob; avoid the String class like the plague. There are probably a bazillion ways to solve your problem, and one might be:
void setup() {
int i;
float val;
char buff[10];
Serial.begin(115200);
val = 0.0;
for (i = 0; i < 10; i++) {
snprintf (buff, sizeof(buff), "%f", val);
Serial.println(val);
val += 5.0;
}
}
void loop() {}
Note you can change the format specifier (i.e., the "%f") if you need different decimal values.
Hi do I print that?
I tried "Serial.println(buff)", it prints a '?'.
Then, I also tried looping it to print out each character stored,
void setup() {
int i;
float val;
char buff[10];
Serial.begin(115200);
val = 0.0;
for (i = 0; i < 10; i++) {
snprintf (buff, sizeof(buff), "%f", val);
Serial.print("val: ");
Serial.println(val);
val += 5.0;
}
Serial.println("Contents of Buffer: ");
for (int a=0; a<10; a++) {
Serial.println(buff[a]);
}
}
void loop() {
}
Do you have the complete coding to convert the gps data? ive been trying to figure out how to do this. Im sending the coordinates through LoRa but it seems impossible for me to do it using floats, but its easy enough to send strings data through LoRa. It would be great if you could help me out.
You say this is solved but I wouldn't be surprised to see you back here with your program randomly crashing because you ignored the advice to avoid the String class (capital S) in favour of a C-style string.
dtostrf(val, 4, 6, buff); //4 is mininum width, 6 is precision
How do you reasonably expect to print at least one digit before the decimal point, a decimal point, and 6 digits after the decimal place, in a field of width 4?
Yes, I know that it is 4 or more, but your values just look dumb.
dtostrf(val, 4, 6, buff); //4 is mininum width, 6 is precision
How do you reasonably expect to print at least one digit before the decimal point, a decimal point, and 6 digits after the decimal place, in a field of width 4?
Yes, I know that it is 4 or more, but your values just look dumb.