user52: Question: Where do I put code if I want it to execute only once per Arduino startup?
Background: I tend to use my Arduino to perform tasks that should be executed once per power cycle.  (For example, my robot should only drive forward once every time I turn it on--more than that, and it might fall off the table.)
The standard Arduino IDE "template" consists of a loop() and a setup() function.  The setup code is run once per power cycle, and the loop is re-started every time it finishes.
I see two approaches to code "1 time tasks:"
Method 1: "Loop once"
void setup() {
  //do setup stuff
}

void loop() {
  //do task

  while(1) ; //Repeat forever, preventing function from re-starting
}

Method 2: "Setup only"
void setup() {
  //do setup stuff

  //do task
}

void loop() {
  //do nothing
}

More detailed form of question:  Is one of these methods "better" from a hardware/software sense?  (e.g. is there anything "special" about the setup function, like it can only run for so much time?)
And, if they are the same from a hardware/software perspective, is there a "best practice" or standard way of writing code for these situations in the Arduino community?

user11: Definitely go with method two. That's what the setup function is there for. It's much easier to read as well. Anything in the loop() implies it will be run in a loop.

user8: There is no time limit on the setup function. I prefer method 2 because the loop can be filled with whatever you need to later.

user13: I would strongly prefer Method 2. If you ever plan to add code to handle input, output, etc,  it's easy with Method 2 -- just fill in loop(), but requires reworking/refactoring in Method 1. 

user62: I usually go with Method 2, but end up doing this:
void setup() {
  //do setup stuff

  //do task
  init(); //Do start-up initialization steps
}

void init() {
  // do tasks on startup
}

void loop() {
  //do looping code
}

With the above setup it allows my code to be even more organized.

user86: I'm kind of confused by your question. You ask where you want to put once-per-startup setup functions, and then discuss the setup function. That's what the setup function is for.
As such, one-time setup functionality goes in the setup function.
FWIW, if you look in the file that calls the setup and loop functions:  
#include <Arduino.h>

int main(void)
{
    init();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
    return 0;
}

For all intents and purposes, the two options are completely identical. Either way, you get a empty busy-wait loop. Frankly, I'd expect the two different options to probably emit the same machine code anyways, so the whole thing is a non-issue.
Note:
if (serialEventRun) serialEventRun(); appears to be a facility to allow you to attach a function that is called upon reception of serial data, but if you do not define a function void serialEvent(){} in your code, it will compile out completely and not be present in the produced machine code.

52: <p><strong>Question:</strong> Where do I put code if I want it to execute only once per Arduino startup?</p>

<p><strong>Background:</strong> I tend to use my Arduino to perform tasks that should be executed once per power cycle.  (For example, my robot should only drive forward <em>once</em> every time I turn it on--more than that, and it might fall off the table.)</p>

<p>The standard Arduino IDE "template" consists of a <code>loop()</code> and a <code>setup()</code> function.  The <code>setup</code> code is run once per power cycle, and the <code>loop</code> is re-started every time it finishes.</p>

<p>I see two approaches to code "1 time tasks:"</p>

<h2>Method 1: "Loop once"</h2>

<pre><code>void setup() {
  //do setup stuff
}

void loop() {
  //do task

  while(1) ; //Repeat forever, preventing function from re-starting
}
</code></pre>

<h2>Method 2: "Setup only"</h2>

<pre><code>void setup() {
  //do setup stuff

  //do task
}

void loop() {
  //do nothing
}
</code></pre>

<p><strong>More detailed form of question:</strong>  Is one of these methods "better" from a hardware/software sense?  (e.g. is there anything "special" about the <code>setup</code> function, like it can only run for so much time?)<br>
And, if they are the same from a hardware/software perspective, is there a "best practice" or standard way of writing code for these situations in the Arduino community?</p>
 11: <p>Definitely go with method two. That's what the setup function is there for. It's much easier to read as well. Anything in the <code>loop()</code> implies it will be run in a loop.</p>
 8: <p>There is no time limit on the setup function. I prefer method 2 because the loop can be filled with whatever you need to later.</p>
 13: <p>I would strongly prefer Method 2. If you ever plan to add code to handle input, output, etc,  it's easy with Method 2 -- just fill in <code>loop()</code>, but requires reworking/refactoring in Method 1. </p>
 62: <p>I usually go with Method 2, but end up doing this:</p>

<pre><code>void setup() {
  //do setup stuff

  //do task
  init(); //Do start-up initialization steps
}

void init() {
  // do tasks on startup
}

void loop() {
  //do looping code
}
</code></pre>

<p>With the above setup it allows my code to be even more organized.</p>
 86: <p>I'm kind of confused by your question. You ask where you want to put once-per-startup setup functions, and then discuss the setup function. <em>That's what the setup function is for.</em></p>

<p>As such, one-time setup functionality goes in the setup function.</p>

<p>FWIW, if you look in the file that <em>calls</em> the <code>setup</code> and <code>loop</code> functions:  </p>

<pre><code>#include &lt;Arduino.h&gt;

int main(void)
{
    init();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
    return 0;
}
</code></pre>

<p>For all intents and purposes, the two options are completely identical. Either way, you get a empty busy-wait loop. Frankly, I'd expect the two different options to probably emit the same machine code anyways, so the whole thing is a non-issue.</p>

<p>Note:<br>
<code>if (serialEventRun) serialEventRun();</code> appears to be a facility to allow you to attach a function that is called upon reception of serial data, but if you do not define a function <code>void serialEvent(){}</code> in your code, it will compile out completely and not be present in the produced machine code.</p>
