Go Down

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

May 26, 2011, 10:21 pm Last Edit: May 27, 2011, 10:54 pm by christophe31 Reason: 1
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

wildbill

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.

AWOL

Your description says 25 seconds, the code suggests 20 seconds (though it's in the wrong place).
"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.

#3
May 27, 2011, 10:50 pm Last Edit: May 27, 2011, 10:52 pm by christophe31 Reason: 1
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

AWOL

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

zoomkat

Is something like below what you are looking for?

Code: [Select]

#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);
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.


Is something like below what you are looking for?

Code: [Select]

#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

tomly

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
}

johnwasser


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

#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..."
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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
  }

AWOL

Code: [Select]
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
Code: [Select]
void loop()
{
    myservo.write(0);             
    delay(28000);                       
    myservo.write(180);
    delay(28000);
 
"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.

ok i'll try this one too.
thanks

#12
Jun 02, 2011, 10:34 am Last Edit: Jun 02, 2011, 01:28 pm by christophe31 Reason: 1
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

AWOL

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

jraskell

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

Go Up