Creating a string of floats

Anyone know how to create a string of floats?

I want to change:

//  Serial.print(timeNow,0);
//  Serial.print("\t");
//  Serial.print(float1,3);
//  Serial.print("\t");
//  Serial.print(float2,2);
//  Serial.print("\t");
//  Serial.print(float3,2);
//  Serial.print("\t");
//  Serial.print(float4,2);
//  Serial.print("\t");
//  Serial.print(float5,2);
//  Serial.println("");

Into something like this:

  String dataString = "";
  
  dataString += (float1,DEC); // 3 decimal places
  dataString += "\t";
  dataString += (float2,DEC); // 2 decimal places
  dataString += "\t";
  dataString += (float3,DEC); // 2 decimal places
  dataString += "\t";
  dataString += (float4,DEC); // 2 decimal places
  dataString += "\t";
  dataString += (float1,DEC); // 2 decimal places
  dataString += "\t";
  dataString += (float5,DEC); // 2 decimal places
  dataString += "\n";
  
  Serial.print(dataString);

So the serial output is something like:

1.123 2.23 3.45 4.56 5.67

You can use sprintf to do that:

// make this string long enough to hold all the data
char dataString[128];
// need this to keep track of where the end of the string is;
char *sp;

  sp = dataString;
  // In the format spec, 5 is the total width of the printed number including sign and decimal point
  // 3 (or 2 etc.) is the number of decimal places
  sprintf(sp,"%5.3f\t",float1);
  sp = dataString + strlen(dataString);
  sprintf(sp,"%5.2f\t",float2);
  sp = dataString + strlen(dataString);
.....
  sprintf(sp,"%5.2f\n",float5);

Pete

Pete,

Thanks very much for your help. I've actually just managed to do it using dtostrf, which is apparently better than sprintf:

  dtostrf(float1,4,0,buffer);
  dataString += buffer;
  dataString += "\t";
  dtostrf(float2,4,3,buffer);
  dataString += buffer;
  dataString += "\t";
  dtostrf(float3,4,2,buffer);
  dataString += buffer;
  dataString += "\t";
  dtostrf(float4,4,2,buffer);
  dataString += buffer;
  dataString += "\t";
  dtostrf(float5,4,2,buffer);
  dataString += buffer;
  dataString += "\t";
  dtostrf(float6,4,2,buffer);
  dataString += buffer;
  dataString += "\n";

Serial.print(dataString);

Produces the output I want too.

Many thanks for your help anyway!

You can use sprintf to do that:

Actually, you can't. The %f format specifier is not supported on the Arduino.

I'm getting an interesting problem with this...

I'm running the code at 50Hz, after about 5-6 seconds one of the tabs vanishes.

Is this likely to be a memory problem, where I haven't defined the String size so it's contracting and expanding and problems arising because of this?

The %f format specifier is not supported on the Arduino.

ARGHHHHHHHHH.
Thanks

Pete

I'm running the code at 50Hz, after about 5-6 seconds one of the tabs vanishes.

Just the tab character, or the data after it too?

Is this likely to be a memory problem, where I haven't defined the String size so it's contracting and expanding and problems arising because of this?

Yes, and no. It is likely to be a memory problem. You can't define the size of a String. It dynamically changes size (and memory usage).

You should get rid of the String object, and use a fixed size char array, instead.

PaulS:
You should get rid of the String object, and use a fixed size char array, instead.

Agreed. I'd use a plain fixed size array, and look into PString to "print" to that string: PString | Arduiniana