#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
//========================================== DHT
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 10
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
sensors_event_t event;
uint32_t delayMS;
float temp,humid;
//========================================== CONST PIN
int rly1Fan=13, rly2AC=12, ledG=4, ledR=3;
float flagMasa=0, saat=0;
//========================================== SETUP
void setup()
{ lcd.init();
lcd.backlight();
dht.begin();
initDHT();
timerInit();
Serial.begin(9600);
pinMode(rly1Fan,OUTPUT);
pinMode(rly2AC,OUTPUT);
pinMode(ledG,OUTPUT);
pinMode(ledR,OUTPUT);
lcd.clear();
}
float an,treshold;
void loop()
{ readPOT();
sensors_event_t event;
//================================================================ Read Temp
dht.temperature().getEvent(&event);
lcd.setCursor(0,0);
lcd.print("T:"); lcd.print(event.temperature,1); lcd.print("C");
lcd.setCursor(8,1);
Serial.print("Temperature: "); Serial.print(event.temperature,1);
Serial.print(" Treshold: "); Serial.println(treshold);
// if(treshold>event.temperature) { FANon(); ledRon(); lcd.print("Mode:FAN"); }
// if(treshold<=event.temperature) { ACon(); ledGon(); lcd.print("Mode:AC "); }
if(flagMasa==0) { flagMasa=0; FANon(); ledRon(); lcd.print("Mode:FAN"); saat=0; }
if(treshold<=event.temperature) { flagMasa=1; ACon(); ledGon(); lcd.print("Mode:AC "); }
if(saat>=3600) flagMasa=0;
//================================================================ Read Humid
dht.humidity().getEvent(&event);
lcd.setCursor(0,1);
if(flagMasa==0) { lcd.print("H:"); lcd.print(event.relative_humidity,1); lcd.print("%"); }
if(flagMasa==1) { lcd.print("T:"); lcd.print(((3600-saat)/60),0); lcd.print("min"); }
}
void ACon() { digitalWrite(rly2AC,HIGH); digitalWrite(rly1Fan,LOW); }
void FANon() { digitalWrite(rly2AC,LOW); digitalWrite(rly1Fan,HIGH); }
void ledGon() { digitalWrite(ledG,HIGH); digitalWrite(ledR,LOW); }
void ledRon() { digitalWrite(ledG,LOW); digitalWrite(ledR,HIGH); }
void readPOT()
{ an = analogRead(A0);
treshold = map(an,0,1023,15,30);
lcd.setCursor(8,0);
lcd.print("TH:");
lcd.print(treshold,1);
lcd.print("C");
}
void initDHT()
{ sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
delayMS = sensor.min_delay / 1000;
}
void timerInit()
{ // TIMER 1 for interrupt frequency 1 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
// set compare match register for 1 Hz increments
OCR1A = 62499; // = 16000000 / (256 * 1) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 256 prescaler
TCCR1B |= (1 << CS12) | (0 << CS11) | (0 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
}
ISR(TIMER1_COMPA_vect)
{ if(flagMasa==1)
{ saat++;
Serial.println(saat);
}
}