Servos Delay

hi guys,
i’m a complete new bee, i’ve buy an arduino uno to control one servo, i need to control it with a very simple program to move it in the 0° position to the 180°position, wait here for 25seconds, and go back to the 0 position, waiting 25sec and again and again…
does this little program will do this:

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#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 < 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 10 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(20000); // waits 20sec for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=10) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(20000); // waits 20sec for the servo to reach the position
}
}

Your opinion and help will be appreciated.
best regards
christophe

Not quite, no. You need to look at the delays you're using. Hook it up and try it though, it'll quickly become obvious what's wrong.

Your description says 25 seconds, the code suggests 20 seconds (though it's in the wrong place).

yes , but it's just for example, in fact i'll change this delay with test, the most important it's to have the delay in seconds, and to change it, if needed.

do you think this code like writting here will do this? (with the 20sec delay).

myy arduino it's on the way and i'll try the code as soon as receive.
your opinion will be appreciated.
may be if someone have one arduino linked with an servos , someone can test this.
christophe

do you think this code like writting here will do this?

No, your servo will move extremely slowly, taking six minutes to sweep 180 degrees.

Is something like below what you are looking for?

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
int a = 10;
int b = 170;

void setup() {
  Serial.begin(9600);
  myservo.write(a); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  myservo.write(a);
  Serial.println(a);
  delay(5000);
  
  myservo.write(b);
  Serial.println(b);
  delay(5000);
}
1 Like

zoomkat:
Is something like below what you are looking for?

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo
int a = 10;
int b = 170;

void setup() {
  Serial.begin(9600);
  myservo.write(a); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println(“servo-test”); // so I can keep track of what is loaded
}

void loop() {

myservo.write(a);
  Serial.println(a);
  delay(5000);
 
  myservo.write(b);
  Serial.println(b);
  delay(5000);
}

oh , in this case it’s only the loop who have delay, is it correct?
in mine i’ve put the delay in the travel of the arms position.
if it’s that , i understand, and will try this one, and make you one video to show youthe result as soon as i receive the arduino.
thanks for all your help.
regards
christophe

If I understand correct you nedd a loop() like this:

void loop()
{
for(pos = 0; pos <= 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 10 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); //delay so that servo reach new position - depends on step - experiment with speed;
}

delay(25000); //waits 25sec in 180 deg. position

for(pos = 180; pos>=0; pos-=10) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); //delay so that servo reach new position - depends on step - experiment with speed;
}

delay(25000); //waits 25sec in 0 deg. position
}

christophe31:
i’ve buy an arduino uno to control one servo, i need to control it with a very simple program to move it in the 0° position to the 180°position, wait here for 25seconds, and go back to the 0 position, waiting 25sec and again and again…

So you want:

#include <Servo.h> 
 
Servo myservo;
 
void setup() 
  {
  myservo.write(0);  // Set initial position
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  } 
 
 
void loop() 
  {
  myservo.write(180);  // "move it [from] the 0° position to the 180°position,"
  delay(25*1000);  // "wait here for 25 seconds,"
  myservo.write(0);  // "and go back to the 0 position, "
  delay(25*1000);  // "waiting 25 sec"
  // "and again and again..."
  }

hi guy’s,
i’ve receive tonight my arduino uno, and after 5 min of coding, try the code that you have give to me, and i’ve found one who work fine like i want :
goes to one position
Wait the time i want there
goes to the original position.
here the code:
#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 < 180; pos += 180) // goes from 0 degrees to 180 degrees
{ // in steps of 180°
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(28000); // waits 28s for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=180) // goes from 180 degrees to 0 degrees
{ // in step on 180°
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(28000); // waits 28s for the servo to reach the position
}

void loop()
{
  for(pos = 0; pos < 180; pos += 180)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 180°
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(28000);                       // waits 28s for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=180)     // goes from 180 degrees to 0 degrees
  {                                   // in step on 180°
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(28000);                       // waits 28s for the servo to reach the position
  }

Well, that’s one(long winded) way of doing it.
Simpler might be

void loop()
{
    myservo.write(0);             
    delay(28000);                       
    myservo.write(180);
    delay(28000);
}

ok i'll try this one too.
thanks

ok so i’ve try this simplest code, but now, the servos don’t do the loop, in fact it does the code one time , and that’s all…
any idea?

how to proceed to be sure to erase and empty the memory ? to upload code like new card

edit:
i’ve succeed with this code:
#include <Servo.h>

Servo myservo;
Servo servo_b; // 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);
servo_b.attach(10); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos<=180; pos += 180) // goes from 0 degrees to 180 degrees
{ // in steps of 180°
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(22000); // waits 28s for the servo to reach the position
}
for(pos = 180; pos>=1; pos-= 180) // goes from 180 degrees to 180 degrees
{ // in step on 180°
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(100); // waits 28s for the servo to reach the position
}

}

don’t take care of the servo_b, it’ss for another function, and as you note the second delay ( 100ms), doesn’t do anything, the servos goes in up/down position each 22sec,
christophe

how to proceed to be sure to erase and empty the memory ? to upload code like new card

There is no need - any sketch uploaded will overwrite the previous one.

I don't understand why it doesn't loop - the code should be functionally equivalent to the one with loops.
Can you post the entire sketch, please?

ok so i've try this simplest code, but now, the servos don't do the loop, in fact it does the code one time , and that's all......
any idea?

Sounds like you put that code in the setup() function instead of the loop() function. If it was in the loop() function, it would get executed repeatedly. It's very simple code, so there's really no room for any other explanation.

for(pos = 0; pos<=180; pos += 180)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 180°
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(22000);                       // waits 28s for the servo to reach the position
  }

Is it planned that this should take a total of 3960 seconds (66 minutes) to complete this loop?

And you really should update your comments to actually tell what it is doing, it can be very confusing when the code says one thing, and the comment says something else.

Is it planned that this should take a total of 3960 seconds (66 minutes) to complete this loop?

No - read the loop conditions.
22 seconds.

it can be very confusing when the code says one thing, and the comment says something else.

Can’t disagree there.

AWOL:

Is it planned that this should take a total of 3960 seconds (66 minutes) to complete this loop?

No - read the loop conditions.
22 seconds.

Ah, now I see it... But it makes no sense to use the "for" loop then.

This would do the same, but not confuse as much. When I see a loop, I expect it to do some looping, not only do what is inside once then move on.

pos = 180;
myservo.write(pos);
delay(22000);

Or maybe make it even shorter and just say.

myservo.write(180);
delay(22000);

Because the "pos" is set to 180 in the next loop anyways.

See reply #10 :wink:

AWOL:
See reply #10 :wink:

lol yeah… :stuck_out_tongue:

Another way of doing it, and make the servo move slower, and complete the distance in 21.96 seconds, could be like doing it like this

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
                
void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}
 
 
void loop()
{
  for(int pos = 0; 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(122);                       // waits 122ms for the servo to reach the position 
  }
  
  for(int pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(122);                       // waits 122ms for the servo to reach the position 
  }
}

Not even sure whey it needs to wait 22 seconds to get to position… it should take less than 1 second to move from one side to another…