1

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.

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Feb 26, 2019 at 13:17

2 Answers 2

1

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.

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
answered Feb 26, 2019 at 13:25
0

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.

answered Feb 26, 2019 at 14:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.