Concatenating strings numbers in serial monitor

In case instead of dealing with C String formatting sprintf concat string and int or even char buffers.
Just do this if it just has to go on the same debug line:

      Serial.print(speed);
      Serial.print("m/s :: ");
      Serial.print(speed * 3.6);   
      Serial.println("km/h");

some print and a println at the end. "1.00m/s :: 3.60km/h"

I prefer using some helper methods for simple printing:

inline void mPrint(Stream&) {}

template <class H, class... Tail> void mPrint(Stream& stream, H const head, Tail const... tail) {
  stream.print(head);
  mPrint(stream, tail...);
}

template <class... Args> void mPrintln(Stream& stream, Args const... args) {
  mPrint(stream, args..., "\r\n");
}

Usage:

mPrintln(Serial, 1, ':', 2, ':', 3, '.', 99);  // Saves six lines of code.

awsome, I will include the template in my lib, thank you

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