Stuck in the loop

Hello!
I have a newbie problem. I have placed a ultrasonic sensor on a servo. I made a loop that turns the servo 120 degrees, one degree at the time, and let the ultrasonic sensor measure distance then the servo turns one more degree and led the sonar measure and so on. Once it has gone thru the loop and checked att 120 positions it knows where the target is. I then want a second servo to turn to aim and shoot the target down with a rubber band.
So then to my problem. I do not know how to end the loop where it is going thru the 120 steps so I can stop the scanning and tell the second servo to aim and shoot. :o

I have a newbie problem. I have placed a ultrasonic sensor on a servo. I made a loop that turns the servo 120 degrees, one degree at the time, and let the ultrasonic sensor measure distance then the servo turns one more degree and led the sonar measure and so on

And the beam angle of the ultrasonic sensor is . . ?

I do not know how to end the loop

I can't see any loop.

#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2);  //Create Liquid Crystal Object called LCD
#include <Servo.h>  //Load the servo Library
int pos = 0;    // variable to store the servo position
int posx = 5000;    // target servo position set large so it kan be shrunk
int servoPin= 6; //Servo is hooked to pin 9
int servoDelay=25; // 25 millisecond delay after each servo write 
int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11;  //Sensor Echo pin connected to Arduino pin 11
int myCounter=0;  //declare your variable myCounter and set to 0
int searchdist;
int searchpos;
float pingTime;  //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.
Servo myPointer;  //Create your servo object. I call mine 'myPointer'
 
void setup() {
Serial.begin(9600);
myPointer.attach(servoPin);  // attaches the servo myPointer to pin servoPin, which should be pin 6  
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:");  //Print Message on First Row
searchdist=1000;
myPointer.write(0); //set servo position to startposition;
delay (1000);
}
 
void loop() {
searching:
int pos = 0 ; //declare the variable j to be an int and set value to 1
while (pos<=120) {     //start your while loop
  myPointer.write(pos); //set servo position to position
  delay(servoDelay);                       // waits 15ms for the servo to reach the position
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(50); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state


  pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
  pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
  targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
  targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
  targetDistance= targetDistance*160934;    //Convert miles to inches by multipling by 63360 (inches per mile)
  
  if (searchdist>targetDistance) posx=pos; //posx will be the best angle to shoot
  if (searchdist>targetDistance) searchdist=targetDistance;
    
  LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" centimeters");  //Print your units.
  delay(300); //pause to let things settle
  pos=pos+2;              // increment pos by 1


// if I could get out of this loop I would do "myPointer.write(posx);"
}}

Moderator edit: code tags. Why is it so difficult?

I read some of your comments.
You should either scrap them or stop believing them.

int pos = 0 ; //declare the variable j to be an int and set value to 1
delay(servoDelay);                       // waits 15ms for the servo to reach the position
pos=pos+2;              // increment pos by 1
targetDistance= targetDistance*160934;    //Convert miles to inches by multipling by 63360 (inches per mile)

To get out of your loop, you could try "break;"

Given you an example below on how to escape your while loop. Sure you can doctor it to your needs:

int pos = 0;
while( pos <= 120 )
{
 pos++;

 // We want to escape this loop at 98 degrees!
 if( pos == 98 )
 {
 break;
 }
}

// After break is hit, code carries on executing from here ...

do not know how to end the loop where it is going thru the 120 steps so I can stop the scanning

Are you sure that you want to prevent it scanning all 120 positions ? How will you know that you have reached the target position and need to stop ? Surely it would be better to do a full scan and then to determine where to aim at.

I want it to scan all 120 positions, and it does. But then I want to breake the loop, aim and shoot

New_to_Arduino:
I want it to scan all 120 positions, and it does. But then I want to breake the loop, aim and shoot

Then you don't want it to break the loop, you want it to loop completely and store off the best angle at which to fire, then after the loop ends, this is the position you choose.

Yes this is what it does. "posx" is dirived from the scanning but I cant break the loope so I can direct the servo to go to that possition. I am realy a newbie

//Thanks for all the help! But it is still looping after it has scanned the120 points

#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2); //Create Liquid Crystal Object called LCD
#include <Servo.h> //Load the servo Library
int pos = 0; // variable to store the servo position
int posx = 0; // target servo position
int servoPin= 6; //Servo is hooked to pin 6
int servoDelay=25; // 25 millisecond delay after each servo write
int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
int myCounter=0; //declare your variable myCounter and set to 0
int searchdist;
int searchpos;
float pingTime; //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.
Servo myPointer; //Create your servo object. I call mine 'myPointer'

