break code in loop

Hello all

I would like to have the following code run three time then stop. How would I change this?

#include <Servo.h>

Servo servoRight;         
Servo servoLeft;

void setup()
{ 
  servoLeft.attach(4);  // Left Servo is connected to digital pin 4
  servoRight.attach(5);  // Right Servo is connected to digital pin 5
} 

void loop() 
{ 

  servoLeft.writeMicroseconds(1300);  // clockwise
  servoRight.writeMicroseconds(1700);
  delay(2000);                      // Wait 2 seconds
  servoLeft.writeMicroseconds(1700);  // Counter clockwise
  servoRight.writeMicroseconds(1300);
  delay(2000);
  servoLeft.writeMicroseconds(1550);  // Stop
  servoRight.writeMicroseconds(1550);
  delay(2000);
 
}

Use if, and a variable that starts out as 0, and which is incremented.
When the variable is < 3 the if statement is true and your statements will execute.
When the variable is => 3, your statements won’t execute.

Put the code in a for loop (for(int n = 0; n < 2; n++)) and put that in setup so the code will go through 3 iterations and stop. Just leave loop() empty as in void loop() {}.

Thank you for your reply

I tried it and it only ran one time. This is what I have.

#include <Servo.h>

Servo servoRight;         
Servo servoLeft;

void setup()
{ 
  servoLeft.attach(4);  // Left Servo is connected to digital pin 4
  servoRight.attach(5);  // Right Servo is connected to digital pin 5
   {
  for(int n = 0; n < 2; n++);
  
    servoLeft.writeMicroseconds(1300);  // clockwise
    servoRight.writeMicroseconds(1700);
    delay(2000);                      // Wait 2 seconds
    servoLeft.writeMicroseconds(1700);  // Counter clockwise
    servoRight.writeMicroseconds(1300);
    delay(2000);
    servoLeft.writeMicroseconds(1550);  // Stop
    servoRight.writeMicroseconds(1550);
   }
} 

void loop() 
{  
}

You have a ; at the end of the for statement.

And the body of the for loop needs to be in curly brackets.

#include <Servo.h>

Servo servoRight;         
Servo servoLeft;

void setup()
{ 
  servoLeft.attach(4);  // Left Servo is connected to digital pin 4
  servoRight.attach(5);  // Right Servo is connected to digital pin 5
   
  for(int n = 0; n < 2; n++)
  {
    servoLeft.writeMicroseconds(1300);  // clockwise
    servoRight.writeMicroseconds(1700);
    delay(2000);                      // Wait 2 seconds
    servoLeft.writeMicroseconds(1700);  // Counter clockwise
    servoRight.writeMicroseconds(1300);
    delay(2000);
    servoLeft.writeMicroseconds(1550);  // Stop
    servoRight.writeMicroseconds(1550);
   }
} 

void loop() 
{  
}

I made that change but it still only runs once.

That worked. Thank you all!

To run the for loop 3 times it should be for(int n = 0; n < 3; Sorry, my bad.