Timing a servo loop ?

Hi There,

I am working on a fun project and am using a servo. I have managed to get it to turn 5 times then stop - sucessfully which has taken me a few days ! :o

I am now trying to figure out how I get it to go through the same loop ( x 5 servo turns / then stop ) every 15 minutes - I’ve been looking for hours - is there anyone out there who can put me in the right direction?

Thank you

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards

int pos =0;    // variable to store the servo position 



void setup() 
{ 

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 


//Rotation 1
{ 
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 

//Rotation 2

  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 

//Rotation 3

myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 

             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 
  
//Rotation 4
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 


//Rotation 5             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 
  
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
    myservo.detach();
  } 
}

Looks like I have sorted it actually! Of course, as soon as I ask a question I find an answer! he he

Attaching below if it may be of help to anyone else. Just change the delay as to how long you need it for!

 #include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards

int pos =0;    // variable to store the servo position 



void setup() 
{ 

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

void loop() 


//Rotation 1
{ 
  delay(4000);
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 

//Rotation 2

  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 

//Rotation 3

myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 

             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 
  
//Rotation 4
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 


//Rotation 5             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
  } 
  
  myservo.write(180);             
    delay(4000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=90; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); // waits 15ms for the servo to reach the position 

  } 
             
    delay(15); //stops motor for 4 seconds   
  for(pos = 90; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position
    myservo.detach();
  } 
}

Hi, as a newbie I have a similar question.

I want to use sweep program10 minutes in every one hour. I don't want to use any external module for this.

Every help will be appreciated.
Thank you.

It is OK to use the delay() function for simple test programs but it is not suitable for "real" programs.

Have a look at how millis() is used to manage timing Several things at a time. As the title implies it allows different things to be timed independently.

You can easily do approximate timing without any external device but an Arduino does not run at precisely 16MHz so the timing will not be in sync with your kitchen clock after several hours. If you need that accuracy you need an external Real Time Clock (RTC) module.

...R