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?

7
  • Aren't you missing a semicolon?
    – TheDoctor
    Mar 20, 2014 at 22:43
  • and one says vnnt while another says vannut
    – TheDoctor
    Mar 20, 2014 at 22:45
  • Sorry some typo's :)
    – stUrb
    Mar 20, 2014 at 22:46
  • Do you know the datatype of GPS.latitude? E.g. is it a float, int, etc.? Mar 20, 2014 at 22:50
  • I think it's a float? It says float in the error-message. How can I find out? Is there a var_dump or something?
    – stUrb
    Mar 20, 2014 at 22:52

1 Answer 1

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);
4
  • 1
    +1 when in doubt, sprintf it out :)
    – Madivad
    Mar 20, 2014 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, 2014 at 7: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, 2014 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, 2014 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.