count loop and start delay

Guys, I am trying to do (2) things and am having problems. I am no a programmer but and trying to have a servo start moving after 20 seconds and stop after (2) cycles. The following is what I am using but nothing that i do works. Any help would be appreciated

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

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

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

Well, considering that you want a 20 second delay, you should have one somewhere in the code. :slight_smile:

All

I am a newbie and found the following servo sweep program. What I need to to modify it to add a delay before it starts and have it cycle a number of times. Im not sure how to cycle it say twice also before it starts put a delay time X before the servo starts moving. Any help would be appreciated

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

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

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

IF the program works the way it currently is, just add the delay you want in the setup. If that works the way you want, come back and we will help you figure out how to put a counter in the loop.

Paul

Hi, See THIS PAGE for another example.

Note the ServoMin and ServoMax constants. Many servos do not work well near the numerical limits and cause problems and overcurrents. After you have it all working you can try moving those limits.

@StevenFox950, please do not cross-post. Threads merged.