Restart loop

Hi, is there a way to start vold loop from begining in the middle of the loop if something happens?

For an example, the loop starts and loops as it should if a pin is set HIGH, but if the pin is LOW, the loop starts from the top and do the first bit of the loop until the pin is set HIGH again, then it runs the entire loop.

Hello,

Yes you can use return;

...or you could just skip to the end of loop()

How does return; work?

Can I use in an if () statement?

Like:

if (pin == LOW){
return;
}

Yes

How does return; work?

As you may guess from its name, return; causes the program to return to where the current function was called from. In this case loop() is called repeatedly from a hidden main() function, hence using return causes loop() to be called again from the start

So if I use the return; function in the middle of a loop, the second half of the loop will never run?

Yes

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