Check the range of infrared sensor

Hello,

I am developing a project that involves the identification of obstacles with infrared sensor (Sharp GP2Y0A21YK) and a buzzer to make noise when an obstacle approaches. My doubt is how can I check the range of an identified object? I want to check if the value of this range is 80cm then the buzzer starts the noise.

Here is my code:

int Value, IR; int Pin=8; int Note=1500;

void setup(){ pinMode(0, INPUT); }

void loop(){ IR = analogRead(0); Value = map(IR, 0, 800, 400, 0);

tone(Pin, Note, Value);

int Time = Value * 1.2; delay(Time);

noTone(Pin); }

Thanks

Hello and welcome,

Description: Infrared proximity sensor made by Sharp. Part # GP2Y0A21YK has an analog output that varies from 3.1V at 10cm to 0.4V at 80cm

AnalogRead return a value between 0 and 1023, corresponding to voltage between 0 and 5V, so the value for 0.4V (80cm) should be: 1023/50.4 = ~82, and for 10cm: 1023/53.1 = ~634

if ( IR >= 82 && IR <= 634 )
  // object is in range..

Maybe this will help?

It worked! Thank you

The final code:

int Value, IR;
int Pin=8;
int Note=1500;

void setup(){
pinMode(0, INPUT);
Serial.begin(9600);
}

void loop(){
IR = analogRead(0);

Serial.print(IR);

Value = map(IR, 0, 800, 400, 0);

if(IR >= 82 && IR <= 634){
tone(Pin, Note, Value);
}

int Time = Value * 1.2;
delay(Time);

noTone(Pin);
}