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)
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;
}
}
@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.
@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.