How to print multiple variables in neat columns? [solved]

Hello,

I am trying to display 3 variables in the Arduino IDE 1.8.10 Serial Monitor in neat columns but these values can change in terms of their number of digits so it breaks the columns.

There are two issues that i'm trying to fix with my output:

  1. There is no tab space displayed after the 'ax' variable value. Is that a bug?
  2. Especially for the first variable, it can change from 2 to 5 digits (including the negative sign), and this breaks the columns. Is there any possible fix?

Code snippet corresponding to the serial data display:

Serial.print("ax: ");
Serial.print(ax);
Serial.print("\t");
Serial.print("ay: ");
Serial.print(ay);
Serial.print("\t");
Serial.print("az: ");
Serial.print(az);
Serial.print("\t");
Serial.print("Tilt angle: ");
Serial.println(tiltAngle);
delay(400);

Sample output from Serial Monitor:

ax: -20 ay: -1288 az: 18688 Tilt angle: 3.94
ax: 40 ay: -1376 az: 18776 Tilt angle: 4.19
ax: -56 ay: -1500 az: 18872 Tilt angle: 4.54
ax: -72 ay: -1368 az: 18708 Tilt angle: 4.18
ax: -144 ay: -1384 az: 18728 Tilt angle: 4.23
ax: 8 ay: -1444 az: 18692 Tilt angle: 4.42
ax: -4 ay: -1312 az: 18716 Tilt angle: 4.01
ax: -100 ay: -1264 az: 18752 Tilt angle: 3.86
ax: 20 ay: -1364 az: 18776 Tilt angle: 4.16
ax: 48 ay: -1412 az: 18864 Tilt angle: 4.28
ax: 0 ay: -1464 az: 18816 Tilt angle: 4.45
ax: 0 ay: -1372 az: 18832 Tilt angle: 4.17

EDIT: I tinkered around with the serial print code and tried inserting 3 spaces before the tab and it fixed the display with no more broken columns. I don't really understand why but i'm guessing there is a bug with the tab command??

Serial.print("ax: ");
 Serial.print(ax);
 Serial.print("   ");
 Serial.print("\t");
 Serial.print("ay: ");
 Serial.print(ay);
 Serial.print("   ");
 Serial.print("\t");
 Serial.print("az: ");
 Serial.print(az);
 Serial.print("   ");
 Serial.print("\t");
 Serial.print("Tilt angle: ");
 Serial.println(tiltAngle);
 delay(400);

New output:

ax: 172   	ay: -8796   	az: 16344   	Tilt angle: 28.29
ax: -68   	ay: -8784   	az: 16296   	Tilt angle: 28.33
ax: 48   	ay: -408   	az: 18804   	Tilt angle: 1.24
ax: 16   	ay: 4   	az: 18804   	Tilt angle: -0.01
ax: -120   	ay: 156   	az: 19024   	Tilt angle: -0.47
ax: -252   	ay: 156   	az: 18904   	Tilt angle: -0.47
ax: 28   	ay: 216   	az: 18908   	Tilt angle: -0.65
ax: 0   	ay: 612   	az: 18648   	Tilt angle: -1.88
ax: 20   	ay: -1144   	az: 18860   	Tilt angle: 3.47
ax: 104   	ay: -996   	az: 18936   	Tilt angle: 3.01
ax: 92   	ay: -1116   	az: 18808   	Tilt angle: 3.40
ax: 156   	ay: -1164   	az: 18984   	Tilt angle: 3.51

Hi,

is this to do with the way that text is 'tabbed' on the screen, I've seen problems like this in simple notepad files?

Adding three spaces pushes everything to the 'second next' tab position along the line and it all suddenly lines up?

Peter

Hi again,

Just thinking about further lining up…

and apologies as I’m not too familiar with ‘C++’ yet, but in basic I’d use something like:

mystring = " " & trim(str(number))

mystring = right(mystring, 5)

ie: adding four or five spaces at the start of the ‘string of the number’…
then taking the five (or whatever) characters from the right hand side and sending that to the monitor.

It should line everything up on the right of each column.

Probably using char and a for loop here, but I’d need to poke around with that for a while to see if I could make it work.

You guys would likely do it in seconds!

Peter

A very efficent way is to use the sprintf():

First declare a print buffer at the beginning of the sketch
char prBuffer[32]; // Print buffer

Then use the sequence to print your line:

sprintf(prBuffer, "ax:%5d ay%5d az:5d Tilt angle: ",ax, ay,az);
Serial.print(prBuffer);
Serial.println(tiltAngle);

titlAngle must be printed separately, because sprintf() does not see float or double.