Disabling Analog Input in the middle of a programme?

Hello Everyone,

I am new to arduino and have run into a bit of an issue with my programme. Basically, I am using the analog input reading from my object sensor to move my robot towards that object. The idea is that if it detects an object it should stop and move towards it. I achieved that. However, the issue is that I want the sensor readings to be ignored once it has successfully detected an object. I thought I'd achieve this by writing a command that would disable the analog input pin that the sensor is connected to but I was not successful. Is there any way to do that?

thanks

p.s. here is my code

int sensorValue = A1;
const int pwmEnablePin = 9; //turns motor ON or OFF(ideally with output from IR sensor)
const int Motor1Pin1 = 5; //one terminal of Motor 1
const int Motor1Pin2 = 4; //second terminal of Motor 1
const int Motor2Pin1 = 6; //one terminal of Motor 2
const int Motor2Pin2 = 7; //second terminal of Motor 2
int pwm_level = 150;

void setup()
{
Serial.begin(9600);
//setting output pins
pinMode(Motor1Pin1, OUTPUT);
pinMode(Motor1Pin2, OUTPUT);
pinMode(Motor2Pin1, OUTPUT);
pinMode(Motor2Pin2, OUTPUT);
pinMode(pwmEnablePin, OUTPUT);
digitalWrite(pwmEnablePin, HIGH); //Setting this to HIGH so that it can turn on the motor
digitalWrite(Motor1Pin1, HIGH);//setting one terminal for Motor1 to HIGH so motor goes forward
digitalWrite(Motor1Pin2, LOW);//setting second terminal for Motor1 to LOW so motor goes forward
digitalWrite(Motor2Pin1, HIGH);//setting one terminal for Motor2 to HIGH so motor goes forward
digitalWrite(Motor2Pin2, LOW);//setting second terminal for Motor2 to LOW so motor goes forward
}

void loop()
{
sensorValue = analogRead(A1) ; //value from sensor
float val = sensorValue * (5.0/1024.0); //converting it into volts
Serial.println(val);
cupsense();
delay(250);
}

void cupsense()
{
float val = sensorValue * (5.0/1024.0);
if(val<0.5)
{
digitalWrite(Motor1Pin1, HIGH);//setting one terminal for Motor1 to HIGH so motor goes forward
digitalWrite(Motor1Pin2, LOW);//setting second terminal for Motor1 to LOW so motor goes forward
digitalWrite(Motor2Pin1, LOW);//setting one terminal for Motor2 to HIGH so motor goes forward
digitalWrite(Motor2Pin1, HIGH);//setting second terminal for Motor2 to LOW so motor goes forward
}
else

{
digitalWrite(Motor1Pin1, LOW); //stop car
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);
delay(1000);
//digitalWrite(Motor1Pin1, HIGH); //turn towards the cup so that the front of the car faces the cup
//digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, HIGH);

}
}

Once the object is detected, set a flag.
Only perform analogRead if the flag is not set.

if (objectgFlag == 0){
// perform analogReads
}

I don't see what part of your code decides on object detection.
Thus, I don't know when you decide to go back to detecting objects & clear the flag.

int sensorValue = A1;
const int pwmEnablePin = 9; //turns motor ON or OFF(ideally with output from IR sensor)
const int Motor1Pin1 = 5; //one terminal of Motor 1
const int Motor1Pin2 = 4; //second terminal of Motor 1
const int Motor2Pin1 = 6; //one terminal of Motor 2
const int Motor2Pin2 = 7; //second terminal of Motor 2
int pwm_level = 150;

void setup()
{
Serial.begin(9600);
//setting output pins
pinMode(Motor1Pin1, OUTPUT);
pinMode(Motor1Pin2, OUTPUT);
pinMode(Motor2Pin1, OUTPUT);
pinMode(Motor2Pin2, OUTPUT);
pinMode(pwmEnablePin, OUTPUT);
digitalWrite(pwmEnablePin, HIGH); //Setting this to HIGH so that it can turn on the motor
digitalWrite(Motor1Pin1, HIGH);//setting one terminal for Motor1 to HIGH so motor goes forward
digitalWrite(Motor1Pin2, LOW);//setting second terminal for Motor1 to LOW so motor goes forward
digitalWrite(Motor2Pin1, HIGH);//setting one terminal for Motor2 to HIGH so motor goes forward
digitalWrite(Motor2Pin2, LOW);//setting second terminal for Motor2 to LOW so motor goes forward
}

void loop()
{
sensorValue = analogRead(A1) ; //value from sensor that detects the object
float val = sensorValue * (5.0/1024.0); //converting it into volts
Serial.println(val);
cupsense();
delay(250);
}

void cupsense()
{
float val = sensorValue * (5.0/1024.0);
if(val<0.5)
{
digitalWrite(Motor1Pin1, HIGH);//setting one terminal for Motor1 to HIGH so motor goes forward
digitalWrite(Motor1Pin2, LOW);//setting second terminal for Motor1 to LOW so motor goes forward
digitalWrite(Motor2Pin1, LOW);//setting one terminal for Motor2 to HIGH so motor goes forward
digitalWrite(Motor2Pin1, HIGH);//setting second terminal for Motor2 to LOW so motor goes forward
}
else //if val>0.5 this means that an object is detected

{
digitalWrite(Motor1Pin1, LOW); //stop car
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);
delay(1000);
//digitalWrite(Motor1Pin1, HIGH); //turn towards the cup so that the front of the car faces the cup
//digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, HIGH);

}
}

Please read # 7
http://forum.arduino.cc/index.php/topic,148850.0.html