PH test

Fork of LinearAnalogSensors by Penn Electric

Committer:
Dengjj
Date:
Thu Aug 11 14:14:49 2016 +0000
Revision:
3:c749fbcf2acb
Parent:
2:989048937c77
working for PH test

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
PennElectric 0:d84ef842074b 21 #ifndef LINEAR_TEMP
PennElectric 0:d84ef842074b 22 #define LINEAR_TEMP
PennElectric 0:d84ef842074b 23
PennElectric 0:d84ef842074b 24 #include "mbed.h"
PennElectric 0:d84ef842074b 25 /** This library is designed to work with devices like the LM335 temperature sensor. There are only
PennElectric 0:d84ef842074b 26 * two requirements for compatibility of a device with this library: 1) It must be a sensor device
PennElectric 0:d84ef842074b 27 * that has an analog voltage output. 2) The physical quantity measured must vary linearly with
PennElectric 0:d84ef842074b 28 * the analog voltage. The LM335 which creates an analog voltage proportional to temperature
PennElectric 0:d84ef842074b 29 * can work very well with this library using the nominal multiplier of 0.0050354 and offset of
PennElectric 0:d84ef842074b 30 * -273.15 or by providing two calibration points to the constuctor to generate a linear response. It
PennElectric 0:d84ef842074b 31 * samples the sensor 9 times and uses the median to minimize the effect of poor ADC readings and sudden
PennElectric 0:d84ef842074b 32 * changes.
PennElectric 0:d84ef842074b 33 *
PennElectric 0:d84ef842074b 34 * Example:
PennElectric 0:d84ef842074b 35 * @code
PennElectric 0:d84ef842074b 36 * LinearTemp mysensor(p20, 0.0050354, -273.15); // Setup a LM335 temp sensor on pin 20
PennElectric 0:d84ef842074b 37 * DigitalOut myled(LED1);
PennElectric 0:d84ef842074b 38 *
PennElectric 0:d84ef842074b 39 * int main() {
PennElectric 0:d84ef842074b 40 * while(1) {
PennElectric 0:d84ef842074b 41 * // Light LED if filtered temperature is greater than 45 degrees Celsius
PennElectric 0:d84ef842074b 42 * if (mysensor > 45) myled = 1;
PennElectric 0:d84ef842074b 43 * else myled = 0;
PennElectric 0:d84ef842074b 44 * }
PennElectric 0:d84ef842074b 45 * }
PennElectric 0:d84ef842074b 46 * @endcode
PennElectric 0:d84ef842074b 47 */
PennElectric 0:d84ef842074b 48 class LinearTemp {
PennElectric 0:d84ef842074b 49 public:
PennElectric 0:d84ef842074b 50 /** Create a calibrated temperature sensor object connected to an Analog input pin
PennElectric 0:d84ef842074b 51 * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected
PennElectric 0:d84ef842074b 52 * @param temp1 Temperature (float in degrees C or F) of first calibration point
PennElectric 0:d84ef842074b 53 * @param read1 Mbed ADC reading (unsigned short) of first calibration point
PennElectric 0:d84ef842074b 54 * @param temp2 Temperature (float in degrees C or F) of second calibration point
PennElectric 0:d84ef842074b 55 * @param read2 Mbed ADC reading (unsigned short) of second calibration point
PennElectric 0:d84ef842074b 56 */
PennElectric 0:d84ef842074b 57 LinearTemp(PinName pin, float temp1, unsigned short read1, float temp2, unsigned short read2);
PennElectric 0:d84ef842074b 58 /** Create a calibrated temperature sensor object connected to an Analog input pin
PennElectric 0:d84ef842074b 59 * @param pin Analog input pin to which an LM335 or comparable linear temp sensor is connected
PennElectric 0:d84ef842074b 60 * @param multiplier Conversion multiplier to go from ADC reading as unsigned short to temperature (change in degrees / change in unsigned short)
PennElectric 0:d84ef842074b 61 * @param offset Conversion offset (positive or negative)
PennElectric 0:d84ef842074b 62 */
PennElectric 0:d84ef842074b 63 LinearTemp(PinName pin, float multiplier, float offset);
PennElectric 1:7ce89c2ecf05 64 /** Returns a new median-filtered, converted reading from the sensor
PennElectric 1:7ce89c2ecf05 65 * @returns New temperature (float) in degrees C or F as designated by the calibration points
PennElectric 1:7ce89c2ecf05 66 */
PennElectric 0:d84ef842074b 67 float readTemp();
PennElectric 0:d84ef842074b 68 /** Return the last calculated and stored temperature
PennElectric 0:d84ef842074b 69 * @returns Last stored temperature (float) in degrees C or F as designated by the calibration points
PennElectric 0:d84ef842074b 70 */
PennElectric 0:d84ef842074b 71 float getTemp();
PennElectric 0:d84ef842074b 72 #ifdef MBED_OPERATORS
PennElectric 0:d84ef842074b 73 /** An operator shorthand for readTemp() to calculate filtered temperature
PennElectric 0:d84ef842074b 74 */
PennElectric 2:989048937c77 75 operator float() {
PennElectric 0:d84ef842074b 76 return readTemp();
PennElectric 0:d84ef842074b 77 }
PennElectric 0:d84ef842074b 78 #endif
PennElectric 0:d84ef842074b 79 private:
PennElectric 0:d84ef842074b 80 AnalogIn _pin;
PennElectric 0:d84ef842074b 81 unsigned short _tempArr[9];
PennElectric 0:d84ef842074b 82 float _temp;
PennElectric 0:d84ef842074b 83 float _multiplier;
PennElectric 0:d84ef842074b 84 float _offset;
PennElectric 0:d84ef842074b 85 };
PennElectric 0:d84ef842074b 86 #endif