0

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!

4
  • 1
    You are asking the wrong question. The loop function runs forever. How many times do you want the servo to move between those values? If that is for example 30 times, then you can put a for-statement in the setup function and use a empty loop function.
    – Jot
    Jan 18 '19 at 19:35
  • what do you do when you want to boil eggs for one minute? .... do the same in the program
    – jsotola
    Jan 18 '19 at 22:34
  • Thank you so much!!!! that was exactly what i needed!! i also tried before to run a servo and a stepper at the same time and that was exactly what happened the stepper was blocking the servo and i wasn't understanding why!! thank you so much for the explanation, it truly helped!!
    – Adri47
    Jan 19 '19 at 10:38
  • Whose explanation are you thanking? The answer boxes are not for comments, I'll migrate your answer to a comment under your question. I'm glad one of us helped you. :)
    – Nick Gammon
    Jan 19 '19 at 10:56
3

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.

1

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.

0

Another way to handle this is to use a variable endTime to tell the loop when to stop moving the servo:

unsigned long endTime;

void setup()
{
  myservo.attach(5);
  endTime = millis() + 60000;
}

void loop()
{ 
  if (millis() <= endTime) {
    myservo.write(45);
    delay(1000);
    myservo.write(125);
    delay(1000); 
  } else {
    //code to run once the servo is done running
  }
}

This code will move the servo back and forth for about 1 minute, then stop. It's set up so you can do something else once the loop has completed.

Note that by using a variable endTime and leaving your servo code in the loop() function, you can have your program start doing something else once the motor motion is completed. You can also add code to set up a new value in EndTime, at which point the servo will start moving back and forth again.

Also note that using delay is bad practice. When you do that you bring the arduino to a full stop while the delay code is running. You can't be doing something else. Say, for example, you wanted to move 2 different servos at different time intervals.

Instead of using delays you should track the end time for each phase of your loop, and execute your code once that end time passes.

2
  • This code will move the servo for one minute every 49.7 days. Jan 19 '19 at 10:17
  • Yeah, I suppose that's true, if you Arduino plugged in that long. (I mostly work on 64 bit platforms now, where an unsigned long is 64 bits, long enough that the above code would only run once in my or my grandchildren's lifetime.) It would be pretty easy to add a bool to the code above to turn it off completely when not in use.
    – Duncan C
    Jan 19 '19 at 12:06

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.