2

Are loops not allowed in setup()? The following doesn't print anything inside the for loop to the serial terminal

Code:

void setup() {
  Serial.begin(9600);
  Serial.println("Ping");
  for (int i = 0; i >= 10; i++) {
    Serial.println(i);
    Serial.println("Pong");
  }
}

void loop() {

}

Output:

Ping

2 Answers 2

8

In principle yes, you can use a loop in setup() however this specific code

for (int i = 0; i >= 10; i++) {
  Serial.println(i);
  Serial.println("Pong");
}

will result in the body of the loop never executing, as the loop test i >= 10 is initially false.

You probably meant i <= 10

Generally speaking, it would be poor program structure to use a loop in setup() in place of one in loop(), however if you need a loop to apply similar operations to a range of things, or to briefly busy wait on a condition as part of setting up operation, that kind of thing could be appropriate.

3

You could (and I occasionally do) write your entire program in the setup() function. Setup() and loop() are constructs provided for your convenience, but they aren't necessary. In C/C++ compiled programs, it conventional for the C run-time code to call main(), and for main() to be written by the programmer.

It is conventional within the Arduino IDE world to bury the complicated stuff to let new user get productive quickly with a gentle learning curve. That includes a pre-written main() that provides the calls to setup() and loop() for you.

The following main() function is typical of a number of pre-written Arduino main() functions, but you don't have to use the pre-written one; you can supply your own. It will need to call init(), and Arduino library function that prepares the hardware and software environment into a known state; after that you're good to go.

/*
 * main.cpp
 *
 *  Created on: Dec 13, 2011
 *      Author: jrobert
 */

#include "WProgram.h"

int main(void){
    init();
    setup();
    for(;;)
        loop();
}



extern "C" void __cxa_pure_virtual() {
    while (1)
        ;

}
3
  • While that was true years ago, this is not sound advice today (2015). The current main() includes (if (serialEventRun) serialEventRun();) after loop(). The user's loop() is expected to return; in order to yield time back for other services to run.
    – jdr5ca
    Dec 21, 2015 at 4:31
  • Correct, modern versions do include such a call. But that is exactly the point: The C main() function is where the run-time setup hands over control to the user; what the user writes from there down is up to him/her. Every program doesn't need every facility, and it is assumed that you know what you are doing when you circumvent the provided ones. Do also note, that unlike setup() and loop(), which you must provide (or the linker will fail), 'if (serialEventRun)` specifically honors the lack of such a requirement by testing for the existence of serialEventRun() before trying to call it.
    – JRobert
    Dec 21, 2015 at 12:39
  • ... An equally reasonable addition to main() would be calls to similarly optional functions for timer events, Ethernet or WiFi events, and even some less critical interrupts. These support facilities aren't at all necessary; in fact I can't remember when I've seen an Arduino program that actually used serialEventRun() instead of managing serial on its own.
    – JRobert
    Dec 21, 2015 at 12:49

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.