Optimized

Fork of LinearAnalogSensors by Penn Electric

Committer:
wang1tao
Date:
Sat Aug 13 09:34:26 2016 +0000
Revision:
4:61e97a2029f8
Changed for Multi-channel PH measurement

Who changed what in which revision?

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