Row of square fill characters on serial monitor output

I am learning Embedded C via tutorials. Current topic is IF and ELSE IF statements. Sketch is below. (Can I attach a screen shot here? If so how?) Most is commented out. At the bottom is the Serial.println text "Y is from 51 to 100". This displays as expected. However often it is pushed off my screen to the right preceded by an entire row of square filler characters. I don't see any syntax errors. If I press reset un my UNU R3 it goes away initially but then returns in subsequent executions? I am running W10. Processor AMD A6-4400M APU with Radeon(tm) HD Graphics, 2700 Mhz, 2 Core(s), 2 Logical Processor(s). It is an older machine (HP) but otherwise works fine. Could it be contention for the port COM4?

int x = 51;
int y = 53;
void setup() {
  // THERE IS NO "THEN" STATEMENT. ONLY THE NEXT STATEMENT OR GROUP OF STATEMENTS FOLLOWING THE "IF" IS EXECUTED
  // FOLLOWED BY THE NEXT LINE AFTER THE IF OR GROUP.  
  Serial.begin(9600);
  /*
  if (x == 50) {
    Serial.println("The value is 50");
    Serial.println("This is a 2nd line in the IF group.");
    Serial.println("****");
  } else {
    Serial.println("The value is NOT 50");
    Serial.println("This is a 2nd line in the ELSE group."); 
    Serial.println("////");
  }       
  Serial.println("Back to sequential outside the IF / ELSE group.");
 */
  // YOU CAN ALSO HAVE NESTED IFs VIA THE "ELSE IF" STATEMENT AS FOLLOWS:
if (y > 100)
    Serial.println("Y is over 100");
else if (y > 50)
    Serial.println("Y is from 51 to 100");
else if (y > 25)
    Serial.println("Y is from 26 to 50");
else    
    Serial.println("Y is 25 or less");
    
   Serial.println("+++ End of Sketch using SETUP only +++");
} 

void loop() {
}

That happens to me too, and you probably just have to live with it.

Random unprintable characters (or zero bytes) somehow end up in the input buffer during startup, before the serial connection is finalized. It happens with a terminal program too, so it is not a fault with the serial monitor.

Start your program with

Serial.begin(rate);
while (!Serial);
Serial.println(); //push the junk up a line

Could be a baud rate issue. Is the baud rate set correctly in the serial monitor?

Try adding 'delay(200);' after 'Serial.begin(9600);'. That will keep the sketch from sending text between the time it starts after an upload and the time it re-starts when Serial Monitor connects.

Thanks. Will give each response a try to see what works & let you know.

I keep seeing that 9600 is standard for my Uno. Thanks

What does that mean? Your code needs to match what is in the monitor.

The standard Baud rate 115200 wastes much less time printing. Just make sure that the rate you use matches the rate set on the serial monitor (or terminal program).

Thanks to all: jremington (3 lines of code), red_car (match baud rate), johnwasser (delay). I have been away from this for a while. At time of replies I didn't know what matching baud rate meant once I set it in my code. Subsequently I discovered how to set it for the serial monitor. The DELAY statement alone seemed to have no effect but matching the rate settings and the 3 lines of code (//push the junk up a line) seem to have nailed it. Now can anyone tell me how to include this code in the default starter sketch (void setup and void loop) every time I begin a NEW sketch) . Not sure where to find it or if I can update it.
Thanks again.
Brewsterblock

Hi @brewsterblock. Please tell me which version of Arduino IDE you are using. That will allow me to provide you with the correct instructions for customizing the code of the sketch created via File > New in Arduino IDE.

Hi ptillisch. Should sign in here more often. Mine is IDE 2.0.2. Thanks

OK, here are the instructions:

  1. Create a sketch that contains the content you want Arduino IDE to use for sketches created via File > New Sketch.
  2. Save the sketch to any convenient location.
  3. Press the Ctrl+Shift+P keyboard shortcut (Command+Shift+P for macOS users) to open the "Command Palette".
  4. Select the "Preferences: Open Settings (UI)" command from the menu.
  5. A "Preferences" tab will now open in the IDE. In the "Search Settings" field, type arduino.sketch.inoBlueprint
  6. Add the path to the file containing your custom new sketch content in the field under the "Sketch: Ino Blueprint" setting.
  7. Click the X icon on the "Preferences" tab.
  8. Select File > Quit from the Arduino IDE menus.
  9. Start the Arduino IDE.

Please let me know if you have any questions or problems while following those instructions.

Thanks ptillisch. Will try this and get back to you.

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