Printing to serial monitor when uploading

The test sketch shown appears to have two flaws in the way it displays printed output on the serial monitor:

  1. The first line of output is printed as if it's right justified, i.e. on the far right of the window.

  2. When uploaded, it does not clear all previous output.

The first problem seems to be resolved by adding a 200 ms delay after the Serial.begin command.

But is that upload behaviour correct?

I usually run a sketch with Ctrl+u as a general rule, not just after a code edit. It's easier than pressing the UNO button, especially with many connector wires to a breadboard. It also catches any accidental code or circuit change I might have made.

Is there some simple fix I've missed? Otherwise guess I'll add 'Clear output' to my workflow.

// Some arbitrary constants
const int onPin = 9;
const int tOn = 100;
const int tOff = 900;
const int analogPin = A1;

// ... and variables
int period = tOn + tOff; // ms
int f = 1000 / period; // Frequency (Hz)

void setup() //
{
  Serial.begin(9600);
//  delay(200);
  Serial.println("Test_Serial_Monitor_Printing");
  Serial.print(F("tOn = "));
  Serial.println(tOn);
  Serial.print("tOff = ");
  Serial.println(tOff);
  Serial.print(F("period = "));
  Serial.println(period);
  Serial.print(F("f = "));
  Serial.print(f);
  Serial.println(F(" Hz"));
}

void loop()
{
}

// The sketch above displayed the following monitor output
// the first time it was uploaded.
// EDIT: Not sure why but cannot paste the rest of my comments. Will try again.
 

Gave up. Must be corrupted text. Here's an image instead.

That is normal. Click on the "Clear output" button at the bottom of the Serial Monitor to clear the previous output.

1 Like

Thanks, understood, that’s the habit I’ll develop then.

Any thought about the ‘first line’ flaw, and the reliability of my work-around?

I approve. I always put a 200mS delay after Serial.begin().

Thanks, appreciate the fast replies.

Sometimes, a previous sketch will have left things in the PC's serial buffers, AFTER the serial monitor has been disconnected for the upload, but before the upload actually occurs. Then this (old) data is displayed when the monitor is re-activated for the new sketch.

1 Like

Thanks. A nuisance, but happily the work around is simple enough.

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