Simple heater library for controlling the temperature via analog temperature sensor and digital heater output.
Revision 0:eb243c2ffbfe, committed 2015-01-22
- Comitter:
- tbjazic
- Date:
- Thu Jan 22 13:37:43 2015 +0000
- Commit message:
- Initial commit.
Changed in this revision
Heater.cpp | Show annotated file Show diff for this revision Revisions of this file |
Heater.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r eb243c2ffbfe Heater.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Heater.cpp Thu Jan 22 13:37:43 2015 +0000 @@ -0,0 +1,41 @@ +#include "Heater.h" + +Heater::Heater(PinName sensorPin, PinName pwmoutPin): sensor(sensorPin), driver(pwmoutPin) { + hysteresis = 0.5; + heaterOn = false; + configureSensor(5, 30); + setTemperature(22); + ticker.attach(this, &Heater::regulateTemperature, 1); +} + +void Heater::configureSensor(float minTemperature, float maxTemperature) { + if (minTemperature < maxTemperature) { + minTemp = minTemperature; + maxTemp = maxTemperature; + } +} + +float Heater::readTemperature() { + return (maxTemp - minTemp) * sensor.read() + minTemp; +} + +bool Heater::isHeaterOn() { + return heaterOn; +} + +void Heater::setTemperature(float temp) { + if (temp >= minTemp && temp <= maxTemp) { + desiredTemp = temp; + heaterOnTemp = desiredTemp - hysteresis / 2; + heaterOffTemp = desiredTemp + hysteresis / 2; + } +} + +void Heater::regulateTemperature() { + currentTemp = readTemperature(); + if (currentTemp < heaterOnTemp && !isHeaterOn()) + driver = 1; // turn ON the heater + else if (currentTemp > heaterOffTemp && isHeaterOn()) + driver = 0; // turn OFF the heater + heaterOn = driver.read(); +} \ No newline at end of file
diff -r 000000000000 -r eb243c2ffbfe Heater.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Heater.h Thu Jan 22 13:37:43 2015 +0000 @@ -0,0 +1,34 @@ +#ifndef HEATER_H +#define HEATER_H + +#include "mbed.h" + +/** A simple class for temperature control in the room using + * a heater driven via digital output and one analog temperature sensor. + * + * Author(s): TVZ Mechatronics Team + * + */ +class Heater { + public: + /** Constructor receives pin names of the temperature sensor and + * the DigitalOut pin to which the heater driver is connected. */ + Heater(PinName sensorPin, PinName digitalOutPin); + /** Set the temperature range of the sensor. */ + void configureSensor(float minTemperature, float maxTemperature); + /** Read the current room temperature from the sensor. */ + float readTemperature(); + /** Test if the heater in turned ON. */ + bool isHeaterOn(); + /** Set the desired room temperature. */ + void setTemperature(float temp); + private: + AnalogIn sensor; + DigitalOut driver; + Ticker ticker; + void regulateTemperature(); + bool heaterOn; + float currentTemp, desiredTemp, minTemp, maxTemp, hysteresis, heaterOnTemp, heaterOffTemp; +}; + +#endif // HEATER_H \ No newline at end of file