wireless sensor / LinearAnalogSensors

Fork of LinearAnalogSensors by Penn Electric

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnaolgPH.cpp Source File

AnaolgPH.cpp

00001 /* Copyright (c) <2012> <P. Patel>, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019 // --------------------- Median Filtered Linear Temperature Sensor Reader ----------------------------
00020  
00021 #include "AnalogPH.h"
00022 #include "mbed.h"
00023 #include "mDot.h"
00024 
00025 // Constructor using two calibration points to define linear multiplier and offset
00026 AnalogPHSensor::AnalogPHSensor(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2):_pin(pin) {
00027     _ph = 0.0;                                     // Zero the temperature
00028     _multiplier = (temp2 - temp1) / (read2 - read1); // Calculate multiplier as slope
00029     _offset = temp1 - (_multiplier * read1);         // Calculate offset
00030 }
00031 // Constructor using user defined multiplier and offset
00032 AnalogPHSensor::AnalogPHSensor(PinName pin, float multiplier, float offset):_pin(pin) {
00033     _ph = 0.0;                                     // Zero the temperature
00034     _multiplier = multiplier;                        // Set multiplier
00035     _offset = offset;                                // Set offset
00036 }
00037 
00038 // Populates an array with ph readings, sorts, and returns median
00039 float AnalogPHSensor::readPH() {
00040     unsigned short i, minValue, maxValue;
00041     
00042     _ph = 0.0;
00043     for (i = 0; i < 6; i++) { 
00044         _phArr[i] = _pin.read_u16();
00045         osDelay(100);
00046     }
00047     
00048         //looking for the minValue and maxValue in the buffer
00049     minValue = _phArr[0];
00050     maxValue = _phArr[0];
00051     for(i=1; i<6; i++){
00052         if(minValue > _phArr[i])minValue=_phArr[i];
00053         if(maxValue < _phArr[i])maxValue=_phArr[i];
00054     } 
00055     
00056         //sum
00057     for(i=0; i<6; i++)
00058     {
00059          _ph += (float)_phArr[i];
00060     }
00061     
00062     // minus max and min
00063     _ph -= (float)minValue;
00064     _ph -= (float)maxValue;
00065     
00066     //averaging
00067     _ph = _ph/4.0;
00068     
00069     _ph = 3.0*_ph/(16.0*4096);
00070     return _ph;
00071 }
00072 // Returns last calculated ph value
00073 float AnalogPHSensor::getPH() {
00074     return _ph;
00075 }