Suspending the loop for one function to finish

I am doing some operations inside the loop every minute based on some sensor output. If the output is a special value (9100), I need to do operation_B() that lasts more than 1 minute. How do I suspend the loop call until the operation_B finishes? Is it a good idea to
reset the value of currentMillis to millis() until the operation_B finishes or is there any other way to do it?

unsigned long previousMillis = 0;
const long interval = 60000;

void loop()
{

        unsigned long currentMillis = millis();
        // loop is reapeating everey minute
        if (currentMillis - previousMillis >= interval)
        {
                previousMillis = currentMillis;
                int command = check_command();     // check_command returns 9000 or 9100 based on some senser value

                // 9000 - normal  9100- update parameter

                if (command == 9000)
                {
                  operation_A(9000);
                }
                else
                {
                  operation_B(9100);
                }
        }
}

void operation_B(int val)
{
  // this operation_B takes more than 2 minutes.
  // I want to suspend loop call during operation_B.
}

When your Arduino executes the function Operation_B, it doesn't do anything in the loop, so it is actually suspended. When operation_B is finished, the loop's execution resumes at the place it stopped.

dayDreamer1013:
I am doing some operations inside the loop every minute based on some sensor output. If the output is a special value (9100), I need to do operation_B() that lasts more than 1 minute. How do I suspend the loop call until the operation_B finishes? Is it a good idea to
reset the value of currentMillis to millis() until the operation_B finishes or is there any other way to do it?

There is no built-in concurrency on the Arduino. Calling a function is just plain old C/C++. It executes and returns to the calling function which is loop().

What will happen is that since operation_B() lasts more than 1 minute then the next loop() call will see that more than an interval of time has passed and perform the check_command and reset previousMillis to currentMillis.

You've not really given enough info re what is happening in these operations for a good answer; @lesept gives a very concise answer if you're okay with operation_X() blocking until completion.

void operation_X( int val )
{
    bool
        done = false;
        
    do
    {
        ...
        if( operation is complete )
            done = true;
               
    }while( !done );
    
}//operation_X

But is that what you want? For nothing else to happen for a minute or more while the operation sorts itself out?

Can the task be broken into "pieces" that can be serviced by the MCU in a non-blocking fashion? Can you implement a state machine to handle the task? The state machine returns "false" until the task is finished whereupon it returns "true"? The code inside loop() can use that to focus attention on that task but also allow it to run other things.

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