Make Servo on and off (looping)

hello, i'm newbie about arduino
i have a project use arduino and digital 180o servo.

this is my program.

// Sweep


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
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() 
{ 
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 10ms for the servo to reach the position 
  } 
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 10ms for the servo to reach the position 
  } 
}

i want to add looping turn ON my servo for 2 minute, and turn OFF for 10 minute,
but i dont know how to add the the program,
anybody help me?
thanks

Look at the BlinkWithoutDelay example in the IDE. It turns an LED on/off but could easily be changed to activate the servo for the periods that you require.

odist:
i want to add looping turn ON my servo for 2 minute, and turn OFF for 10 minute,
but i dont know how to add the the program,
anybody help me?

Using even more delay() code within the loop function?

For the reason of blocking the program execution at some point for a while, as you already do with the delayed rotation?

Perhaps like that:

#define MILLISPERMINUTE 60000L   // milliseconds in one minute
void loop() 
{ 
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 10ms for the servo to reach the position 
  } 
  delay(2*MILLISPERMINUTE); // two minute delay
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 10ms for the servo to reach the position 
  } 
  delay(10*MILLISPERMINUTE); // ten minute delay
}

Total cycle length for the loop is then something at 12 minutes and 1.8 seconds:
2x 0.9 seconds servo rotation delay
1x 2 minutes
1x 10 minutes

Good programming practise is different and avoids delay().
Especially if you want to do "different tasks at the same time".

i want to add looping turn ON my servo for 2 minute, and turn OFF for 10 minute,

What are you expecting when the servo is "on" and when "off"?

UKHeliBob:
Look at the BlinkWithoutDelay example in the IDE. It turns an LED on/off but could easily be changed to activate the servo for the periods that you require.

i'm so sorry, i'am very newbie with this one.

can you help me to add the code to my program that i have?

Please answer the question posed by zoomkat. What exactly are you expecting the servo to do ?

zoomkat:
What are you expecting when the servo is "on" and when "off"?

when the servo is "on" for 2 minutes, i want the servo doing sweep from 0 to 90 degrees and back to 0 degrees. After 2 minutes, i want the servo is "off" and do nothing for 10 minutes. After 10 minutes the servos is "on" and doing sweep again.

Should the servo always stop at the same position before the 10 minute wait and, if so, what is that position, and does it matter if it jumps to that position at the end of the 2 minute wait or must the current sweep be completed until it reaches that position ?

UKHeliBob:
Should the servo always stop at the same position before the 10 minute wait and, if so, what is that position, and does it matter if it jumps to that position at the end of the 2 minute wait or must the current sweep be completed until it reaches that position ?

After doing sweep for 2 minute, i want the servo stop or being "OFF" at position 0 degrees and do nothing. And after 10 minutes, the servo is going "ON" and doing sweep from 0 degrees to 90 degrees (looping) for 2 minute, after that the servo is "OFF" again at position 0 degrees.

this is the flowchart of the program that I want to create.
anybody help me?
thanks.

Nice attempt at a flow chart, they're good things to use (also state diagrams) - however, it's a bit incomplete.

I'm sure someone will link the 'blink without delay' thread soon ... it is very relevant to those (incomplete) 'after t=2mins' bits in your flowchart. In the meantime also ask yourself about that last branch, you've got no condition there...

Have a look at this and see what you make of it.

#include <Servo.h>
Servo aServo;
//byte servoState = 0;
byte timeState = 0;
unsigned long servoStateStart;
unsigned long timerStateStart;
unsigned long timeNow;
unsigned long timerPeriod = 2UL * 60UL * 1000UL;  //2 minutes
unsigned long servoPeriod = 10;
int servoIncrement = 1;
int servoPos = 0;
boolean servoActive = true;

void setup()
{
  Serial.begin(115200);
  aServo.attach(9);
  servoStateStart = millis();
  timerStateStart = millis();
  timeNow = millis();
}

void loop()
{

  switch (timeState)
  {
    case 0:  //sweep servo for 2 mins
      timeNow = millis();
      if (timeNow - timerStateStart >= timerPeriod)  //timer period is up
      {
        aServo.write(0);  //servo to home position
        servoActive = false;
        timeState = 1;
        timerStateStart = timeNow;
        timerPeriod = 10UL * 60UL * 1000UL;  //next period is 10 mins
        Serial.println("Entering 10 minute period with servo at home position");
      }
      break;

    case 1:  //sweep servo for 2 mins
      timeNow = millis();
      if (timeNow - timerStateStart >= timerPeriod)  //timer period is up
      {
        aServo.write(0);  //servo to home position
        servoActive = true;
        timeState = 0;
        timerStateStart = timeNow;
        timerPeriod = 2UL * 60UL * 1000UL;  //next period is 2 mins
        Serial.println("Entering 2 minute period with servo sweeping");
      }
      break;
  }

  if (servoActive)
  {
    timeNow = millis();
    if (timeNow - servoStateStart >= servoPeriod)  //time to move the servo
    {
      if (servoPos > 180 || servoPos < 0)
      {
        servoIncrement = servoIncrement * -1;
      }
      servoPos += servoIncrement;
      aServo.write(servoPos);
      servoStateStart = timeNow;
    }
  }
}

UKHeliBob:
Have a look at this and see what you make of it.

#include <Servo.h>

Servo aServo;
//byte servoState = 0;
byte timeState = 0;
unsigned long servoStateStart;
unsigned long timerStateStart;
unsigned long timeNow;
unsigned long timerPeriod = 2UL * 60UL * 1000UL;  //2 minutes
unsigned long servoPeriod = 10;
int servoIncrement = 1;
int servoPos = 0;
boolean servoActive = true;

void setup()
{
  Serial.begin(115200);
  aServo.attach(9);
  servoStateStart = millis();
  timerStateStart = millis();
  timeNow = millis();
}

void loop()
{

switch (timeState)
  {
    case 0:  //sweep servo for 2 mins
      timeNow = millis();
      if (timeNow - timerStateStart >= timerPeriod)  //timer period is up
      {
        aServo.write(0);  //servo to home position
        servoActive = false;
        timeState = 1;
        timerStateStart = timeNow;
        timerPeriod = 10UL * 60UL * 1000UL;  //next period is 10 mins
        Serial.println("Entering 10 minute period with servo at home position");
      }
      break;

case 1:  //sweep servo for 2 mins
      timeNow = millis();
      if (timeNow - timerStateStart >= timerPeriod)  //timer period is up
      {
        aServo.write(0);  //servo to home position
        servoActive = true;
        timeState = 0;
        timerStateStart = timeNow;
        timerPeriod = 2UL * 60UL * 1000UL;  //next period is 2 mins
        Serial.println("Entering 2 minute period with servo sweeping");
      }
      break;
  }

if (servoActive)
  {
    timeNow = millis();
    if (timeNow - servoStateStart >= servoPeriod)  //time to move the servo
    {
      if (servoPos > 180 || servoPos < 0)
      {
        servoIncrement = servoIncrement * -1;
      }
      servoPos += servoIncrement;
      aServo.write(servoPos);
      servoStateStart = timeNow;
    }
  }
}

thanks so much for your help. I have finished my project. :smiley:

Do you understand how it works ?
If not, then study it until you do because it uses techniques that will be useful in other projects.