How to get a servo motor to Run for 30mins then stop.

Hi i am new to arduino boards and just wonder could anyone help me figure out how to get a sevo motor to run for 30mins and then stop. i have been looking at tutorials for the last couple of day and still have no joy. i have copied my program below it currently has a counter and it runs fine its just i want to swap out the counter with some sort of timer to make it run for 30 mins. Thank you

#include <Servo.h>

Servo myservo;

int pos = 0;
int myCounter = 0;
int mySwitchPin = 4;

void setup() {
myservo.attach(9);
pinMode(mySwitchPin, INPUT_PULLUP);
}

void loop() {

if(myCounter<10)
{
for (pos = 35; pos <= 155; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (pos = 155; pos >= 35; pos -= 1)
{
myservo.write(pos);
delay(15);

}
myCounter = myCounter + 1;
}
else if(digitalRead(mySwitchPin) == LOW)
{
myCounter = 0;
}
}

See this other current Thread

Are you using a continuous rotation servo?

...R

Maybe you can use the now() function. It returns the time in number of seconds.

So for initialisation, use a variable to store the start time... like start_time = now();

Then.... in your loop.... just keep time_elapsed.

eg. time_elapsed = now() - start_time;

.... and in your loop.... if time_elapsed exceeds 1800 seconds, then apply a command to stop your motor.

Sorry its not a continuous servo, i think i may have got it working im going to try this today.

#include <Servo.h>

Servo myservo;

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

void loop()
{
if (millis() < 30UL60UL1000UL)
{
for (int pos = 35; pos <= 155; pos += 1)
{
myservo.write(pos);
delay(15);
}
for (int pos = 155; pos >= 35; pos -= 1)
{
myservo.write(pos);
delay(15);
}
}
}

Tell me what yous think..ill also need to add in a button off some sort
Thank you

DSher:
Tell me what yous think.

You tell us if it works first.

...R

How accurate does the thirty minutes need to be? It appears that in the worst case, your servo will continue to move for an extra ~3600mS after time is up.

Ye that program works fine..about the 3600ms it wont really make that much of a difference to the application i am doing. Cheers thanks for your help thou :slight_smile: