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.
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:
It gives the hardware time to reset and update the baud rate to the desired value.
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.
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.