#ifndef Light_h
#define Light_h
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
class Light {
private:
int pin;
String name;
public:
Light(int x, String y) {
pin = x;
name = y;
}
String getName() {
return name;
}
void init() {
pinMode(pin, OUTPUT);
}
void turnOn() {
digitalWrite(pin, HIGH);
}
void turnOff() {
digitalWrite(pin, LOW);
}
};
// Define Light
Light redLight(4, "Red");
Light yellowLight(15, "Yellow");
Light greenLight(2, "Green");
void setup() {
Serial.begin(3000);
// Inisiate an object
dht.begin();
redLight.init();
yellowLight.init();
greenLight.init();
}
void loop() {
// Listener New Value
float temperature = dht.readTemperature();
Serial.println("temperature: " + String(temperature, 2) + "\xC2\xB0"+"C");
// Handle Condition
if (temperature < 20) {
displayState(yellowLight);
redLight.turnOff();
yellowLight.turnOn();
greenLight.turnOff();
} else if (temperature >= 20 && temperature <= 24.5) {
displayState(greenLight);
redLight.turnOff();
yellowLight.turnOff();
greenLight.turnOn();
} else {
displayState(redLight);
redLight.turnOn();
yellowLight.turnOff();
greenLight.turnOff();
}
Serial.println("=================");
delay(300);
}
void displayState(Light light) {
Serial.println("Current State is " + light.getName());
}
#endif