LM35
Dependents: HealthCare_Graduation
Revision 0:813970f650f3, committed 2018-06-18
- Comitter:
- DuyLionTran
- Date:
- Mon Jun 18 07:38:21 2018 +0000
- Commit message:
- version 1.3.7 04-06-2018 Some minor bugs fixed
Changed in this revision
LM35.cpp | Show annotated file Show diff for this revision Revisions of this file |
LM35.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM35.cpp Mon Jun 18 07:38:21 2018 +0000 @@ -0,0 +1,60 @@ +#include "LM35.h" + +LM35Therm::LM35Therm(PinName analogPin, float compensation) { + _pAin = new AnalogIn(analogPin); + _calibrate = compensation; + _isFirstRead = true; +} + +LM35Therm::~LM35Therm() { + delete _pAin; +} + +void LM35Therm::getAverageValue() { + _sumAnalog = 0; + + for (uint8_t i = 0; i < SAMPLE_COUNT; i++) { + analogSamples[i] = _pAin->read(); + } + + for (uint8_t i = 0; i < SAMPLE_COUNT; i++) { + _sumAnalog = _sumAnalog + analogSamples[i]; + } + + averageAnalog = _sumAnalog / SAMPLE_COUNT; + _sumAnalog = 0; + + if (_isFirstRead) { + for (uint8_t i = 0; i < SAMPLE_COUNT; i++) { + analogAverageSamples[i] = averageAnalog; + _sumAnalog = _sumAnalog + analogAverageSamples[i]; + } + filtedAvgAnalog = _sumAnalog / SAMPLE_COUNT; + _isFirstRead = false; + } + else { + for (uint8_t i = 0; i < SAMPLE_COUNT - 1; i++) { + analogAverageSamples[i] = analogAverageSamples[i]; + _sumAnalog = _sumAnalog + analogAverageSamples[i]; + } + analogAverageSamples[SAMPLE_COUNT - 1] = averageAnalog; + _sumAnalog = _sumAnalog + analogAverageSamples[SAMPLE_COUNT - 1]; + } + filtedAvgAnalog = _sumAnalog / SAMPLE_COUNT; +} + +void LM35Therm::setCompensation(float newCompensation) { + _calibrate = newCompensation; +} + +float LM35Therm::LM35Therm::getTempInC() { + _readVoltage = filtedAvgAnalog * 3.3; + float avgVolt = filtedAvgAnalog * _calibrate * 1000.0; + tempInC = avgVolt; + return tempInC; +} + +float LM35Therm::LM35Therm::getTempInF() { + tempInF = (9.0 * tempInC)/5.0 + 32.0; + return tempInF; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM35.h Mon Jun 18 07:38:21 2018 +0000 @@ -0,0 +1,41 @@ +#ifndef __LM35_H__ +#define __LM35_H__ + +#include "mbed.h" + +#define SAMPLE_COUNT 30 + +class LM35Therm { +private: + AnalogIn *_pAin; + bool _isFirstRead; + + float _sumAnalog; + float _calibrate; + + float analogSamples[SAMPLE_COUNT]; + float analogAverageSamples[SAMPLE_COUNT]; +// float _readVoltage; + +public: + float tempInC; + float tempInF; + + float averageAnalog; + float filtedAvgAnalog; + + float _readVoltage; + +public: + LM35Therm(PinName analogPin, float compensation); + + ~LM35Therm(); + + void getAverageValue(); + void setCompensation(float newCompensation); + float getTempInC(); + float getTempInF(); + +}; + +#endif /* __LM35_H__ */ \ No newline at end of file