#define DHTPIN 2
#define RELAYPIN 4
#define DHTTYPE DHT11 // DHT 21 (AM2301)
#define next 9
#define INC 7
#define set_mode 5
/* ----- C Program for Arduino based Alarm Clock ---- */
#include <Wire.h>
#include<EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0;
const long interval = 5000;
int HMIN,HMAX,h,t;
int add = 11;
int st=0; //var for void match
int sb=0;//var for void match
void setup()
{
cekMem();
//Serial.begin(9600);
Wire.begin();
dht.begin();
lcd.init();
lcd.backlight();
pinMode(INC, INPUT);
pinMode(next, INPUT);
pinMode(set_mode, INPUT);
pinMode(RELAYPIN, OUTPUT);
digitalWrite(next, HIGH);
digitalWrite(set_mode, HIGH);
digitalWrite(INC, HIGH);
lcd.setCursor(0,0);
lcd.print("Sensor Starting..");
delay(2000);
}
void loop()
{
//unsigned long currentMillis = millis();
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
lcd.setCursor(1,0);
lcd.print("Error");
lcd.setCursor(0,1);
lcd.print("Check Sensor cable");
}
else {
lcd.clear();
lcd.print("Now: T:");
lcd.print(t);
lcd.print(" H: ");
lcd.print(h);
lcd.setCursor(0, 1);
lcd.print("H Min/max: ");
lcd.print(HMIN);
lcd.print("/");
lcd.print(HMAX);
delay(1000);
}
if(digitalRead(set_mode)==0){
lcd.clear();
lcd.setCursor(1,0);
setHmin();
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Setting Humidity");
lcd.setCursor(0,1);
lcd.print("tersimpan");
delay(2000);
}
match();
}//loop
void setHmin(){
lcd.setCursor(1,0);
lcd.clear();
lcd.print("SET H MIN");
lcd.setCursor(0,1);
lcd.print(HMIN);
int temp=1,hmin1=0,hmax1=0;
while(temp==1){
if(digitalRead(INC)==0){
HMIN++;
lcd.setCursor(0,1);
lcd.print(HMIN);
if(HMIN==99){
HMIN=65;
}//if(HMIN==99){
//while(digitalRead(INC)==0);
}//if(digitalRead(INC)==0){
delay(1000);
if(digitalRead(next)==0){
hmin1=HMIN;
EEPROM.write(add++,hmin1);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("H MIN SAVED at: ");
lcd.setCursor(0,1);
lcd.print(HMIN);
delay(3000);
while(digitalRead(next)==0);
temp=2;
}//if(digitalRead(next)==0)
}//while temp==1
while(temp==2){
//setting HMAX
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Set H MAX");
lcd.setCursor(0,1);
lcd.print(HMAX);
if(digitalRead(INC)==0){
HMAX++;
lcd.setCursor(0,1);
lcd.print(HMAX);
if(HMAX==99){
HMAX=65;
}//if(HMAX==99){
//while(digitalRead(next)==0);//?
}
delay(1000);
if(digitalRead(next)==0){
hmax1=HMAX;
EEPROM.write(add++, hmax1);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("H MAX Saved at: ");
lcd.setCursor(0,1);
lcd.print(HMAX);
temp=0;
while(digitalRead(next)==0);
}//if(digitalRead(next)==0){
}//if(digitalRead(INC)==0){
delay(1000);
//}//while temp=2
}//setHmin()
void cekMem(){
int tem[17];
for(int i=11;i<17;i++)
{
tem[i]=EEPROM.read(i);
}
HMIN= tem[11];
HMAX =tem[12];
if(HMIN >= 100) { HMIN = 65;}
else if(HMIN <= 65) {HMIN = 60; }
if(HMAX >=100){ HMAX=65;}
else if (HMAX <= 65) {HMAX = 60; }
}
void match(){
/if (currentMillis - previousMillis >= interval) {
//}
if((HMIN >= h) && (st==0) ){
digitalWrite(RELAYPIN, HIGH);
st=1;
sb=0;
}
if((HMAX <= h) && (sb==0)){
digitalWrite(RELAYPIN, LOW);
sb=1;
st=0;
}
}//void match