using ir sensor and servo sweep question

i am having trouble running these two devices together
how can i run the sensor to check for something (in its script i have it averaging the value to watch out for spikes)
and on the servo side i want it to continuously sweep until something appears infront of it, at that point i want it to light up an led for now.
i have tried multiple ways of doing this but keep running into the same issues. either the sensor checks after the servo finishes its cycle which is not good. another way i put the sensor check code inside the servo code which made it move very slowly and did not stop the servo.
and the last way i added a if statement to the servo code which made it stop at the appropriate place and do what its supposed to do, but i couldnt have it average the distance.

so what can i do?

this way my initial script

void loop() {
  do {
    digitalWrite(led, LOW);
    spin();
  distance1 = cm;
  delay(30);
  distance1 = cm;
  delay(30);
  distance1 = cm;
  delay(30);
  distance1 = ((distance1 + distance2 + distance3)/3);
   Serial.print("Cm: ");
  Serial.println(cm);
  Serial.print("IR value: ");
  Serial.println(sensorValue);
  }
  while(distance1 < boundary);
  digitalWrite(led, HIGH); }
  delay(500);
  } 
void irsensor() {
  sensorValue = analogRead(sensorIR);
  //inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  //cm = 15213.8 * pow(sensorValue,-0.95) - 10;
  cm = 10650.08 * pow(sensorValue,-0.935) - 10;
  delay(500);
  Serial.print("Cm: ");
  Serial.println(cm);
  Serial.print("IR value: ");
  Serial.println(sensorValue);
}
void spin() {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 170; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
}

and this is the one with the if statement. its a bit messy cause i was just cutting and pasting around till something worked

void loop() {
    digitalWrite(led, LOW);
    spin();
} 

void irsensor() {
  sensorValue = analogRead(sensorIR);
  //inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  //cm = 15213.8 * pow(sensorValue,-0.95) - 10;
  cm = 10650.08 * pow(sensorValue,-0.935) - 10;
 
}
void spin() {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    irsensor();
    Serial.println(cm);
    delay(10);        
if(cm < 50) { 
 digitalWrite(led, HIGH); 
  delay(2000);
}  // waits 15ms for the servo to reach the position 
  } 
  for(pos = 170; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    irsensor();
    Serial.println(cm);
    delay(10);
if(cm < 50) { 
 digitalWrite(led, HIGH); 
  delay(2000);
}    // waits 15ms for the servo to reach the position 
  } 
}

thx

In the first code, spin() just waves the servo around. It does not cause the sensor that it is presumably carrying to take readings.

Function names should have a noun and a verb, like digitalRead(), analogWrite(), etc. If there is no noun, like Serial.begin(), the thing to be operated on is implied (the serial port). A name like irsensor() does not define what the function does.

In that function, it appears as though you are taking a reading, waiting an eternity, then printing the value. Why are you waiting? Not that it really matters, since you never call the function, anyway.

In the second sketch, you do call the function, which still has the lousy name, which is bad, but no longer delays, which is good.

}  // waits 15ms for the servo to reach the position

That comment is completely useless at that point, not to mention wrong.

What is it you are trying to do? Do not describe it in terms of the code that you have, but in terms of the project that the code is for.

why is it nessesary for it to have a noun and a verb? unless u mean just so its easier to understand?

what i want to do for the project is to have the ir sensor to take readings WHILE the servo is spinning and then when it detects something, to stop and then figure out to go either left or right based where it stopped.
but right now what i was trying to figure out is how to make them both run at the same time. but the delays of the sensor to not affect the speed of the servo because i wanted the delays to take an average because im assumming there going to be a bumpy ride on my robot.

why is it nessesary for it to have a noun and a verb? unless u mean just so its easier to understand?

Isn't that a good enough reason? What does the function twoleftshoes() do? How about readSerialData()?

what i want to do for the project is to have the ir sensor to take readings WHILE the servo is spinning and then when it detects something, to stop and then figure out to go either left or right based where it stopped.

You really need to get rid of the for loops. You need to move the servo, take a reading, move the servo take a reading, etc. If the second reading is higher than the first, that tells you one thing. If the second reading is lower than the first, that tells you something else. If the readings are the same, that tells you a third thing.

What you do in those three cases is up to you.

Moving the servo in 1 degree increments is probably not a good idea. Some larger steps are probably more useful.

but the delays of the sensor to not affect the speed of the servo because i wanted the delays to take an average because im assumming there going to be a bumpy ride on my robot.

Delays are almost never a good idea. If you need to average readings, you need to take the readings close together, then move to the next position, and do it again. I don't think that you need to do that, though. Look one direction. Take a reading. Look another direction. Take a reading. Make a decision based on the relative values read, which may include looking somewhere else.

If one (or both) of the readings is bad, and you make a wrong move decision, that will be corrected on the next iteration.

thanks for your replies. good stuff.
i am begginer so i got the servo code from the included arduino examples. how else should i make the servo move in increments without using a 'for' loop?

i am begginer so i got the servo code from the included arduino examples. how else should i make the servo move in increments without using a 'for' loop?

The first thing to consider is when to move the servo. You're driving a car, headed down the road. Where are you looking? Straight ahead, mostly. Your robot should be doing the same thing. Only when there is an obstacle in the way does it need to look in a different direction. So. move the servo some amount, just using a simple servo.write() to define a new position. Say 10 degrees in one direction. How far away is the nearest obstacle in that direction? Then, make it look the other way. Say 10 degrees on the other side. How far away is the nearest obstacle in that direction? Turn the robot in the direction where the obstacle is farthest away, and move the servo back to the middle. Repeat as needed. Slow, stop, back up as needed, all based on three distances.

You can decide how far to turn on each side.

If the idea is not to drive a robot, there are other looping mechanisms - while and do/while come to mind - and there is nothing wrong with using a for loop. You can always break out of the loop is needed.

Just don't think that is always necessary to complete a loop. If conditions change, it is fine to break out of the loop.