void setup() {
Serial.begin(9600);
myPointer.attach(servoPin); // attaches the servo myPointer to pin servoPin, which should be pin 6
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:"); //Print Message on First Row
searchdist=1000;
myPointer.write(0); //set servo position to startposition;
delay (1000);
}

void loop() {
searching:
int pos = 0 ; //declare the variable pos to be an int and set value to 1
while( pos <= 120 )
{
pos++;

myPointer.write(pos); //set servo position to position
delay(servoDelay); // waits for the servo to reach the position
digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(50); //Let signal settle
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in high state

pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance= targetDistance*160934; //Convert miles to centimeters

if (searchdist>targetDistance) posx=pos;
if (searchdist>targetDistance) searchdist=targetDistance;

Serial.print (pos);
Serial.print (" ");
Serial.print (targetDistance);
Serial.print (" ");
Serial.println (posx);

LCD.setCursor(0,1); //Set cursor to first column of second row
LCD.print(" "); //Print blanks to clear the row
LCD.setCursor(0,1); //Set Cursor again to first column of second row
LCD.print(targetDistance); //Print measured distance
LCD.print(" centimeters"); //Print your units.
delay(300); //pause to let things settle
pos=pos+2; // increment pos by 2

// We want to escape this loop at 98 degrees!
if( pos == 98 )
{
break;
}
}}

myPointer.write(posx); //aim the servo on the target

//Thanks for all the help! But it is still looping after it has scanned the120 points

Yes it will, loop() does just that!

What is this for?

searching:

A label for a goto? Don't use those.

Try this, no idea if your while loop works.

int g_state = 0;
void loop() 
{
	switch( g_state )
	{
		case 0:
		{
			int pos = 0 ; //declare the variable j to be an int and set value to 1
			while (pos<=120) 
			{
				myPointer.write(pos); //set servo position to position
				delay(servoDelay);                       // waits 15ms for the servo to reach the position
				digitalWrite(trigPin, LOW); //Set trigger pin low
				delayMicroseconds(50); //Let signal settle
				digitalWrite(trigPin, HIGH); //Set trigPin high
				delayMicroseconds(15); //Delay in high state
				digitalWrite(trigPin, LOW); //ping has now been sent
				delayMicroseconds(10); //Delay in high state

				pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
				pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
				pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
				targetDistance= speedOfSound * pingTime;  //This will be in miles, since speed of sound was miles per hour
				targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
				targetDistance= targetDistance*160934;    //Convert miles to inches by multipling by 63360 (inches per mile)
  
				if (searchdist>targetDistance) 
					posx=pos; //posx will be the best angle to shoot
				if (searchdist>targetDistance) 
					searchdist=targetDistance;
    
				LCD.setCursor(0,1);  //Set cursor to first column of second row
				LCD.print("                "); //Print blanks to clear the row
				LCD.setCursor(0,1);   //Set Cursor again to first column of second row
				LCD.print(targetDistance); //Print measured distance
				LCD.print(" centimeters");  //Print your units.
				delay(300); //pause to let things settle
				pos=pos+2;              // increment pos by 1

				// if I could get out of this loop I would do "myPointer.write(posx);"
			}

			g_state++;
		}
		break;
		case 1:
		{
			myPointer.write(posx); //aim the servo on the target
			g_state++;
		}
		break;
		case 2:
		{
			// if and when I want to rescan, we set g_state = 0 ...
		}
		break;
	}
}

Please read Nick Gammon's two posts at the top of this Forum for guidelines on posting here, especially the use of code tags ("</>") when posting source code. The tags help to prevent the source code from being mangled when displayed.

New_to_Arduino:
I want it to scan all 120 positions, and it does. But then I want to breake the loop, aim and shoot

Can you please describe what should happen. To me it seems logical to do the following

start of loop() function
  wait for user input to start scanning
  scan all positions and save distances for each of them
  use the results to determine where to aim
  aim
  fire
end of loop() function

If you really only want to do this once and will repeat the process by resetting the Arduino or powering it on and off then put the code in the setup() function and don't worry about waiting for user input. Have nothing in the loop() function so there is nothing to exit from.