Sketch does not run after reset button or after USB out/in

Hello,

I'm testing the simplest possible Serial Hello World example, combined with the Blink example. After upload (with the serial monitor already open), the sketch runs fine (diode blinks and serial output is correct). When closing and opening the serial monitor, the MKRZERO resets, and the monitor again displays the "SETUP" message from setup(), and then the "Hello World!" messages from loop() - and the diode blinks.

void setup() {
  Serial.begin(9600);
  // delay(1000);
  while (!Serial);
  Serial.println("SETUP");
  pinMode(LED_BUILTIN, OUTPUT);   
}
void loop() {
  Serial.println("Hello World!");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

If I now press the RESET button on the MKRZERO, I hear the USB disconnect and USB connect sounds from the PC, but the sketch does not start - no serial output (if the monitor is already open), and no diode blinking.

The same happens if I physically disconnect the USB (powers down the board, since there is no battery) and reconnect the USB. I get PC sounds, but no serial output or blinking. The USB enumeration has (most often) not changed.

In both cases, as soon as I open the serial monitor, the program will start running.

Also, if I upload the code without the serial monitor already open, the program does not start.

I use Windows 10 version 1703, with a fresh Arduino 1.8.5 (installed today, after uninstalling, cleaning out Arduino from Program Files and from the User Documents, and running CCLEANER on files and registry), I only updated libraries as indicated by the interface, before trying this sketch.

Initially, the Serial.println() in the setup() function would never return an output. This problem was (sometimes) fixed by a delay(). It was also fixed by the while (!Serial); line. When using the delay() version, however, I sometimes find that the serial output does not re-appear after pressing the RESET button, while the diode blinking may resume (still without any Hello World!" output strings)!

I'm surprised by such a complex problem scenario from such a simple sketch, and I clearly hope for help from the forum!

Terje

The "while (!Serial);" is not needed on the MKR series boards.
Comment that out and check again.

That specifically waits until it sees a connection...hence your issue.
The reset also resets the connection too which also indicates the same as you have been seeing.

"The "while (!Serial);" is not needed on the MKR series boards."

This is not my experience at all. If I have this sketch

void setup () {
while (!Serial) {}
Serial.begin(115200);
Serial.println("Hello World");
}

void loop() {}

and I upload it to the MKRZERO, hit the reset, then open the monitor I see Hello World.

If, on the otherhand I have teh sketch

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

I don't get any output.

@tozz88

That is only true for sketches that use the USB port to output to the monitor and even then not always required.

For a "stand alone" use of the MKR ZERO you DO need to omit the while serial otherwise it will wait at that point in the code all day and do nothing else.

Or you could provide a timeout for that section if you really needed the while serial.