Serial Print One Line Arduino Multi Data

How to print once in Arduino?

Assalamualaikum. In debugging, we will usually print various characters or text to the serial monitor, in the form of plain text or variables whose values ​​will change every time. In this article, I will share how to print multiple data once to Serial With One Line Arduino.

By default, if you are new to the Arduino IDE, to print data on the Serial Monitor, we will create it line by line, as in the following code:

//by arduinokode.blogspot.com

bool   a = true;
int    b = 10;
float  c = 20.34;
char   d = '%';
String e = "Final";

void setup() {
  Serial.begin(9600);

  Serial.print("A=");
  Serial.print(a);
  Serial.print(" B=");
  Serial.print(b);
  Serial.print(" C=");
  Serial.print(c);
  Serial.print(" D=");
  Serial.print(d);
  Serial.print(" E=");
  Serial.print(e);
  Serial.println();
}

void loop() {
  // put your main code here, to run repeatedly:
}

So the above code will give the following result:

Serial Print One Line Arduino Multi Variable

Now, imagine you want to print a lot of data to the serial monitor, of course, there will be a lot of Serial.print() lines that we will create instead.

There are 2 methods to do that based on my experience which convert all data to string or use printf.

Method 1: Use String

This is the easiest and simplest way that you can use. In order to display all variables with different data types in one line, we must convert all data types to strings.

You do this by adding “(String)” before the variable in Serial.print. The writing format is shown in the following Arduino code:

//by arduinokode.blogspot.com

bool   a = true;
int    b = 10;
float  c = 20.34;
char   d = '%';
String e = "Final";

void setup() {
  Serial.begin(9600);
  Serial.println((String)"A=" + a + " B=" + b + " C=" + c + " D=" + d + " E=" + e);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Then the result is the same as in the image above.

Method 2: Using printf

In addition to the above method, one of the commands from C++ that we can use is printf.

It cannot be used on Arduino Uno, Mega, or Nano boards. If it compiles it will produce an error:

class HardwareSerial’ has no member named ‘printf

However, it works fine for ESP8266, NodeMCU, or any other ESP. An example of a single line Arduino serial monitor code using printf is as follows:

//by arduinokode.blogspot.com

bool   a = true;
int    b = 10;
float  c = 20.34;
char   d = '%';
String e = "Final";

void setup() {
  Serial.begin(9600);
  Serial.printf("A=%d B=%d C=%f D=%c E=%s \n",a,b,c,d,e);
}

void loop() {
  // put your main code here, to run repeatedly:
}

The result is the same as in the image above. To learn more about using printf, see the article format string printf.

Hopefully, this article is useful for you.

Read more:
> Display Time Every Second Arduino RTC
> gpg: keyserver receive failed: General error Linux
> Pass Audio Mic to Headphone (Speaker) PulseAudio

Similar Posts