Code first start running after I open serial monitor

Hi all

I having a little problem in understanding how the console read function is working on the Arduino Yun. My problem started with the tutorial of consoleRead. https://www.arduino.cc/en/Tutorial/ConsoleRead

The code run fine and the Arduino reads all in the console lines, but it is seem that the code first start running after I open the serial monitor. Is there a way to start the program automatic by just give the arduino power, as the arduino normal do.

Hi, Arduino does start running after you apply power, when you open the serial monitor the arduino is reset, so the code restarts.

Tom... :)

Hi Tom

Thanks for reply.

Yes I know, that the cool thing with the arduino but if I using the function Console.begin() will my code first start running after I have started the serial monitor. Do you know why?

Best Peter

Limited knowledge here !!

This is basically the case with all 32U4 based boards. A connection needs to be established before the while(!Console) will end.

Hi, Sorry I have no experience with YUN. You might be better putting YUN in the subject line of this thread.

Tom.. :)

Hi, again. There is a YUN section on this forum, you may need to ask there.

Tom... :)

sterretje has the right idea: early on in setup() is this line:

  // Wait for Console port to connect
  while (!Console);

This line waits here until a connection is made to the Console object, in other words, until a serial monitor is opened. The sketch is indeed starting up as soon as power is applied, but is spinning here doing nothing until that connection is made.

This line looks very cryptic, so it's interesting to break it apart to see what's happening. This is a while loop all in one line. Console is an object, which has a custom function defined to convert it to a Boolean: it converts to true if a connection has been made, or false if no connection. The "!Console" part of the line makes this determination and inverts it: it's true as long as there is no connection. The while loop will spin as long as this is true - as long as there is no connection. Because there is a semicolon right after it, this is a null loop that does nothing but keep checking whether there is a connection. As soon as there is a connection, the conversion of Console to a Boolean returns true, the "!" negates that to false, and the while loop drops out - the rest of the sketch now runs.

I said the sketch will start as soon as power is applied - this is true, but the Bridge.begin() call needs to establish a connection with the Linux side of the Yun, and it takes about a minute for Linux to boot up. So, in reality, while the sketch is running as soon as power is applied, it will take a minute to get past the Bridge.begin() call.