Delay function in setup

Hi,
I have 1 question for you. Pls help me take a look it

// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

Could i remove this delay in my application (I do not need to debug in Serial Monitor) ?. I am not sure if i remove it then there are any impact or not stable something.

Thank in advance

Contrary to the comment, delay(1500) would indeed block for 1.5 seconds.

As to the impact of removing it from the un-posted code, it is difficult to tell.

1 Like

What is you application?
Which arduino are you using?
Do you actually use the serial monitor in your code?

I use this as the first line in setup() on ESP devices. The only issue is that the ports are floating until I initialize them. To address this, I use pull-down resistors on all critical ports, which prevents any problems.

Alternatively, you can set up the ports first and then add the delay. The delay serves two purposes:

  1. It gives the hardware time to reset and update the baud rate to the desired value.
  2. It provides an easy way to recover from a serial flood, allowing you to regain control.

delay() makes the Arduino count clock cycles. If you right-click on delay() in your IDE, you will see this definition:

void delay(unsigned long ms)
{
	uint32_t start = micros();

	while (ms > 0) {
		yield();
		while ( ms > 0 && (micros() - start) >= 1000) {
			ms--;
			start += 1000;
		}
	}
}

delay() just counts by 1000 us, waiting for the count to equal the argument.

An object initialized just before delay() allows that device to start (for "1500" ms) before receiving any information from the Arduino. A few years ago, delay() following Serial.begin() was often advised, especially if the first characters in a Serial.print() were "garbled." The assumption being unsuccessful handshaking with a device not fully ready to communicate.

Remove delay() after Serial.begin() and verify the Serial comm works. If so, leave delay() out. You will learn to sparingly use delay() (almost never) and have more responsiveness in your programs.

1 Like

hi jim-p,

it is only blynk led (simple projects). I do not want to add delay + a Serial Monitor. Only want to remove it and will same time in setup function.

Thanks and regards

Hi @11119999. As mentioned previously in the thread, it is impossible to give a definitive answer without studying your code. However, in general this delay is only required if you want to make sure you can see the initial output (which can include debug output produced by the ArduinoIoTCloud library) printed by the program to serial.

If you aren't using Serial Monitor, or if you aren't concerned about seeing the initial output, then you can remove the delay without doing any harm.

Got it, thank you so much
I will remove Serial.begin and delay(1500) in my application. it will be better to fast run in setup function

You are welcome. I'm glad if I was able to be of assistance.

Regards, Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.