Pages: [1]   Go Down
Author Topic: Check the range of infrared sensor  (Read 374 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if ( IR >= 82 && IR <= 634 )
  // object is in range..

Maybe this will help?
« Last Edit: November 10, 2012, 09:20:55 pm by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: