Go Down

### Topic: Check the range of infrared sensor (Read 713 times)previous topic - next topic

#### BrnTsw

##### Nov 11, 2012, 01:15 am
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(){
Value = map(IR, 0, 800, 400, 0);

tone(Pin, Note, Value);

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

noTone(Pin);
}

Thanks

#### guix

#1
##### Nov 11, 2012, 01:31 amLast Edit: Nov 11, 2012, 03:20 am by guix Reason: 1
Hello and welcome,

Quote
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/5*0.4 = ~82, and for 10cm: 1023/5*3.1 = ~634
Code: [Select]
`if ( IR >= 82 && IR <= 634 )  // object is in range..`

Maybe this will help?

#### BrnTsw

#2
##### Nov 11, 2012, 01:20 pm
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(){

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);
}

Go Up

Please enter a valid email to subscribe