How do i stop a loop?

How do i stop a loop once it finishes. Also how do i use the serial monitor to move a servo? I want to input a “Int” into it and i want it to run the code i have set up. So lets say i type in “It1” then i want it to run the code that i have down at the bottom.

\#include<Servo.h>

Servo servo1;
int It1 = 120;
int It2 = 45;
int It3 = 0;


void setup() {
 
Serial.begin(9600);
servo1.attach(4);
servo1.write(90);
}

void loop() {

servo1.write(It1);
delay(1000);
servo1.write(180);
delay(1000);
servo1.write(90);
delay(1000);
servo1.write(It2);
delay(1000);
servo1.write(180);
delay(1000);
servo1.write(90);
delay(1000);
servo1.write(It3);
delay(1000);
servo1.write(180);
delay(1000);
servo1.write(90);
delay(1000);



}

Create a separate servo function, place your code inside.
Use parseInt() to get servo position if there is a new one.
If there isn’t a new position check to see if a new position has been received.
If there is a new position, call your servo function.

Itz_Jack:
How do i stop a loop once it finishes

Erm, if its finished you don't need to stop it...

Anyway you seem to be confusing the function called "loop" with a loop.

The function is called from a loop in a piece of code you can't see (unless you look in the system
sources, which you can do it you want, its in main.cpp).

In loop you place all your tests for things that need to happen - its an "event loop", you just
need to ensure everything that needs to be checked regularly is checked in loop() function body

loop() gets called repeatedly forever, so if you want something to happen only under some sort
of condition, you need to program that condition.

If you want something to happen only at startup, do it from setup(), not from loop()

use this:

for(;;) {


}

It is called an infinite ‘for’ loop. What it does is continually loops whatever is in the infinite for loop thus disallowing anything from within the main loop to run. If there is nothing in the for loop, then it will infinitely do nothing.

– DH

As for controlling a servo from the serial monitor, Robin2 has a great post on that.

This thread will explain anything you will ever need to know about the serial monitor.

-- DH

Do you mean to say you want the program to wait until a serial command is received, then run that servo sequence once, then wait for the next command?