Can't view Serial.print from setup()

this doesn't seem to be an issue with Arduino itself, but I'm having the following problem. I just started using an Arduino this week, and I'm familiar with c-based languages (PHP specifically) so this shouldn't be a coding problem.

I would like to print a line of code each time the Arduino starts. This is obviously easy:

void setup() {
  Serial.begin(9600);
  Serial.println("Hello World");
}

The problem is not whether this is actually happening (the TX light flashes once during startup so I know it's being sent), the problem is I can't see it happening for the following two reasons:

  1. I can't open the serial monitor until after the Arduino is physically connected to the computer, so I miss the first output (all output from the loop() shows up just fine)

  2. When using terminal as a screen screen /dev/tty.usbmodem621 then unplugging and replugging in the Arduino looses the original connection and nothing new is output, same result by simply pressing the reset button.

I'm just confused as to how I'm supposed to be able to see the first message sent during the setup() if I can't connect a screen before that function runs?

I'm running OSX Mountain Lion with an Arduino Micro.

1 Like

Since the Micro, like the Leonardo, uses software USB instead of a hardware USB-to-Serial adapter you need to add some code:

http://arduino.cc/en/Guide/ArduinoLeonardo

"This change means that if you're using any Serial print(), println() or write() statments in your setup, they won't show up when you open the serial monitor. To work around this, you can check to see if the serial port is open after calling Serial.begin():

   Serail.begin(9600);
   // while the serial stream is not open, do nothing:
   while (!Serial) ;
3 Likes

alyda21: I'm familiar with c-based languages (PHP specifically) so this shouldn't be a coding problem.

PHP isn't C-Based. PHP is its own language.

The Arduino is programmed with C++ and some nice libraries to make programming easier. It isn't C-based, it is C.

alyda21: 1. I can't open the serial monitor until after the Arduino is physically connected to the computer

In addition to the "serial wait" code suggested by John, keep in mind that each time your computer opens the serial port, it will cause the Arduino to reset. So opening the Serial monitor or running screen, causes a reset. It's called Auto-Reset.

Thanks johnwasser, your explanation and reference helped me to understand what was happening.

and James C4s, all I meant was that I am familiar with the code structure because of my background in PHP. I myself wondered why I was able to understand the code without knowing anything about Arduino itself, until I found this: http://en.wikipedia.org/wiki/List_of_C-based_programming_languages.

Also, you are incorrect about the Auto-Reset, please see the section titled No reset when you open the serial port on this page: http://arduino.cc/en/Guide/ArduinoLeonardoMicro

Thanks for your help.

1 Like

PHP isn't C-Based. PHP is its own language.

True, but the structure is very much like C, and many of the functions in PHP are mapped exactly to corresponding C functions.

Quote PHP isn't C-Based. PHP is its own language. True, but the structure is very much like C, and many of the functions in PHP are mapped exactly to corresponding C functions.

But both get ther block structure from ALGOL-60!

Mark

Does your micro have a reset button or reset pin? If so, have you tried resetting the micro after the computer/micro setup is complete?

Thanks johnwasser, saved me many more hours of frustration!