Will millis() in called functions delay the main loop until they are completed?

I am writing a program with PS2 remote control running in the main loop (read button presses and run corresponding functions), while also calling a random function (out of 25) after a set time using the "LED blink without delay" method. These random functions are meant to send commands to an external audio player via UART, but a few also need to change to 2 LED states while the audio is playing, then return to the normal state (all done within the called function). If I place a delay in the called function, it will delay the main loop until it is finished - BUT - if I use the non-delay method in the called function, will this delay the main loop as well? Is there a better way to accomplish this?

The main loop transfers program execution to the function, then resumes execution after the function returns.

1 Like

It depends on the actual code... If you stay in the function during the delay (no matter how it's done) you are in that function and not back in your main loop.

2 Likes

Here's an example of what my code contains:

unsigned long MainPrevious = 0;
unsigned long AudioPrevious = 0;
const long MainInterval = 25000;

void setup() { 
player.begin();
digitalWrite PlayLED low;
}

void loop() {
//remote controller functions
unsigned long MainCurrent = millis();
if (MainCurrent - MainPrevious >= MainInterval) {
MainPrevious = MainCurrent;
called_function;
   }
}

void called_function() {
const long AudioInterval = 20000;  //delay for length of audio playback
unsigned long AudioCurrent = millis();
digitalWrite PlayLED high; //change state of LED
player.playSpecified(1); //starts playback of audio, plays once until completed
if  (AudioCurrent - AudioPrevious  >= Interval) {
Previous = Current;
digitalWrite PlayLED low; //change LED state back
return;
}

If that does, indeed, "play until completed", then that line will block until the playback is completed.
It would be as if you had put a delay() there for the playback duration.

If you don't want everything else to stop & wait while the playback is happening, then you need to just start the playback and not wait until it finishes.

In that case, you'd need some other way to tell when the playback has finished; eg, polling its status, or a so-called "callback"...

This line sends a command to an external audio player via UART. The player then plays the requested audio file once independent of the UNO programming. This player has multiple playback options, including audio looping until stopped, single play, and play only while input is held.

I currently have a NANO programmed to handle the audio and LED control, but would like to eventually have it so that a PS2 controller button press will not only command a servo sequence, but trigger the audio playback and LED change as well.
Would it make more sense to leave the audio/ LED control to the NANO, and trigger that from the UNO programming via serial/ UART link?

1 Like

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