Pages: 1 [2]   Go Down
Author Topic: Servos Delay  (Read 2673 times)
0 Members and 1 Guest are viewing this topic.
Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

captain-slow.dk | non contagious!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
pos = 180;
myservo.write(pos);
delay(22000);

Or maybe make it even shorter and just say.
Code:
myservo.write(180);
delay(22000);
Because the "pos" is set to 180 in the next loop anyways.
Logged

captain-slow.dk | non contagious!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See reply #10  ;-)
Logged

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

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 9
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

See reply #10  ;-)
lol yeah... smiley-razz

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:
#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...
Logged

captain-slow.dk | non contagious!

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: