Hi! Thank you in advance for your help! I am extremely new to programming, Arduino, electronics, etc.
I am building a fake cuckoo clock for a holiday display. My bird sits on an arm connected to a servo motor. I can get it to sweep back and forth to the right positions using the Sweep Sketch.
But I also need it do a couple more things... It needs to sweep back and forth 6 times and then stop for 30 min, then repeat over and over again. I cannot figure out how to modify the Sweep Sketch to get it to sweep back and forth only six times or to pause for any length of time. (My understanding is that the delay command in the sweep sketch just controls how quickly it gets to position, but does not hold it in position for any amount of time.)
Any code suggestions that I could play with that would help with pausing the servo or looping for a set number of times would be very much appreciated!!!
I cannot figure out how to modify the Sweep Sketch to get it to sweep back and forth only six times
Look up how to use a "for" loop. See the language reference under the help menu of the IDE.
or to pause for any length of time
If you don't want to do anything else during this pause, simply use the delay function call. Again see the language reference under the help menu of the IDE.
Understand how to use a "for loop". You're already using Sweep which uses for loops to move the servo for 180 steps. You could easily add another loop outside all that to do the whole thing 6 times.
And when it's finished it's 6 sweeps another delay() at the end would make it pause for some time and then repeat the whole thing.
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
}
delay (10000);
}
Would my 'for' statement about the number of times to repeat go above or below 'void loop() {' Do I write it as an additional void loop statement or can there only be one in a sketch?
void loop() {
for (byte times = 0;times < 6; times++) // <<<<new
{
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
}
delay (10000);
} // <<<<new
delay(1800000UL); // 30 minutes <<<<new