How to add start stop function for servo motor in below code.

Hi,
Below is simple program for servo motor to swing 0-90 digree in continuous form in loop. But I want to stop the servo motor after 10 cycles count.
Please share your comments if anyone knows.

#include<Servo.h>
Servo servo1;
int pos=0;

void setup() {
servo1.attach(9); // servo motor is connected to pin # 9
}

void loop() {
for (pos = 0; pos < 90; pos += 1)
{
servo1.write(pos);
delay(300);
}

for (pos = 90; pos < 0; pos -= 1)
{
servo1.write(pos);
delay(300);
}

}

But I want to stop the servo motor after 10 cycles count.

So, use a for loop, and move the code to setup().

shailmanin:
Below is simple program for servo motor to swing 0-90 digree in continuous form in loop. But I want to stop the servo motor after 10 cycles count.

If you only want it to do a single group of 10 movements then @PaulS's answer is a simple solution.

If you want it to do 10 movements every time you press a button then you need a variable to count the movements, Set the variable to 0 when the button is pressed. Don't make any more movements when the count gets to 10.

...R

Set the variable to 0 when the button is becomes pressed.

Thank you for your greater precision @PaulS

...R

Thanks for suggestion.
I am seeing couple of examples which point towards usage button or remote control connection to control the servo motor.
But I need to use break or stop to stop the servo motor after some time without using any other device.

shailmanin:
But I need to use break or stop to stop the servo motor after some time without using any other device.

Did you read Reply #2?

...R