What is the state-of-the-art way to Serial.print 5 floats together with descript

Hi,

I just found out that on an Arduino sprintf() does not work with floats.
What is the state-of-the-art way to Serial.print e.g. 5 floats together with their descriptions?

Regards

dtostrf()

Serial.print

streaming

e.g.

Serial << F("green: ") << float1 << F("red: ") << float12 << endl;

aarg:
streaming

e.g.

Serial << F("green: ") << float1 << F("red: ") << float12 << endl;

Could you please elaborate?
Your code does not compile, what are the preconditions?

blh64:
dtostrf()

That is really ugly. Then i'd prefer the dumb way with Serial.print().

RIN67630:
That is really ugly. Then i'd prefer the dumb way with Serial.print().

Beauty is in the eye of the beholder. Rather than focusing on "elegant" or "ugly" or "dumb", maybe you should focus on "works" vs. "doesn't work" I find that a much better criteria

dtostrf() is useful when you need all the text in a single c-string, otherwise individual .print() statements work well.

RIN67630:
Could you please elaborate?
Your code does not compile, what are the preconditions?

#include <Streaming.h>

This has been the “modern” way to output in C++ for a long time now. Look at any C textbook I/O examples, it’s the de facto standard. Because of that, it’s also easy to find usage information and examples. I find Serial.print() clumsy for anything more than a handful of prints. Edit - however this Arduino Streaming library uses the print functions to operate.

The only problem comes when you use a print function that takes an extra parameter, like HEX, or the number of digits specification for float printing. Then you have to use print().

So… The Arduino IDE now supports binary libraries, right?
Can we implement adding floating point printf/sprintf/etc just by wrapping a Arduino Library “shell” around the existing libprintf_flt.a library? “user” libraries are searched before the system libraries, right?

Meanwhile, how about:

#define pfltval(s, v, eol)			\
    do {					\
	if (s)					\
	    Serial.print(s);			\
	else					\
	    Serial.print(  #v " = ");		\
	Serial.print(v);			\
	if (eol)				\
	    Serial.print(eol);			\
    } while (0)

float myvariable = 4 * PI;
void setup ()
{
  Serial.begin(9600);
  while (!Serial)
    ;
  pfltval("Pi =", PI, ",");
  pfltval(" and 2*Pi = ", 2 * PI, "\n");
  pfltval(0, myvariable, "\n");
}
void loop() {}

Produces output like:

Pi =3.14, and 2*Pi = 6.28
myvariable = 12.57

Note that source code that the macro creates like:

 if (0)
  Serial.print("stuff");
//and 
if ("this is a string")
  Serial.print("this is a string")

Compiles to the same code as if the “if” parts were not there, since the tests can be done at compile time.
This is probably significantly smaller than the printf-based code.

blh64:
Beauty is in the eye of the beholder. Rather than focusing on "elegant" or "ugly" or "dumb", maybe you should focus on "works" vs. "doesn't work" I find that a much better criteria

Both methods do work.

I meant, with dtostrf() encapsulating all floats and sprintf to a charbuff and finally Serial.print(charbuff), you get an extremely clumsy code, which is no way smaller than using separate Serial.print() instructions.
I merely looks like obfuscating what you really intend to do.

So i'd prefer using the much more readable traditional way.