Hey everyone,
I want to use the voltage results of a Sharp IR Sensor GP2Y0A710K and use the voltage readings to drive how a light blinks. During testing I am getting some strange output to the Serial Monitor. Using the code below the numbers tend to stay at the 130 then drop down to Zero when I move close then back up to the 130s and stabilize there. Looking at the other topics on the forum it looks like when working correctly the serial output should follow the curve and give a farily consistent result depending on the distance.
Thanks for your help.
#define sensorPin 0
int sensorValue = 0;
// this is called on startup
void setup() {
/*Description: sharp 2Y0A710K distance ranger.(Range from 1m to 5.5m.)
This code prints out data of valid range.
However, if we put ranger very close to objects, we will have completely wrong results.
The error is inevitable because of electric character of this device.
So the only way of correct use is to make sure the distance of objects not close to the ranger. (1m to 5.5m.)
Another tip: it is not very precise. So it is fit for detection,but not for measurement.
Tested by Michael from DFrobot
2012/2/13*/
/*Principle of this ranger: (See details in datasheet.)
Voltage is linear to the inverse of distance. (Distance range is 1m to 5.5m)
Two reference points:
Analog Voltage(V) Digital value(0-1023) Distance(cm)
2.5 512 100
1.4 286 500
Then, we have a linear equation of digital value and distance.
(512-sensorValue)/(1/100-1/distance)=(512-286)/(0.01-0.002)
=> distance=28250/(sensorValue-229.5);
*/
void setup() {
Serial.begin(9600);
}
int sensorValue;
int distance;
void loop() {
sensorValue = analogRead(A0);
if (sensorValue >= 280 && sensorValue <= 512) //Corresponding distance range from 1m to 5.5m
{
Serial.print("The distance is: ");
distance = 28250 / (sensorValue - 229.5);
Serial.print(distance);
Serial.println("cm");
}
delay(200);
}