Serial New Line Printing

I'm trying to write code that every time there is information being printed to the serial monitor, a new line get printed by the end of the loop. The issue with my code is that sometimes that loop will go through and print no new information. If this is the case, I don't want the serial monitor to print a new line.

Is there a way to only Serial.println(); when there is already information in that line of the serial monitor.

I have attached example code below to show what I mean.

//Depending on what I need to see; some, all, or no "defines" will be uncommented.
//In this case only one define in uncommented
#define Hello_OUT
//#define INFO_OUT
#define RADIO_OUT

int count = 0;

void setup() {
  Serial.begin(115200);
  Serial.print("Start"); Serial.println();
}

void loop() {
  
  #ifdef Hello_OUT
  if ( (count % 3) == 0) {
  Serial.print("Hello");
  }
  #endif

  #ifdef INFO_OUT
  if ( (count % 5) == 0) {
  Serial.print(" INFO");
  }
  #endif

  #ifdef RADIO_OUT
  if ( (count % 7) == 0) {
  Serial.print(" Radio ");
  }
  #endif
  
  Serial.println();
  count = count +1;
  delay(100);
}

you could have a boolean that you set when you print data

void loop() {
boolean print=false;  // fixed it!
  #ifdef Hello_OUT
  if ( (count % 3) == 0) {
  Serial.print("Hello"); print=true;
  }
  #endif
  if(print)Serial.println();
  count = count +1;
  delay(100);
}

Aha - the old 'flase' condition...!

Serial.print("Hello"); print=true;

But please don’t copy the habit of putting multiple statements on the same line :confused: Except in a for-loop, NOTHING follows a ;

And if you want to make it easier to do, aka, reduce the chance of forgetting to setting/reset the flag, make functions:

bool newLineFlag;

void setup() {
  Serial.begin(115200);
  
}

void loop() {
  if(random(0,2)){
    debugPrint("yep");
  }
  else{
    Serial.print("nope");
  }
  debugNewLine();
  delay(1000);
}

template <typename T> inline size_t debugPrint(T in, int num){
  newLineFlag = true;
  return Serial.print(in, num);
}

template <typename T> inline size_t debugPrint(T in){
  newLineFlag = true;
  return Serial.print(in);
}

template <typename T> inline size_t debugPrintln(T in, int num){
  newLineFlag = true;
  return Serial.println(in, num);
}

template <typename T> inline size_t debugPrintln(T in){
  newLineFlag = true;
  return Serial.println(in);
}

void debugNewLine(){
  if(newLineFlag){
    Serial.println();
  }
  newLineFlag = false;
}

Here I do use Serial but for it to work you should not. Now it’s just used to distinguish between a loop with printing (via debugPrint()) or loops without (in which I now misuse Serial.print()).