While (!Serial) {}; Not Sufficient On Upload or USB Plugin

I was having an issue requiring to unplug from USB power to reset the BLE Module and kept noticing unexpected Serial print behavior.

Using this sketch for testing:

void setup() {
  // delay(1201);  // For Upload
  // delay(850);  // For USB Plugin
  // No Delay for Reset
  Serial.begin(9600);
  while (!Serial) {};
  Serial.println("Serial enabled");
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println("Time to Blink");
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

On my laptop while (!Serial) {}; only seems to properly work when I restart a sketch with the Reset button.

When I upload the sketch "Serial enabled" does not get sent to the Serial Monitor unless I add a delay > 1200.

When I plug the R4 WiFi into USB to power it up a delay of 850 is needed for "Serial enabled" to be printed.

Not sure if it matters, but it was unexpected behavior since no delay is necessary when using the reset button.

PS - the function is changing lowercase 'l' to uppercase 'L' after //'s

!Serial only works for boards with native USB. on Uno R4 WiFi the main MCU is not connected to computer by its native USB

Thank you for the clarification.

So is the only option to start every sketch with a delay?

just have the Serial Monitor already open

On the WIFI board, this does absolutely nothing!

Specifically on WIFI board, the object:
UART _UART1_(UART1_TX_PIN, UART1_RX_PIN);

is used for Serial:
#define Serial _UART1_

And the bool operator used for detection is implemented:

/* -------------------------------------------------------------------------- */
UART::operator bool() {
/* -------------------------------------------------------------------------- */  
	return true;
}

I don't know if there is any way to fix it to work better. Earlier I created a thread to talk about differences in the USB Serial between the two boards:

USB Serial - differences between WIFI and MINIMA - UNO R4 / UNO R4 WiFi - Arduino Forum

So far, no responses. Nor were there any on the issue I opened on it that was closed out.

That does not work either. Example sketch:

void setup() {
  Serial.begin(115200);
  while(!Serial) {}
}

int loop_count = 0;
void loop() {
  Serial.print("Loop: ");
  Serial.println(++loop_count, DEC);
  if (Serial.available()) {
    while (Serial.read() != -1);
    Serial.println("Paused");
    while (Serial.read() == -1);
    while (Serial.read() != -1);
  }
}

Output after download:
image

Although it does work if you do a reset:

Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
Loop: 6
Loop: 7
Loop: 8
Loop: 9
Loop: 10
Loop: 11
Loop: 12
Loop: 13
Loop: 14

It also does not work properly if I just turn the power on:

p: 556
Loop: 557
Loop: 558
Loop: 559
Loop: 560
Loop: 561
Loop: 562
Loop: 563
...

Not that it matters, but I personally find this to be an implementation detail.

And that there are probably lots of sketches that wish to know when the USB is connected and properly configured.... But again, that is probably just me.

1 Like

I sometimes use

  while (!Serial.available()) {}

or

  while (!Serial.available()) {
    Serial.println("Send any key");
    delay(1000);
  }

As @KurtE mentioned this does not work when I upload the sketch or first plug it in.

My ignorance led me to believe the While command was working for the times when a sketch is restarted with the reset switch.

I will try your alternative - thank you!

while (!Serial.available()) {}

These work, thanks.
But then you have to hit a key to start every sketch.
I guess I will stick with a delay at the beginning of the sketch until a way to detect the USB connection is ready.

Not just you...