user53122: This is my first Arduino project, and I would like to stop my servo's loop after 1 minute. Can someone help me?
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);
}

void loop()
{ 
  myservo.write(45);
  delay(1000);
  myservo.write(125);
  delay(1000); 
}

Thanks!

user7508: To answer your question as asked, add this to loop():
if (millis() >= 60000) exit(0);

However, I would suggest you seriously consider Jot's comment: simply
stopping loop() from running is most of the time not what you may
want to do.

user10794: As suggested by Jot in a comment, one way would be to simply do what you want (which takes roughly two seconds per iteration) in a loop, like this:
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);

  for (int i = 0; i < 30; i++)
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000); 
    }
}

void loop()
{ 
}


The problem with that is it relies upon knowing how long your delays are, and assumes that the myservo.write() functions take no time. A better method is:
#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(5);

  unsigned long start = millis ();

  while (millis () - start <= 60000)  // for 60 seconds
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000); 
    }
}

void loop()
{ 
}

This uses the millis() function call to find out how much time has elapsed, and wait for 60000 milliseconds (60 seconds).

I've put the code into setup because that is where you put things you want done once.

53122: <p>This is my first Arduino project, and I would like to stop my servo's loop after 1 minute. Can someone help me?</p>

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

void setup()
{
  myservo.attach(5);
}

void loop()
{ 
  myservo.write(45);
  delay(1000);
  myservo.write(125);
  delay(1000); 
}
</code></pre>

<p>Thanks!</p>
 7508: <p>To answer your question as asked, add this to <code>loop()</code>:</p>

<pre><code>if (millis() &gt;= 60000) exit(0);
</code></pre>

<p>However, I would suggest you seriously consider Jot's comment: simply
stopping <code>loop()</code> from running is most of the time <em>not</em> what you may
want to do.</p>
 10794: <p>As suggested by Jot in a comment, one way would be to simply do what you want (which takes roughly two seconds per iteration) in a loop, like this:</p>

<pre class="lang-c++ prettyprint-override"><code>#include &lt;Servo.h&gt;
Servo myservo;

void setup()
{
  myservo.attach(5);

  for (int i = 0; i &lt; 30; i++)
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000); 
    }
}

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

<hr>

<p>The problem with that is it relies upon knowing how long your delays are, and assumes that the <code>myservo.write()</code> functions take no time. A better method is:</p>

<pre class="lang-c++ prettyprint-override"><code>#include &lt;Servo.h&gt;
Servo myservo;

void setup()
{
  myservo.attach(5);

  unsigned long start = millis ();

  while (millis () - start &lt;= 60000)  // for 60 seconds
    {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000); 
    }
}

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

<p>This uses the <code>millis()</code> function call to find out how much time has elapsed, and wait for 60000 milliseconds (60 seconds).</p>

<hr>

<p>I've put the code into <code>setup</code> because that is where you put things you want done once.</p>
