/* ZSharpIR Arduino library for retrieving distance (in mm) from the analog GP2Y0A21Y and GP2Y0A02YK,... Original comment from Dr. Marcal Casas-Cartagena : inspired from : - https://github.com/qub1750ul/Arduino_SharpIR.git - https://github.com/jeroendoggen/Arduino-GP2Y0A21YK-library.git - https://github.com/guillaume-rico/SharpIR.git - https://github.com/nikv96/MDP-Arduino.git - https://github.com/jeroendoggen/Arduino-GP2Y0A21YK-library.git */ #include "Arduino.h" #include "WMath.h" #include "ZSharpIR.h" // Initialisation function // + irPin : is obviously the pin where the IR sensor is attached // + sensorModel is a int to differentiate the two sensor models this library currently supports: // 1080 is the int for the GP2Y0A21Y and // 20150 is the int for GP2Y0A02YK and // 100500 is the long for GP2Y0A710K0F // The numbers reflect the distance range they are designed for (in cm) ZSharpIR::ZSharpIR(int irPin, const uint32_t sensorModel) { _irPin=irPin; _model=sensorModel; // Define pin as Input pinMode (_irPin, INPUT); _Adcres=10; _refVoltage=5000; } // Sort an array void ZSharpIR::sort(int a[], int size) { for(int i=0; i<(size-1); i++) { bool flag = true; for(int o=0; o<(size-(i+1)); o++) { if(a[o] > a[o+1]) { int t = a[o]; a[o] = a[o+1]; a[o+1] = t; flag = false; } } if (flag) break; } } // Read distance and compute it int ZSharpIR::distance() { int ir_val[NB_SAMPLE]; int distanceMM; float current; for (int i=0; i 3300) { //false data distanceMM = 0; } else { distanceMM =(int)( 10.0 / (((current - 1125.0) / 1000.0) / 137.5)); } } return distanceMM; } /// /// setARefVoltage:set the ADC reference voltage: (default value: 5000mV, set to 3300mV, typically 3.3 on Arduino boards) /// void ZSharpIR::setARefVoltage(int refV) { _refVoltage=refV; } /// /// SetAnalogReadResolution:set the ADC resolution : (default value: 10, set to 12, typically 10 on Arduino boards) /// void ZSharpIR::SetAnalogReadResolution(int res) { _Adcres=res; analogReadResolution( res); }