A while back, I created a program that wrote logs to the Serial console and I noticed that the first few log entries were not being displayed. I fixed that by adding a blocking delay before the first logs were written. Later, I added another line of code to setup(), and I immediately noticed that the original problem had returned. An increased delay solved the issue, but puzzled me because the added line came after the blocking delay and after the first few lines were generated. Testing proved that removing the added line allowed me to reduce the delay, and adding the line forced me to increase the delay.
After finishing that code with the increased delay, I decided to go back to basics … to see if I could figure out was going on. I took the following example directly from Particle documentation … and “Hello there!” does not print.
void setup()
{
Serial.begin(); // open serial over USB
while(!Serial.isConnected()) // wait for Host to open serial port
Particle.process();
Serial.println("Hello there!");
}
The example works when I insert a delay(2000) … I tested smaller delays in 100ms increments … nothing smaller worked, including delay(1900).
void setup()
{
Serial.begin(); // open serial over USB
while(!Serial.isConnected()) // wait for Host to open serial port
Particle.process();
delay(2000);
Serial.println("Hello there!");
}
The example also works if I insert a Serial.println(); and a delay(500) … which seems odd since printing an empty line clearly does not take 1.5 seconds and a full 2-second delay is required when I don’t print it.
void setup()
{
Serial.begin(); // open serial over USB
while(!Serial.isConnected()) // wait for Host to open serial port
Particle.process();
Serial.println();
delay(500);
Serial.println("Hello there!");
}
While the above scenario is not exactly like what I saw the first time around, I find it equally puzzling so I decided to reach out before I disappear down the rabbit hole.
- Why is a delay required after Serial.isConencted() toggles true?
- Why does the required delay vary based on code changes like the above?
- Could it have anything to do with my using TeraTerm VT running on Windows as my Serial console?