Repeat a loop n number of times and then restart the code

Hi, I need help please. I need that the loop repeats for an n amount of times and then restart again, the code alredy restart but just makes the loop once.

#include <Servo.h>

// Declaramos la variable para controlar el servo
Servo servoMotor;
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
 const unsigned long period = 10000;

void setup() {
  // Iniciamos el monitor serie para mostrar el resultado
  Serial.begin(9600);

  // Iniciamos el servo para que empiece a trabajar con el pin 9
  servoMotor.attach(9);
  
  startMillis = millis();
}

void loop() 
{
    currentMillis = millis();
 
    if (currentMillis - startMillis >= period)
    {
        startMillis = currentMillis;
        servoMotor.write(0);
        delay(3000);
        servoMotor.write(90);
        delay(1000);
        
 
    }


   

  
  }

If something repeats n amount of times, then restarts, it's gonna go back and repeat n amount of times again which means that it is just constantly repeating. Can you be more specific about what you want to achieve?

Metallor:
If something repeats n amount of times, then restarts, it's gonna go back and repeat n amount of times again which means that it is just constantly repeating. Can you be more specific about what you want to achieve?

Literally I want that the code repeats the loop n amount of times and then reset, because I'm creating a electro valve using a servo.

While your concept code will do something, using delay() with millis() timing isn’t a good idea.
The timing logic will fall apart really quickly when you want to do more, or add functionality.

A type of simple state machine would be the better approach.

I'm new in this and I don't really know how to do a simple state machine with the servo. How can I change the code? In order to achive the process I want the servo to do.

A single millis() timer, with target points where different states should occur...
This clunky pseudo-code that outlines the flow.
You need to read up, post your code as you progress, and we can steer you as needed. You’ll get it quickly!

enum states {IDLE, FORWARD, REVERSE, HOME}:
enum durations {0L, 3000L, 1000L, 1000L}:
::::
current_state = IDLE;
::::
if (millis() - started_at > duration[current_state]) {
  switch (current_state) {
    case IDLE:
      break;
    case FORWARD:
      break;
    case REVERSE:
      break;
    default: //HOME
      break;
  }
// when the current state (except for HOME) is complete...
if (current_state) < 2). // auto sequence up to state 3
  current_state++;  // or jump directly to some other state...
else 
  // manually restart, return to IDLE state ‘0’
}

You could put your loop counter where it fits best - and restart the state machie as needed.

cgaguja2802:
Literally I want that the code repeats the loop n amount of times and then reset, because I'm creating a electro valve using a servo.

That is not being more specific. Once the n number of times are up how does it decide when to do the next n number of times? Is there a button you press? Is it after a certain amount of time?
What do you mean by reset? The same as pressing the reset button on the Arduino? Stop the servo from moving and do something?
Can n change? Or is it a specific number you have in your head that you are keeping a secret from us for some reason?

For example: I want a servo to go to 0 degrees, wait 3 seconds, go to 90 degrees, wait 1 second. I want this cycle to repeat 5 times. Once this has cycled 5 times I want to (wait for a button press / wait for 10 seconds) before going through the 0 to 90 cycle again another 5 times, waiting for (button press / 10 seconds) and so on.

@OP

Passing very very hard time to understand what you have wanted to achieve for your Servo with the codes that you have posted.

1. The system (Control Program) is checking if 10 sec time has elapsed or not.

2. If 10 sec time has not elapsed, bring the Servo-shaft at 00position and stay here for 3-sec. After that bring the Servo-shaft at 900 position and maintain this position for 1 sec.

3. Practically, the Servo behaves in this way: it remains at 900 position for 3 sec and at 00 position for about 7 sec.

4. So, what you want to achieve? Do you want that the Servo should at 00 position for 1 sec and at position 900 for 3 sec? For this purpose, you use either delay() or millis() but not the mix.

OR Do you want the following due to@Metallor of Post#5?

For example: I want a servo to go to 0 degrees, wait 3 seconds, go to 90 degrees, wait 1 second. I want this cycle to repeat 5 times. Once this has cycled 5 times I want to (wait for a button press / wait for 10 seconds) before going through the 0 to 90 cycle again another 5 times, waiting for (button press / 10 seconds) and so on.

Hi, I'm new in this and I don't really know how to do a simple state machine with the servo. How can I change the code? In order to achive the process I want the servo to do.

Now would be a good time to learn.
Make a start then we’ll rip you to pieces.
If you don’t try, where’s the fun in that?
So far, I’ve made more effort at a state machine than you have.