[SOLVED] Is it possible to put more than one instruction in Serial.print?

Is it possible to use a line like
Serial.println (inst1:" ":inst2:" ":inst3);
To get 3 variables on the same line?
I know I could do it with 5 lines but is it possible to do it with one and if so, how?

Which Arduino board? Several (ESP, Teensy, etc) have Serial.printf() enabled. If it's not on yours, you might try sprintf().

Serial.println ("The " "quick " "brown " "fox");

Is certainly possible.

Here is an example with snprintf() which you could use on say a Uno:

char buf[80] = {0} ;
. . .
. . .
snprintf( buf, sizeof(buf)-1, "%d  %d  %d \n" , inst1, inst2, inst3 ) ;  // %d assumes int data type
serial.print( buf ) ;
1 Like

On line is easy , just don’t use the return key :wink:

Serial.print (inst1); Serial.write(' '); Serial.print (inst3); Serial.write(' '); Serial.println (inst3); 

1 Like

Would Serial.printf just do the same as Serial.print?

I cant get it to work with three variables?

Thanks 6v6gt I put the char buf before the setup and it works perfectly.

thanks J_M_L I was looking for different command like the one above.

on which controller?
and WHY? What do you want to solve?

sprintf and printf were already mentioned. But also check "Streaming.h" on an Uno:

#include <Streaming.h>
HardwareSerial &cout = Serial;

void setup() {
  Serial.begin(115200);
  int inst1 = 1;
  int inst2 = 2;
  int inst3 = 3;
  cout << inst1 << ' ' << inst2 << ' ' << inst3 << endl;
  //cout << (F("\r\npoor mans cout\r\n"));   
}

void loop() {
}

That takes up even more room than my original lines.

your single line is not a full working program.
my example is a full working example.
and now start comparing the used program memory.

Program from my post #7

Sketch uses 1740 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.

Program build on snprintf

void setup() {
  Serial.begin(115200);
  int inst1 = 1;
  int inst2 = 2;
  int inst3 = 3;
  char buf[80] = {0};
  snprintf( buf, sizeof(buf) - 1, "%d  %d  %d \n" , inst1, inst2, inst3 ) ; // %d assumes int data type
  Serial.print( buf );
}

void loop() {
}

Sketch uses 3048 bytes (9%) of program storage space. Maximum is 32256 bytes.
Global variables use 198 bytes (9%) of dynamic memory, leaving 1850 bytes for local variables. Maximum is 2048 bytes.

that's why I asked you regarding the used controller and what you want to achieve.

No, it would make no sense to implement a new function that just the same thing as an existing one. Serial.printf() implements functionality similar standard printf() .

No. it is a real printf()
All the 3rd party cores have a printf() method in their Print class.
This allows a sketch to format output the same as using printf().
i.e. Serial.printf(...) works and outputs the same as a standard xxprintf() function.

And since it was added to the Print class it works with any class object that inherits the Print class, so you can use on serial ports, lcds, network interface etc...

The community has been asking Arduino.cc developers for around 15 years to add a printf() method to the Print class.
The powers in charge including some of the founders have over and over again refused. Often claiming that xxprintf() is too scary and complex for Arduino users.

Years ago there was an Arduino Playground area and there was a page for printf()
I added instructions for several alternatives of how to add printf() support to your Arduinio.cc core.
remnants of that are here:
https://playground.arduino.cc/Main/Printf/

So as of right now, if you use a 3rd party core like Teensy, ESP32, ESP8266, chipKit, etc... you get a printf() method in your Print class.
If you use an Arduino.cc core, you don't.

These days, when necessary, I use a function, Pprintf(), I created to add printf() support for a sketch that does not require modification of the platform core code.
It works like fprintf() but instead of passing in FILE pointer, you pass in a Print class object so it can work on any Arduino object that inherits the Print class.

Here is a link to see a forum post about it:
https://forum.arduino.cc/t/basic-printf-implementation/1106424/6?u=bperrybap

Another issue that will come up if using the Arduino.cc AVR platform core is that floats are not supported by the xxprintf() code "out of the box".
i.e. if you attempt to print floats, it will not work.
This can be a deal killer.
This is mainly due to the retarded way avr-gcc developers implemented the xxprintf() code.
It requires linking in different versions of the library to get different versions of the code. While this works, there were other ways this could be done.
I made some suggestions, but the avr-gcc cLib developers didn't want to change to a new way of doing things.

The default version of the xxxprintf() library is a bit smaller than the one that supports float formatting (like 1.5k).
Arduino.cc developers have over the years been asked to support being able to use the floating point xxprintf() support but so far have never added support to alter the linker settings from the IDE GUI to allow using floats, even with they have been given various working solutions through the years.
There are various ways to work around this limitation in the Arduino.cc AVR platform
from patching the platform file (which hard codes it for all boards in the core) or modifying the boards file.
With the newer versions of the IDE you can now create menus to set various compile and link options by making some updates to the boards.txt file.
This is more work than a small patch to the platform file but offers the most flexibilty as it allows the user to change the options from the IDE GUI on a per board, per build basis.

--- bill

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.