Help with Ir sensor programming

Hello everyone! I am having trouble with coding. It has been 5 years, since I had programmed. I have made a ir sensor setup. The goal is to be able to control distance when the ir sensor senses an object, so that a servo runs for a programmed amount of time.

  1. Servo runs till object is detected.
  2. After object is detected servo code runs for a certain time, then stops.
  3. Buzzer goes off to signal code is complete.

This is what I have so far:
It runs the servo when it does not detect an object. It is running reverse of what I want.
Thank you!!!

// --------------------(START)--------------------

// (LIBRARIES)
// (SERVO) ----------------------------------------------------
#include <Servo.h>
Servo myServo; // Create Servo object to control the servo

// (CONSTANTS)
// (IR SENSOR)-------------------------------------------------
int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object
int value[10]; // variable to store the IR values
int distance; // variable that will tell if there is an obstacle or not
// (BUZZER) ----------------------------------------------------
int speakerPin = 8;
// (BUZZER)CODE FOR TONE AND PACE ------------------------------
int length = 15; // the number of notes
char notes = "a a ab a a ac aa "; // a space represents a rest
int beats = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 100;

// (VOID PLAYTONE)
// (BUZZER) ---------------------------------------------------
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
// (BUZZER) ----------------------------------------------------
void playNote(char note, int duration) {
char names[] = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’ };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones
, duration);_
_
}_
_
}_
_
}*_
// (VOID SETUP)
void setup() {

* // (IR SENSOR)------------------------------------------------*
* Serial.begin(9600); // initializing Serial monitor*
* pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2*
* digitalWrite(IRemitter,LOW);// setup IR LED as off*
* pinMode(11,OUTPUT); // buzzer in digital pin 11*
* // (SERVO) ----------------------------------------------------*
* myServo.attach(9); // Servo is connected to digital pin 9*
* // (BUZZER) ---------------------------------------------------*
* pinMode(speakerPin, OUTPUT);*
}
// DRIVING CODE FOR SERVO, BUZZER -------------------------------
// (SERVO) 1300/Clockwise 1500/Stop 1700/Counter-Clockwise ----
// 1287.5536480687/@46.6RPM/360 degrees -------------------------
// (VOID LOOP)
void loop()
{
* // (IR Sensor) ------------------------------------------------*
* // HIGH = ON LOW = OFF*
* distance = readIR(5); // calling the function that will read the distance and passing the “accuracy” to it*
* Serial.println(distance); // writing the read value on Serial monitor*
* buzzer(); // buzzer function*
*} *
* int readIR(int times){*
* for(int x=0;x<times;x++){ *
* digitalWrite(IRemitter,LOW); // turning the IR LEDs off to read the IR coming from the ambient*
* delay(1); // minimum delay necessary to read values*
* ambientIR = analogRead(IRpin); // storing IR coming from the ambient*
* digitalWrite(IRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle*
* delay(1); // minimum delay necessary to read values*
* obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle*
* value[x] = ambientIR-obstacleIR; // calculating changes in IR values and storing it for future average*
* }*

* for(int x=0;x<times;x++){ // calculating the average based on the “accuracy”*
* distance+=value[x];*
* }*
* return(distance/times); // return the final value*
}
//-- Function to sound a buzzer for audible measurements --//
void buzzer(){
* if (distance>1){*
* if(distance>100){ // continuous sound if the obstacle is too close*
* digitalWrite(11,HIGH);*
* }*
* else{ // beeps faster when an obstacle approaches*
* digitalWrite(11,HIGH);*
* delay(150-distance); // adjust this value for your convenience*
* digitalWrite(11,LOW);*
* delay(150-distance); // adjust this value for your convenience*
* }*
* }*
* else{ // off if there is no obstacle*
* digitalWrite(11,LOW);*

* // (SERVO) ----------------------------------------------------*
* myServo.writeMicroseconds(1300); *
_ delay(1287.55364806871); // wait (time 1000=1sec)_
_
myServo.writeMicroseconds(1500);_
_
delay(1000);*_

* // (SERVO) ---------------------------------------------------*
* myServo.writeMicroseconds(1700); *
_ delay(1287.55364806871); // wait (time 1000=1sec)_
_
myServo.writeMicroseconds(1500);_
_
delay(1000);*_

* // (BUZZER) --------------------------------------------------*
* for (int i = 0; i < length; i++) {*
_ if (notes == ’ ') {
delay(beats * tempo); // rest
* } else {_
playNote(notes, beats _ tempo);
* }
// pause between notes*

* delay(tempo / 2);
}
while(1) {}*_

_ // *(RUN LOOP N TIMES)
}
}
// --------------------END-------------------*_

Start here