This library is designed to work with devices like the LM335 temperature sensor. There are only two requirements for compatibility: 1) The device must be a sensor device that has an analog voltage output. 2) The physical quantity measured must vary linearly with the analog voltage. The LM335 creates an analog voltage proportional to temperature. It can work very well with this library using the nominal multiplier of 0.0050354 and offset of -273.15 or by providing two user defined calibration points to the constructor. It uses a median filter to remove unwanted noise from ADC readings.

Dependents:   LoRA_PH_SensorHub

Fork of LinearAnalogSensors by Penn Electric

Committer:
wang1tao
Date:
Thu Aug 11 11:09:32 2016 +0000
Revision:
4:a0ac12c46095
Parent:
3:6a28ca406c91
First version of PH sensor Hub

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PennElectric 0:d84ef842074b 1 /* Copyright (c) <2012> <P. Patel>, MIT License
PennElectric 0:d84ef842074b 2 *
PennElectric 0:d84ef842074b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
PennElectric 0:d84ef842074b 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
PennElectric 0:d84ef842074b 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
PennElectric 0:d84ef842074b 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
PennElectric 0:d84ef842074b 7 * furnished to do so, subject to the following conditions:
PennElectric 0:d84ef842074b 8 *
PennElectric 0:d84ef842074b 9 * The above copyright notice and this permission notice shall be included in all copies or
PennElectric 0:d84ef842074b 10 * substantial portions of the Software.
PennElectric 0:d84ef842074b 11 *
PennElectric 0:d84ef842074b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
PennElectric 0:d84ef842074b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
PennElectric 0:d84ef842074b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
PennElectric 0:d84ef842074b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
PennElectric 0:d84ef842074b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PennElectric 0:d84ef842074b 17 */
PennElectric 0:d84ef842074b 18
PennElectric 0:d84ef842074b 19 // --------------------- Median Filtered Linear Temperature Sensor Reader ----------------------------
PennElectric 0:d84ef842074b 20
wang1tao 3:6a28ca406c91 21 #include "AnalogPHSensor.h"
PennElectric 0:d84ef842074b 22 #include "mbed.h"
wang1tao 4:a0ac12c46095 23 #include "mDot.h"
PennElectric 0:d84ef842074b 24
wang1tao 3:6a28ca406c91 25 // initialize
wang1tao 3:6a28ca406c91 26 AnalogPHSensor::AnalogPHSensor(PinName pin):_pin(pin) {
wang1tao 4:a0ac12c46095 27 _ph = 0.0; // Zero the _ph
PennElectric 0:d84ef842074b 28 }
PennElectric 0:d84ef842074b 29
wang1tao 3:6a28ca406c91 30 // Populates an array with PH readings, sorts, and returns average value
wang1tao 3:6a28ca406c91 31 float AnalogPHSensor::readPH() {
wang1tao 3:6a28ca406c91 32 unsigned short i, tmp, minValue, maxValue;
wang1tao 3:6a28ca406c91 33
wang1tao 3:6a28ca406c91 34 _ph = 0.0;
wang1tao 3:6a28ca406c91 35 for (i = 0; i < 10; i++) {
wang1tao 3:6a28ca406c91 36 tmp = _pin.read_u16();
wang1tao 3:6a28ca406c91 37 osDelay(200);
wang1tao 3:6a28ca406c91 38 while(tmp == 0) //suppose "0" is an invalid reading
wang1tao 3:6a28ca406c91 39 {
wang1tao 3:6a28ca406c91 40 tmp = _pin.read_u16();
wang1tao 3:6a28ca406c91 41 osDelay(200);
PennElectric 0:d84ef842074b 42 }
wang1tao 3:6a28ca406c91 43 _phArr[i] = tmp;
PennElectric 0:d84ef842074b 44 }
wang1tao 3:6a28ca406c91 45
wang1tao 3:6a28ca406c91 46 //looking for the minValue and maxValue in the buffer
wang1tao 3:6a28ca406c91 47 minValue = _phArr[0];
wang1tao 3:6a28ca406c91 48 maxValue = _phArr[0];
wang1tao 3:6a28ca406c91 49 for(i=1; i<10; i++){
wang1tao 3:6a28ca406c91 50 if(minValue > _phArr[i])minValue=_phArr[i];
wang1tao 3:6a28ca406c91 51 if(maxValue < _phArr[i])maxValue=_phArr[i];
wang1tao 3:6a28ca406c91 52 }
wang1tao 3:6a28ca406c91 53
wang1tao 3:6a28ca406c91 54 //sum
wang1tao 3:6a28ca406c91 55 for(i=0; i<10; i++)
wang1tao 3:6a28ca406c91 56 {
wang1tao 3:6a28ca406c91 57 _ph += (float)_phArr[i];
wang1tao 3:6a28ca406c91 58 }
wang1tao 3:6a28ca406c91 59
wang1tao 3:6a28ca406c91 60 // minus max and min
wang1tao 3:6a28ca406c91 61 _ph -= (float)minValue;
wang1tao 3:6a28ca406c91 62 _ph -= (float)maxValue;
wang1tao 3:6a28ca406c91 63
wang1tao 3:6a28ca406c91 64 //averaging
wang1tao 3:6a28ca406c91 65 _ph = _ph/8.0;
wang1tao 3:6a28ca406c91 66
wang1tao 3:6a28ca406c91 67 return _ph;
PennElectric 0:d84ef842074b 68 }
wang1tao 3:6a28ca406c91 69 // Returns last calculated ph value
wang1tao 3:6a28ca406c91 70 float AnalogPHSensor::getPH() {
wang1tao 3:6a28ca406c91 71 return _ph;
PennElectric 0:d84ef842074b 72 }