time consuming function, how to get back to void loop?

i am making a program that involves an input(sensor), a servo, and a feedback(sensor).
also i used interrupts to my codes

i have 2 questions:

  1. in void loop, does it need to finish the function called inside in order to restart the loop?
  2. if Q1 is true, then can i go back to the start of the loop while the program is at the called function.

here is a sample code for you to understand:

void Setup()
{
  attAchInterrupt(0, rty, FALLING);   //interrupt for input
  attachInterrupt(1, uio, FALLING);   //interrupt for feedback
}

void loop()
{
  // do something here that involves millis()  
   (if else statement)
   {
      abc();   //function1 called
    }
  // do something increment/transfer values.
    (if else statement)
    {
        def(); //function2 called that is time consuming about 4-7secs.
    }
}

void abc()
{
  //do something
}

//INTERRUPT for input
void rty()
{
  count1++;
  RecordTime= millis();
}

//INTERRUPT for feedback
void uio()
{
  count2++;
}

void def()
{ 
  //do something time consuming.
}

You have one processor and no threads or OS level processus (there is no OS) so your function indeed needs to return before the loop can continue, ends and start again

You need to study state machines where you would call repetitively your function from the loop until it completes - and ensure your function proceeds small steps by small steps To not lockup the loop if you care catching events from the main loop

(Ensure your count variables are defined as volatile)

Maybe have a look at Planning and Implementing a Program.

Note how all the functions return very quickly but it may need many calls to a function before it completes its task.

...R

thanks @robin2 ill read from that link later, lets see if i can get answers to solve my prob, definitely ill learn alot..

You will need to break your time consuming operation into bite-sized chunks. Lets say that the below simulates your time consuming function.

void longOperation()
{
  for (long step = 0; step < 100000; step++)
  {
    delay(1);
  }
}

You can replace this by a non-blocking version as shown below

/*
  time consuming operation
  returns:
    true when completed, false while in progress
*/
bool longOperation_nonBlock()
{
  static long step = 0;

  if (step < 100000)
  {
    // simulate something that takes a little bit of time
    delay(1);
    // increment step
    step++;
    // indicate that we have not completed our long operation
    return false;
  }
  else
  {
    // next time we need to start from the beginning again
    step = 0;
    // indicate that we have completed the long operation
    return true;
  }
}

And in loop() you can call it

void loop()
{
  static bool longoperationCompleted = false;

  // start or continue the time consuming operation
  if (longoperationCompleted == false)
  {
    longoperationCompleted = longOperation_nonBlock();
  }

  // to run the time consuming operation again, set longoperationCompleted to false (e.g. on button press)
  if(digitalRead(yourButton) == LOW)
  {
    longoperationCompleted = false;
  }
}

It's anything but, "void loop". It's "the loop function", or "loop".

thanks a lot @sterretje this makes sense a lot... but im gonna have to recode this.. ill update if any thing good happens...

If it helps, you can use "return;" at any point in a function, including -loop- to exit the function early.

Paul

i needed to finish the loop in the function, thats why i cannot exit early. thanks tho.

yoyoy_kun:
i needed to finish the loop in the function, thats why i cannot exit early. thanks tho.

In which function?

@sterretje has suggested the ONLY way to handle a muti-step function that takes 'too long'

I use a similar FSM idea to send SMS - because if I start sending, then wait for it to complete, it may take several seconds, so I perform the send in several steps - returning and waiting in the main loop() for the current state to complete.

enum modem_states {
  MODEM_IDLE, 
  MODEM_SMS_WAITPROMPT,
  MODEM_SMS_SENDBODY,
  MODEM_SMS_WAITFOROK,
};

I actually have other states as well for other functions (Clock, RSSI etc), along with zillions of millis() events too. And it all works!

@AWOL, the time consuming function.
what i am doing at the moment is reconstructing my codes the way sterretje is suggesting.
while reading Robin2's tutorials.

@lastchancename, thanks, i've now learned about FSM, but can't get your code can elaborate, is that from a library? i just started coding in arduino.