Will a delay() function in a void SerialEvent() also stop the code that is running in the void loop() function?
This is about an Arduino Uno.
2 Answers 2
This is the main function of the Arduino core:
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
serialEventRun()
runs the serialEvent()
function, if it is assigned,
so yes, a delay in serialEventRun will delay the loop.
In an Atmega328p, the Uno's processor, there is only 1 CPU; it can only do one thing at a time. If it is executing code in the SerialEvent() function, then by definition, it is not executing code in the loop() function or anywhere else.
There is a minor exception to this: interrupt service routines will run briefly when their respective interrupts occur. In fact, one way a delay() function can operate depends on timer updates that take place as interrupts. (The other way is by executing a loop whose execution time is accurately known, a specified number of times).
But interrupt service routines do one, very short, job, and return the CPU to whatever it was doing immediately before (delay() within the SerialEvent() function), so no other user code can be executed until the delay ends.