methods other than loop()

I always thought that all Arduino actions had to be within the loop(), but I found examples of methods other than loop() being used successfully without being inside of loop(). For example: reply #9 in the following thread: (another strange thing is as the person points out a new loop() had to be declared at the end to make the program compile.

Can anyone explain the rules of capturing everything in one loop or whether other methods can follow without being in a loop? I see the reference to how to use loop() but it's not clear why the code above works since another loop() is introduced. And maybe we can use multiple loops like "go to B loop if certain requirements are satisfied"?

loop() is a function, like setup() or Serial.print() or analogRead() or digitalWrite().

Of course you can create and call additional functions.

There can only be one loop() function in any sketch. That function is called in an infinite loop - as soon as it ends, it is called again. From inside loop, you can call your functions.

Thanks PaulS. So it is accurate that all methods have to be inside of loop(). Do you know why in the example below a second declaration of loop() has to be made to make the compileable? I tried it out and it turned out to be true. I wonder why.


In that code, there is only one loop function. The comment says that the loop function needed to be defined, even though all the action occurs in the setup() function, which never ends (unlike most setup functions), so loop() never actually gets called. But, it must be there.

So it is accurate that all methods have to be inside of loop().

Functions are defined outside of loop() or setup(). They are then called from setup(), loop(), or a function that is called from setup() or loop().

I see. I didn't notice that, but you're right. It seems odd to declare all the actions in setup.