1

I want to end loop at a specific time and start setup again. Is this possible? (I can explain reason in case you require).

1
  • 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 C
    Jun 15, 2020 at 20:13

2 Answers 2

6

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.
}
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.