Go Down

Topic: Servos Delay (Read 2874 times) previous topic - next topic

bld

Code: [Select]
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.
captain-slow.dk | non contagious!

AWOL

Quote
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.

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

Can't disagree there.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

bld


Quote
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.
Code: [Select]

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


Or maybe make it even shorter and just say.
Code: [Select]

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

Because the "pos" is set to 180 in the next loop anyways.
captain-slow.dk | non contagious!

AWOL

See reply #10  ;-)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

bld


See reply #10  ;-)
lol yeah... :P

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
Code: [Select]

#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...
captain-slow.dk | non contagious!

christophe31

the servos goes to the position quickly, but he wait 22seconds before come back to the "zero" position.
  and the code work now.
  thanks all for your help.
next step.... doing the accelerometer linked with the servos..... lol.... not done.....
 

Go Up