//PWR
//
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.hpp>
#define PwrPin 2 //AC OnOff
#define IRSendPin 3 //Sender Pin 3
#define DHTPin 4 //DHT Pin 4
#define SelectPin 5 //Mode Selector Pin 5
#define IRRecPin 6 //Receiver Pin 2
#define RAW_BUFFER_LENGTH 600
bool status = 1;
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPin, DHT22); //Creates an object dht
void setup() {
pinMode(PwrPin, INPUT_PULLUP);
pinMode(SelectPin, INPUT_PULLUP);
Serial.begin(9600);
lcd.init(); //Initialize LCD
lcd.clear(); //Clear LCD
lcd.backlight(); //Turn on LCD backlight
dht.begin();
IrReceiver.begin(IRRecPin);
Serial.println(IRRecPin);
attachInterrupt(digitalPinToInterrupt(PwrPin), pwr, FALLING);
}
void loop() {
if (digitalRead(SelectPin)==1) {
normal();
} else {
calib();
}
}
void normal() {
float temp = dht.readTemperature();
float humid = dht.readHumidity();
lcd.clear();
lcd.setCursor(2,0); //Set cursor to character 2 on line 0
lcd.print("Temp: ");
lcd.print(temp,1); //Output temperature
lcd.print((char)223);
lcd.print("C"); //Output degrees character
lcd.setCursor(2,1); //Set cursor to character 2 on line 0
lcd.print("Humid: ");
if (humid <10){
lcd.print(0);
}
lcd.print(humid,1); //Output humidity
lcd.print("%"); //Output %
float humidex = hdex(temp, humid);
if (humidex > 28){
if (temp > 28 || humid > 50) {
//Decrease temp by 4
} else {
//Decrease temp by 2
}
} else {
//Do something
}
Serial.println(humidex);
delay(10000);
}
void calib() {
lcd.setCursor(0,0);
lcd.print("Calibration Mode"); //Output temperature
if (IrReceiver.decode()) {
IrReceiver.printIRSendUsage(&Serial);
IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, true); //Output the results as uint16_t source code array of micros
IrReceiver.resume();
}
}
float hdex(float temp, float humid) {
//Calculate dew point
double RATIO = 373.15 / (273.15 + temp);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
double T = log((pow(10, RHS - 3) * humid) / 0.61078);
double dew = (241.88 * T) / (17.558 - T);
//Calculate humidex
double e = 5417.7530*((1/273.15)-(1/(273.15 + dew)));
return temp + 0.5555 * ( 6.11 * exp (e) - 10);
}
void pwr() {
status = !status;
if (status) {
Serial.println("On");
} else {
Serial.println("Off");
scroll();
}
}
void scroll() {
while(1) {
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" Press Green Button to Power On ");
for (int x = 0; x < 18; x++) {
lcd.scrollDisplayLeft();
lcd.setCursor(x,0);
lcd.print(" POWERED OFF");
delay(500);
}
}
}