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