I want to end loop at a specific time and start setup again. Is this possible? (I can explain reason in case you require).
-
As others have suggested, that is not how an Arduino program works. It runs the setup function once, and then calls the loop function repeatedly and forever until it is powered down or reset. You'll have to explain what you're trying to do in order for us to help you get it working.– Duncan CJun 15, 2020 at 20:13
2 Answers
You could just call setup()
yourself:
void loop() {
if (some_condition) {
setup(); // reinitialize everything
return;
}
// etc.
}
But, as Majenko puts it, if you nee to do that it looks like there is something wrong with your methodology.
That being said, it would be less suspicious if you only need to
reinitialize some specific part of your program. In this case you can
make a separate function for these initializations, and call it both
from setup()
and, when needed, from loop()
:
void init_foo() {
// ...
}
void setup() {
init_foo();
// other initializations...
}
void loop() {
if (some_condition) {
init_foo(); // reinitialize foo
return;
}
// etc.
}
Not directly, no. You can reboot the Arduino though so everything starts from scratch (either through the watchdog timer or wiring a GPIO pin to RESET and pulling it LOW), but if you find you need to do this sort of thing then you should rethink your methodology so that you don't need to do this sort of thing.