13

Arduino sketches usually feature a setup and loop function. Are these functions only provided for convenience or do they actually have special purposes? (e.g.: are some operations disallowed or allowed in setup and loop)

Are these two pieces of code equivalent:

Classic

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

Without loop

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);

    while(true) {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(1000);
        digitalWrite(LED_BUILTIN, LOW);
        delay(1000);
    }
}


void loop() {
}

Is it possible to write code without either setup or loop, providing, for example, a main method or other entry point?

10

Those pieces of code are pretty much the same.

If you look at the Arduino source code you'll see:

setup();

for (;;) {
  loop();
  if (serialEventRun) serialEventRun();
}

(I'm not sure what serialEventRun() does.)

I don't think you can 'overload' the main function declaration. Also note that the main function calls init, which e.g. configures the timers, so millis() etc. will work.

9

Are these functions only provided for convenience or do they actually have special purposes?

The Arduino libraries do a tiny bit of housekeeping with loop(), related to serial handling.

Are these two pieces of code equivalent:

Only if you never use serial events.

Is it possible to write code without either setup or loop, providing, for example, a main method or other entry point?

Not while strictly using the Arduino libraries via the IDE; the linker will complain of either duplicate main definitions or of missing setup or loop definitions.

2

Is it possible to write code without either setup or loop, providing, for example, a main method or other entry point?

The last time I used the IDE, you could have a project with no .ino/.pde file, only .cppfiles; since it's the mangling of the .ino into a .cpp that defines the main function, if you bypass that step then you can define your own main.

If the IDE no longer allows this (as I've heard) you can still do the same thing with e.g. a Makefile-based build outside the IDE, without giving up the Arduino libraries. The caveats that I know of are:

  • You should #include "Arduino.h" if you want access to the library functions.
  • You should call init() if you want the library to set up peripherals the way you're used to.
  • You should put the serialEvent code that Gerben points out in your mainloop if you want a serialEvent handler to run (but you probably don't need this anyway if you want to write your own mainloop).
1

Arduino sketches usually feature a setup and loop function. Are these functions only provided for convenience or do they actually have special purposes?

They throw in an extra function call to init which initializes the timers which let millis, micros and delay work without further effort. Other than that, no.

This code compiles (and runs) under the IDE:

int main ()
  {
  }

Effectively the IDE generates code like this:

int main ()
  {
  init ();  // set up timers
  setup (); // your own initialization
  while (true)
    loop ();  // stuff you want to keep doing
  }

They have fiddled with it since then, as Ignacio Vazquez-Abrams said, but basically you can ignore that. There is nothing particularly special about setup and loop.

You can use main like I showed above. You can do everything in setup if you want. You can do everything in loop if you want (if you never return from it).

Remember, you are dealing with a C++ compiler here. Functions don't have some magic significance based on their names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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