first serial message from arduino after startup contains newline

the first message sent from the arduino to the computer (after the arduino has started up) using Serial.println() or Serial.print contains a newline at the beginning of the message.
for example: when i send the string connected #! to the arduino, it will usually reply with connected 1 # but the first response from the arduino after starting up (all other messages are fine) contains a newline at the beginning of the message:

connected 1 #

the newline consists of two bytes, HEX values 0D 0A.

i know there is a function in one of the libraries (Adafruit MotorShield library) that sends a message, however this function is called (multiple times) before Serial.begin(), no part of the message this function usually sends is sent to the pc.
could this be the source of the problem? if yes, is it possible to prevent the leading newline from being sent (without altering the library)?

Hi wergor

Please post your complete program. Ideally, if you can cut the code down to a test version that still has the problem, that would be great.

Also, can you give a link to the library you are using.

Are you using the Arduino serial monitor or another terminal program?

Regards

Ray

Hi,

i'm using the Adafruit Motor Shield v2 library.
i have narrowed the problem down to the call of the setPWMFreq() function in the file Adafruit_PWMServoDriver.cpp which is called by the function begin() in the Adafruit_MotorShield.cpp file. Serial.print and Serial.println are both called 2x in this function, commenting out these lines seems to solve the problem.

wergor:
i have narrowed the problem down to the call of the setPWMFreq() function in the file Adafruit_PWMServoDriver.cpp which is called by the function begin() in the Adafruit_MotorShield.cpp file. Serial.print and Serial.println are both called 2x in this function, commenting out these lines seems to solve the problem.

That's particularly bad form. Library files shouldn't be writing to Serial with such a cavalier attitude.

i assume the Serial output was just overlooked when preparing the library for release to the public, as this file contains a number of other commented out calls to Serial.print() and Serial.println().

Have you tried using Serial.flush() right after Serial.begin?

Have you tried using Serial.flush() right after Serial.begin?

i did, but it didn't help. Serial.flush() - Arduino Reference

@hackscribble i didn't post my code earlier because my program has several thousand lines of code already, breaking it down would just take too long (at least longer than fixing the library).

wergor:
@hackscribble i didn't post my code earlier because my program has several thousand lines of code already, breaking it down would just take too long (at least longer than fixing the library).

Fair enough :slight_smile:

You didn't read the reference page for Serial.flush(). It waits for the tx buffer to empty in IDE versions 1.0.x. It does not empty the rx buffer. Try this:

Serial.begin(9600);
delay(100);
while(Serial.available()) Serial.read();

edit: Added a delay to allow time for the Serial buffer to get any characters from the sending device.