Serial.Print() delayed

Hi

I have a simple sketch (see) below;


void setup() {
// put your setup code here, to run once:
Serial.begin(19200);
Serial.setTimeout(10);
// while(!Serial){
// }
delay(10000); //Take some time to open up the Serial Monitor
int x = 0;
while(x < 10) {
Serial.print(“Startup!!!”);
Serial.println(String(x));
++x;
}
Serial.flush();
}

void loop() {
// put your main code here, to run repeatedly:

}

when I run this with the Serial monitor open I get no output for over ten seconds and then all ten lines appear at once.

I have tried different baud rates and a different serial monitor but no joy?

Can anybody help please?

GordonNorris:
when I run this with the Serial monitor open I get no output for over ten seconds and then all ten lines appear at once.

That seems to be what you programmed. What do you want it to do?

This is silly

delay(10000); //Take some time to open up the Serial Monitor

because the business of opening the Serial Monitor will cause the program to restart which means, in practice, that the Serial Monitor was already open before the program got to this line.

...R

GordonNorris:
when I run this with the Serial monitor open I get no output for over ten seconds and then all ten lines appear at once.

I have tried different baud rates and a different serial monitor but no joy?

If you want to delay BETWEEN messages you have to put the delay between messages:

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(19200);
  // Serial.setTimeout(10);  // Only used for input

  // This is equivalent to your: "int x=0; while(x<10){... ++x;}" but easier to read
  for (int x = 0; x < 10; x++)
  {
    delay(1000); // delay one second
    Serial.print("Startup!!! ");
    Serial.println(x);  // No need to convert an integer to a String to print it.
  }
  
  Serial.flush();  // Not needed.  The serial buffer will empty pretty quickly.
}


void loop()
{
  // put your main code here, to run repeatedly:
}