7

new to arduino I'm struggling with what sounds like fairly n00b problem... I've wired up a adafruit GPS-board to my Arduino and it is working as it spits out GPS data to the serial port with Serial.print(GPS.latitude, DEC)

I Now want to concat a string which I can process (read: I want to sent it via an ethernet client.) This is what I got already:

......
String vnnt = "$VNNT,";

if (GPS.fix) {
   vnnt += "GPS,";

   //this works:
   vnnt.concat(GPS.fix);

   //but this not:
   vnnt.concat(GPS.latitude);

}else{
   vnnt += "INFO,Acquiring Sats";
}

Serial.println(vnnt);

The error message is: Call of overloaded 'concat(float&)' is ambiguous When I Serial.print(GPS.latitude, DEC) it results in: 4418.5937996050

So it is probably to big or something...

How can I concat the vars and create the long string?

stUrb
  • 341
  • 2
  • 5
  • 9

1 Answers1

7

The concat function does not implement a float version, but some of char, int, unsigneds...

unsigned char String::concat(long unsigned int)
unsigned char String::concat(unsigned int)
unsigned char String::concat(int)
...

so the compiler does not know how to cast (truncating probably the float) to integer, as there are several options.

You have to convert first your float to string, use dtostrf() or sprintf() then concat to your string.

char outstr[25];
sprintf(outstr, "%f", GPS.latitude);

or

dtostrf(GPS.latitude, 6, 2, outstr);  //check docs for 6, 2 values, and set them for your needs

And then:

vnnt.concat(outstr)

Also note that sprintf is very handy for compact creation of a (char) string:

char msg[80];
sprintf(msg, "$VNNT,GPS, %s %f", GPS.fix, GPS.latitude);
drodri
  • 1,404
  • 11
  • 12
  • 1
    +1 when in doubt, `sprintf` it out :) – Madivad Mar 20 '14 at 23:55
  • 1
    One point of caution though: `sprintf()` is a complex function, hence it is rather slow and uses quite some memory. Generally, I would avoid it for "production" code (only keep for debug purposes). – jfpoilpret Mar 22 '14 at 07:13
  • That is a good point @jfpoilpret, I didnt take into account. Do you have some order of magnitude of efficiency vs a dtostrf+concat version? Does the same apply to sscanf()? (btw, I now doubt if sscanf() actually works fine in arduino) – drodri Mar 23 '14 at 22:31
  • I did not perform actual measurements to compare both calls, but I guess one could just take a look at the C code and possibly check the generated assembly. Regarding `sscanf`, IIRC it has some limitations (with floating point numbers I think). – jfpoilpret Mar 23 '14 at 23:14