You are viewing an older revision! See the latest version
Room temperature control
Implement a class named Heater in a Heater.cpp file, with the following declaration part:
Heater.h
#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 PwmOut pin to which the heater driver is connected. */
Heater(PinName sensorPin, PinName pwmoutPin);
/** Set the temperature range of the sensor. */
void configureSensor(float minTemperature, float maxTemperature);
/** Read the current room temperature from the sensor. */
float readTemperature();
/** Get the current room temperature (do not read from sensor again). */
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
Then write the main function main.cpp for mbed application board, in which the potentiometer Pot1 acts as a temperature sensor, Pot2 acts as a thermostat (sets a desired temperature) and LED1 indicates the heater state (ON or OFF). Display the data on the LCD screen, according to the following example:
Desired temperature: 22.5 Current temperature: 24.0 Heater state: OFF