Skip to content

Commit

Permalink
Added support for more sensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHauser committed Aug 20, 2019
1 parent 9cd713b commit 98c61aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ See the readme.md file of the master branch (v1.0) for a little bit of backgroun
- The distance is given as a float instead of an integer.
- Much better way to sort out out-of-tolerance values.
- Gives better accuracy.
- GP2Y0A02Y and GP2Y0A710K0F sensors are also supported.

# How it works

Expand All @@ -28,27 +29,30 @@ The distance is calculated from a formula extracted from the graphs on the senso
Since this library takes some measurements before giving a final result, it is not as responsive as
direct measurements.

This library has the formulas to work with the GP2Y0A21Y and the GP2Y0A02YK sensors but expanding it for
other sensors is easy enough.
This library has the formulas to work with the GP2Y0A21Y, GP2Y0A02YK, GP2Y0A02Y and GP2Y0A710K0F sensors
but expanding it for other sensors with similar behaviour should be easy enough.

# Setting it up

I won't be explaining how to copy the library into your Arduino IDE, it's well documented...
I won't be explaining how to copy the library into your Arduino IDE, it's well documented. But you
have to do it.

#### 1. Import Library:
`<include "SharpAverageIR.h>`
<include "SharpAverageIR.h>

#### 2. Initialize an object:
`SharpAverageIR sharp(irPin, avg, tolerance, model);` //I called the object sharp in this example
`SharpAverageIR sharp(irPin, avg, tolerance, model); //I called the object sharp in this example`

**irPin** is the Analog Pin in which the sensor is attached.
**avg** is an Int representing the number of measurements it will take.
**tolerance** is the requested tolerance among values. It is an Int between 1 and 100. Experiment with it.
**model** it is an Int which represent the sensor in use.
use **0 or 1080** on the constructor for the GP2Y0A21Y (from 10 to 80cm)
use **1 or 20150** on the constructor for the GP2Y0A02Y (from 20 to 150cm)
- **irPin** is the Analog Pin in which the sensor is attached.
- **avg** is an Int representing the number of measurements it will take.
- **tolerance** is the requested tolerance among values. It is an Int between 1 and 100. Experiment with it.
- **model** it is an Int which represent the sensor in use.
- use **0 or 1080** on the constructor for the GP2Y0A21Y (from 10 to 80cm)
- use **1 or 20150** on the constructor for the GP2Y0A02Y (from 20 to 150cm)
- use **2 or 0430** on the constructor for the GP2Y0A41SK0F (from 04 to 40cm)
- use **3 or 10550** on the constructor for the GP2Y0A710K0F (from 100 to 550cm)

#### 3. Call the public method:
`float dis=sharp.distance();` //this method returns a float with the distance.
`float dis=sharp.distance(); //this method returns a float with the distance.`

Please see the example provided on this Library.
19 changes: 14 additions & 5 deletions SharpAverageIR/SharpAverageIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ switch (caseVar) {
case 20150: //GP2Y0A02Y (from 20 to 150cm) //left for legacy reasons
puntualDistance=(61.573*pow(floatRaw, -1.1068))*NONDECIMALMULTIPLIER;
break;
case 2: //GP2Y0A02Y (from 04 to 80cm)
puntualDistance=(12.577*pow(floatRaw, -1.0512))*NONDECIMALMULTIPLIER;
break;
case 0430: //GP2Y0A02Y (from 04 to 80cm) //left for legacy reasons
puntualDistance=(12.577*pow(floatRaw, -1.0512))*NONDECIMALMULTIPLIER;
break;
case 3: //GP2Y0A710K0F (from 100 to 550cm)
puntualDistance=(1078.4*pow(floatRaw, -2.6965))*NONDECIMALMULTIPLIER;
break;
case 100550: //GP2Y0A710K0F (from 100 to 550cm) //left for legacy reasons
puntualDistance=(1078.4*pow(floatRaw, -2.6965))*NONDECIMALMULTIPLIER;
break;


default:
//nothing to show
break;
Expand All @@ -73,8 +87,6 @@ switch (caseVar) {





float SharpAverageIR::distance() {

//first, an array is populated and the average calculated
Expand All @@ -90,10 +102,8 @@ float SharpAverageIR::distance() {

summ=summ/_avg;


//we sort out the values too far from the average


unsigned long newAverage;
int averageCounter=0;

Expand All @@ -107,7 +117,6 @@ float SharpAverageIR::distance() {

}


// we return the new average

return (newAverage*1.0/(averageCounter*NONDECIMALMULTIPLIER));
Expand Down
8 changes: 3 additions & 5 deletions SharpAverageIR/SharpAverageIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@
Since this library takes some measurements before giving a final result, it is not as responsive as
direct measurements.
This library has the formulas to work with the GP2Y0A21Y and the GP2Y0A02YK sensors but exanding it for
This library has the formulas to work with the GP2Y0A21Y, GP2Y0A02YK and the GP2Y0A41SK0F sensors but exanding it for
other sensors is easy enough.
Model refenceces:
use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm)
use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm)
*/

/* **************************** HOW TO USE *******************
Expand All @@ -66,6 +62,8 @@ When you initialize the library object on your sketch you have to pass all the a
*** sensorModel is a int to differentiate the two sensor models this library currently supports:
use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm)
use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm)
use 2 or 0430 on the constructor for the GP2Y0A41SK0F (from 04 to 40cm)
use 3 or 10550 on the constructor for the GP2Y0A710K0F (from 100 to 550cm)
*/

Expand Down
2 changes: 2 additions & 0 deletions SharpAverageIR/examples/SharpSensorCm/SharpSensorCm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ When you initialize the library object on your sketch you have to pass all the a
*** sensorModel is a int to differentiate the two sensor models this library currently supports:
use 0 or 1080 on the constructor for the GP2Y0A21Y (from 10 to 80cm)
use 1 or 20150 on the constructor for the GP2Y0A02Y (from 20 to 150cm)
use 2 or 0430 on the constructor for the GP2Y0A41SK0F (from 04 to 40cm)
use 3 or 10550 on the constructor for the GP2Y0A710K0F (from 100 to 550cm)
3- Call the public method:
Expand Down

0 comments on commit 98c61aa

Please sign in to comment.