Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: TemperatureSensor.cpp
- Revision:
- 5:4cbe44452889
- Parent:
- 3:7c648d1d8802
- Child:
- 7:46e65aeb4df2
--- a/TemperatureSensor.cpp Fri Jun 17 09:24:01 2016 +0000 +++ b/TemperatureSensor.cpp Fri Jun 17 11:03:37 2016 +0000 @@ -8,64 +8,45 @@ TemperatureSensor::TemperatureSensor( Printer &printer, - PinName pin, - double const_voltage, - double const_converter, - double const_lower_boundary, - double const_upper_boundary, - double variance, - double vin, - double resistance + PinName pin ): _printer(printer), _analog_in(pin), - _const_voltage(const_voltage), - _const_converter(const_converter), - _const_lower_boundary(const_lower_boundary), - _const_upper_boundary(const_upper_boundary), - _const_sample_number(100), - _variance(variance), - _vin(vin), - _resistance(resistance), _reading(0.0), _voltage(0.0), _temperature(0.0), _status(0.0), - _strStatus("OK"), - _k0(0.00102119), - _k1(0.000222468), - _k2(0.000000133342), - _kelvin_to_celcius(-273.15) + _strStatus("OK") { this->reload(); } void TemperatureSensor::reload() { - double readings[this->_const_sample_number]; - for(int counter = 0; counter < this->_const_sample_number; ++counter){ + double readings[SAMPLING_NUMBER]; + for(int counter = 0; counter < SAMPLING_NUMBER; ++counter){ readings[counter] = _analog_in.read(); } - sort(readings, readings + this->_const_sample_number); + sort(readings, readings + SAMPLING_NUMBER); - this->_reading = readings[this->_const_sample_number / 2]; - this->_voltage = this->_reading * this->_const_voltage * this->_const_converter; - double RT = (this->_voltage * this->_resistance) / (this->_vin - this->_voltage); + this->_reading = readings[SAMPLING_NUMBER / 2]; + this->_voltage = this->_reading * VIN * CONVERTER; + double RT = (this->_voltage * RESISTANCE) / (VIN - this->_voltage); double logRT = log(RT); - double K0 = this->_k0; - double K1 = this->_k1 * logRT; - double K2 = this->_k2 * pow(logRT, 3.0); - double kelvin = 1.0 / (K0 + K1 + K2); - this->_temperature = (kelvin + this->_kelvin_to_celcius) + this->_variance; + double k0 = K0; + double k1 = K1 * logRT; + double k2 = K2 * pow(logRT, 3.0); + double kelvin = 1.0 / (k0 + k1 + k2); + this->_temperature = (kelvin + KELVIN_TO_CELCIUS) + VARIANCE; this->_status = 0.0; this->_strStatus = "OK"; - if(this->_temperature < this->_const_lower_boundary){ - this->_status = this->_temperature - this->_const_lower_boundary; + if(this->_temperature < LOWER_BOUNDARY){ + this->_status = this->_temperature - LOWER_BOUNDARY; this->_strStatus = "LW"; - } else if(this->_temperature > this->_const_upper_boundary){ - this->_status = this->_temperature - this->_const_upper_boundary; + } else if(this->_temperature > UPPER_BOUNDARY){ + this->_status = this->_temperature - UPPER_BOUNDARY; this->_strStatus = "HI"; } }