How to stop ultrasonic sensor when i get a sufficient number of distance values

Hi,I have been using arduino uno and an ultrasonic sensor(HC-SR04) that is connected to a servo motor.the servo moves from angle 55 to 125 during its rotation the ultrasonic sensor should take only 4 values to an array,not the same values but different.But when i am taking the values the array elements are exceeding.How can i take only 4 values of different .And how can I make the sensor to take reading once it gives the sufficient values.The below is code

#include<Servo.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,11);

#define sizee 4
#define trigPin 12
#define echoPin 13

Servo myServo[4];
long duration;
float distance = 0.0;
int servoAngle[8];
int Angle = 55;
float retDistance;
int pwmPin[6] = {3, 5, 6};
float xVal = 0.0,yVal = 0.0;
float x[4] = {0.0,0.0,0.0,0.0};
float y[4] = {0.0,0.0,0.0,0.0};
int angle[4] = {};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
myServo[3].attach(9);
myServo[3].write(Angle);
servoAttach();
// homePosition();
servoDetach();
}

float distanceSense() {
distance = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2;
//Serial.println(distance);
return distance;
}

void servoAttach()
{
for (int i = 0; i < 3; i++)
{
myServo_.attach(pwmPin*);_
_
}_
_
}_
void servoDetach()
_
{_
_
for (int i = 0; i < 3; i++)_
_
{_
_ myServo.detach();
}
}
void reverseSweep()
{
for (int i = 125; i >= 55; i–)
{
myServo[3].write(i);
delay(25);
}
}
void loop() {
Angle = myServo[3].read();
SerialSend();
Angle++;
myServo[3].write(Angle);
if (Angle == 125)
reverseSweep();
delay(100);*_

}
float xval() {
* Angle = myServo[3].read();*

* retDistance = distanceSense();*
* if(retDistance < 50)*
_ xVal = cos(0.0174533 * (180 - Angle)) * retDistance;
* return xVal;*_

}
float yval() {
* Angle = myServo[3].read();*

* retDistance = distanceSense();*
* if(retDistance < 50)*
_ yVal = (sin(0.0174533 * (180 - Angle)) * retDistance) + 6.5;
* return yVal;
}
void SerialSend() {
for(int j = 0;j<sizee;j++){
if (x[j] == 0.0)
// x[j] = xval();*_

* x[j] = xval();*

/* if(x[1] == x[0])
* x[1] = xval();*
* else if(x[2] == x[3] or x[2] == x[1] or x[2] == x[0])*
* x[2] = xval();*
* else if(x[3] == x[2] or x[3] == x[1] or x[3] == x[0])*
* x[3]= xval();*
_ delay(100);/
y[j] = yval();
delay(100);
/ Serial.print(“x0”);

Serial.println(x[0]);
* Serial.print(“x1”);
Serial.println(x[1]);_

_Serial.println(x[2]);/_

* //Serial.println(y[j]);*
* float dis[4],dis1[4],dis2[4],dis3[4];*

* for(int i =0;i<4;i++) {*

dis = sqrt(sq(x[0] - x_)+sq(y[0]-y*));
delay(10);
dis1 = sqrt(sq(x[1] - x)+sq(y[1]-y));
delay(10);
dis2 = sqrt(sq(x[2] - x)+sq(y[2]-y));
delay(10);
dis3 = sqrt(sq(x[3] - x)+sq(y[3]-y));
//Serial.print(dis);
Serial.println(“A,” +String(dis[0]) + “,” + String(dis1[0]) + “,” + String(dis2[0]) + “,” + String(dis3[0]) + “,” + String(dis[1]) + “,” + String(dis1[1]) + “,” + String(dis2[1]) + “,” + String(dis3[1]) + “,” + String(dis[2]) + “,” + String(dis1[2]) + “,” + String(dis2[2]) + “,” + String(dis3[2]) + “,” +String(dis[3])+","+String(dis1[3])+","+String(dis2[3])+","+String(dis3[3])+",");
}
}
}
void Serialrec() {
int j = 1;
int valfinal[8];
String readData = “”;
readData = Serial.readString();
Serial.println(readData);
if (readData[0] == ‘M’)
{
String valfinal = “”;
for (int i=j;i<readData.length();i++)
{
if(readData = ‘,’)
{
j = i + 1;
break;
}
valfinal += readData;
}
int val = valfinal.toInt();
valfinal[8] = val;
Serial.println("<c," +String(valfinal[2] +“c”));
//Serial.println("<c," +String(valfinal[2] +“c”));
delay(10);
}
}*_

If you take a close look at your post you probably see why we always insist in using code tags. Please edit your post and add code tags to make it readable.

The answer to "how to not take more than x readings" is normally: stop taking more readings when you have what you need.

Code tags indeed would make the code readable. More detailed explanation from you would also be helpful.