How to add timing to servo motor?

Hi Everyone!
I am very new to all of this and am trying to accomplish a small project for school. I’ve been reading forums, articles, and watching hours of Youtube and learning all that I can to make my project work, to no avail. Hoping someone can help!

My goal: Using the Arduino Leonardo with a Radioshack Standard Servo Motor connected to window blinds I want to open and close the blind flaps (not pull up and down, merely open and close flaps) at what appears to be different intervals in time.

Everything is hooked up and working fine for the standard Servo Sweep code that comes in the Arduino Library. The flaps open and close wonderfully, but I cannot figure out how to stop them from opening and closing, or to delay them so it appears more random. I do not want a continuous open/close, open/close…

I have tried changing the delay and it continues to run in the same pace.
I have tried the standard Servo Sweep and also the code below.
Thanks in advance! If I am trying to do something that just isn’t plausible, please let me know. Ignorance is not bliss, but hundreds of hours of futile attempts. :stuck_out_tongue:

#include <Servo.h>

Servo myservo;
void setup()
{
myservo.attach (9);
}
void loop()
{
myservo.writeMicroseconds (1000);
delay (500);
myservo.writeMicroseconds (1000);
delay (500);
myservo.writeMicroseconds (3000);
delay (300);
}

Welcome....

Couple of things:

  • Are those delays of 300 and 500ms long enough for the open or close to complete? Once the servo.write command has been issued, the delay() will kick in, but the servo might still physically be moving. Nett effect might be that it seems like no delay took place before the next servo.write
  • Why do you have 2 consecutive servo writes of 1000?

You can use the RANDOM(min,max) function for the delay(). Make sure that the "min" value will be enough for the servo to complete his move.

Hi! Thanks so much for the response! The window flaps open and close with those delays, they work great as far as I can tell (open fully, then close fully). I was just playing with the code to see what would happen with a repeated servo write, but it doesn't seem to make a difference. A newbie experiment which likely doesn't make sense to anyone who really understands the system. I also tried really long delays, 9000, etc. and it made no difference.

JimboZA: Welcome....

Couple of things:

  • Are those delays of 300 and 500ms long enough for the open or close to complete? Once the servo.write command has been issued, the delay() will kick in, but the servo might still physically be moving. Nett effect might be that it seems like no delay took place before the next servo.write
  • Why do you have 2 consecutive servo writes of 1000?

Thanks! It sounds like a good idea, I'm not entirely sure how to use it. I read the link you provided, thank you. Would I just put this in instead of the delay? Sorry I am so new to this!

YohiZucker: You can use the RANDOM(min,max) function for the delay(). Make sure that the "min" value will be enough for the servo to complete his move.

jennifishv: I also tried really long delays, 9000, etc. and it made no difference.

On the face of it, the delay() should work. As long as it's longer than the travel time of the servo, it should be visible. I'm about to leave for work (0430 here....) so I'll test that tonight if you still have the problem.

Regarding the use of random(), you don't replace delay() with it no, but use random() to set the value inside the delay().

Let's say you want the delay to be a minimum of 500 to allow the servo to move, but up to say 10 seconds. Then you would go:

delay(random(500, 10000));

There, random(500,10000) comes up with a value, let's say it's 3456 this time through. That's as if you had written:

delay(3456);

Ah, very interesting. That makes sense. I pulled everything apart and am starting all over, will try this and see what happens.

JimboZA: On the face of it, the delay() should work. As long as it's longer than the travel time of the servo, it should be visible. I'm about to leave for work (0430 here....) so I'll test that tonight if you still have the problem.

Regarding the use of random(), you don't replace delay() with it no, but use random() to set the value inside the delay().

Let's say you want the delay to be a minimum of 500 to allow the servo to move, but up to say 10 seconds. Then you would go:

delay(random(500, 10000));

There, random(500,10000) comes up with a value, let's say it's 3456 this time through. That's as if you had written:

delay(3456);

Well, no luck here. I must be doing something fundamentally wrong, I’ve tried the delay with the RANDOM and no change. I tried another servo motor, tried powering the servo off a separate power source than the Arduino, tried adding a potentiometer and controlling it with Knob code from Arduino. No matter what I do the servo just continuously goes back and forth.

Is there a glaring mistake I could be making as a newbie?

@JennifishV: Four years on so I’m hoping you’re still around and that you eventually found a solution? I have almost exactly the same problem and found this thread while researching. It seems a fairly common one from novice users of servos with Arduino like me.

In short, how to control what in end user terms I see as the key variables associated with servo operation:

  1. First angle of rotation (or its finishing position)
  2. Speed of that movement (or its duration)
  3. Delay before next movement (or how long it remains static)
  4. Angle of that
  5. Speed of that Etc.