#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>
#define DHTPIN 16 // Pin dimana sensor DHT22 terhubung
#define DHTTYPE DHT22 // Tipe sensor DHT
DHT dht(DHTPIN, DHTTYPE); // Inisialisasi objek DHT
RTC_DS1307 rtc;
float temperature = 0;
float humidity = 0;
//unsigned long previousMillis1 = 0;
bool ld1 = false;
bool ld2 = false;
bool ld3 = false;
bool ld4 = false;
bool ext = false;
bool ext1 = false;
bool auto1 = false;
bool auto2 = false;
float thth1 = 25.00;
float thth2 = 27.00;
float thtl1 = 23.00;
float thtl2 = 24.00;
unsigned long previousMillis1 = 0;
const long interval = 2100; // Delay interval in milliseconds
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
//const long interval = 2100;
const long interval1 = 5000;
const long interval2 = 10000;
bool status1 = false;
bool status2 = false;
bool status11 = false;
bool status22 = false;
bool menu = false;
bool statusACAB = false;
bool statusACCD = false;
const int ledPin1 = 33; // Pin untuk LED 1
const int ledPin2 = 25; // Pin untuk LED 2
const int ledPin3 = 26; // Pin untuk LED 3
const int ledPin4 = 27; // Pin untuk LED 4
const int pb1 = 2;
const int pb2 = 0;
const int pb3 = 5;
const int pb4 = 17;
int count1 = 1;
int count2 = 1;
int thabon = 10;
int thcdon = 20;
int thaboff = 26;
int thcdoff = 16;
int thjd1 = 11;
int thjd2 = 21;
int thjd22 = 17;
int thjd11 = 27;
unsigned long previousoto = 0;
unsigned long previousMillis = 0; // Waktu sebelumnya Alarm
unsigned long previoussinyal = 0; // Waktu sebelumnya Notifikasi
const long AlarmDuration = 30000; // Durasi LED 1 aktif (30 detik)
const long sinyalInterval = 5000;
const long Interval9 = 1000;
const long otointerval = 3000;
unsigned long previousreset1 = 0;
unsigned long previousoff1 = 0;
unsigned long previousoff2 = 0;
const long intervalreset1 = 60000;
const long intervaloff1 = 300000;
const long intervaloff2 = 5000;
bool reset1 = false;
bool off1 = false;
int thresholdaktif1 = 15;
int thresholdaktif2 = 25;
// Variabel untuk menandai status LED 1 dan LED 2
bool led1State = false;
bool led2State = false;
bool led3State = false;
bool led4State = false;
void setup() {
Serial.begin(9600);
dht.begin();
Wire.begin();
pinMode(ledPin1, OUTPUT); // LED 1 sebagai output
pinMode(ledPin2, OUTPUT); // LED 2 sebagai output
pinMode(ledPin3, OUTPUT); // LED 3 sebagai output
pinMode(ledPin4, OUTPUT); // LED 4 sebagai output
pinMode(pb1, INPUT_PULLUP);
pinMode(pb2, INPUT_PULLUP);
pinMode(pb3, INPUT_PULLUP);
pinMode(pb4, INPUT_PULLUP);
#ifndef ESP8266
while (!Serial); // Wait for serial port to connect. Needed for native USB
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
unsigned long currentMillis = millis();
DateTime now = rtc.now();
int spb1 = digitalRead(pb1);
int spb2 = digitalRead(pb2);
int spb3 = digitalRead(pb3);
int spb4 = digitalRead(pb4);
//bool statusACAB = false;
//bool statusACCD = false;
if(spb1 == 0){
thabon += count1;
thcdon += count1;
thaboff += count1;
thcdoff += count1;
thjd1 += count1;
thjd2 += count1;
thjd22 += count1;
thjd11 += count1;
}
if(spb2 == 0){
thabon -= count2;
thcdon -= count2;
thaboff -= count2;
thcdoff -= count2;
thjd1 -= count2;
thjd2 -= count2;
thjd22 -= count2;
thjd11 -= count2;
}
if (menu == true){
status1 = false;
status2 = false;
status11 = false;
status22 = false;
}
if(spb3 == 0){
menu = false;
}
if(spb4 == 0){
menu = true;
}
if (now.minute() >= thabon) {
status1 = true;
status2 = false;
statusACAB = true;
statusACCD = false;
ld1 = true;
ld2 = true;
ld3 = false;
ld4 = false;
//Serial.println("memulai led 1, 2 ");
//previousreset1 = millis(); // Reset waktu Alarm
}
if (status1 == true && (now.minute() >= thjd1)) {
status1 = false;
off1 = true;
//Serial.println("reset status1 ");
//previousreset1 = millis(); // Reset waktu Alarm
}
if (off1 == true && (now.minute() >= thcdoff)) {
status22 = true;
off1 = false;
ld3 = false;
ld4 = false;
//Serial.println("mengakhiri led 3, 4 ");
//previousoff1 = millis();
}
if (status22 == true && (now.minute() >= thjd22)) {
status22 = false;
//Serial.println("reset status22 ");
//previousreset1 = millis();
}
if (now.minute() >= thcdon) {
status1 = false;
status2 = true;
statusACAB = false;
statusACCD = true;
ld1 = false;
ld2 = false;
ld3 = true;
ld4 = true;
//Serial.println("memulai led 3, 4 ");
//previousreset1 = millis(); // Reset waktu Alarm
}
if (status2 && (now.minute() >= thjd2)) {
status2 = false;
off1 = true;
//Serial.println("reset status2 ");
//previousreset1 = millis(); // Reset waktu Alarm
}
if (off1 && (now.minute() >= thaboff)) {
status11 = true;
off1 = false;
ld1 = false;
ld2 = false;
//Serial.println("mengakhiri led 1, 2 ");
//previousoff1 = millis();
}
if (status11 && (now.minute() >= thjd11)) {
status11 = false;
//Serial.println("reset status11 ");
//previousreset1 = millis();
}
unsigned long currentsinyal = millis();
if (currentsinyal - previoussinyal >= sinyalInterval) {
previoussinyal = currentsinyal;
if (status1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
Serial.println("led 1, 2 on ");
}
if (status2) {
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
Serial.println("led 3, 4 on ");
}
if (status11) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
Serial.println("led 1, 2 off ");
}
if (status22) {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
Serial.println("led 3, 4 off ");
}
}
if(statusACAB){
Serial.println("AC-AB");
}
if(statusACCD){
Serial.println("AC-CD");
}
if (currentMillis - previousMillis >= Interval9) {
previousMillis = currentMillis;
Serial.print("Time: ");
Serial.print(String(now.year()) + "/" + String(now.month()) + "/" + String(now.day()));
Serial.println();
Serial.print(String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()));
Serial.println();
Serial.print(spb1);
Serial.print(spb2);
Serial.print(spb3);
Serial.print(spb4);
Serial.print(menu);
Serial.print(status1);
Serial.print(status2);
Serial.print(status11);
Serial.println(status22);
Serial.print(thabon);
Serial.println(thcdon);}
unsigned long cekstatus = millis();
if (currentMillis - previousMillis1 >= interval) {
previousMillis1 = currentMillis;
float h = dht.readHumidity();
float t = dht.readTemperature(); // put your main code here, to run repeatedly:
Serial.print("Temp: ");
Serial.print(t);
Serial.print("C");
Serial.print(" ");
Serial.print("Hum: ");
Serial.print(h);
Serial.println("%");
if(t >= thth1){
ext = true;
previousMillis2 = cekstatus;
//auto1 = true;
}
if (t >= thth2){
ext1 = true;
previousMillis3 = cekstatus;
//auto2 = true;
}
if(ext == true && ( cekstatus - previousMillis2 >= interval2)){
if(t < thtl1){
ext = false;
Serial.println("1aman");
previousMillis2 = cekstatus;
//auto1 = false;
}}
if(ext1 == true && ( cekstatus - previousMillis3 >= interval1)){
if(t <= thtl2){
ext1 = false;
Serial.println("2aman");
previousMillis3 = cekstatus;
//auto2 = false;
}}}
if (ext == true){
if(ld1 == false && ext == true && statusACAB == false){
ld1 = true;
//Serial.print("status 1");
}
if(ld3 == false && ext == true && statusACCD == false){
ld3 = true;
//Serial.print("status 2");
}
auto1 = true;
}
if (ext1 == true){
if(ld2 == false && ext1 == true && statusACAB == false){
ld2 = true;
//Serial.print("status 3");
}
if(ld4 == false && ext1 == true && statusACCD == false){
ld4 = true;
//Serial.println("status 4");
}
auto2 = true;
}
//-------------------------------------------------------------------//
if (ext == false && auto1 == true){
if(ld1 == true && ext == false && statusACAB == false){
ld1 = false;
//Serial.print("status 1 off");
}
if(ld3 == true && ext == false && statusACCD == false){
ld3 = false;
//Serial.print("status 2 off");
//previousMillis2 = currentMillis;
auto1 = false;
}
}
if (ext1 == false && auto2 == true){
if(ld2 == true && ext1 == false && statusACAB == false){
ld2 = false;
//Serial.print("status 3 off");
}
if(ld4 == true && ext1 == false && statusACCD == false){
ld4 = false;
//Serial.println("status 4 off");
auto2 = false;
}
}
unsigned long currentoto = millis();
//if(ext == true || ext1 == true){
if(currentoto - previousoto >= otointerval){
previousoto = currentoto;
if(ld1 == true){
digitalWrite(ledPin1, 1);
}
if(ld1 == false){
digitalWrite(ledPin1, 0);
}
if(ld2 == true){
digitalWrite(ledPin2, 1);
}
if(ld2 == false){
digitalWrite(ledPin2, 0);
}
if(ld3 == true){
digitalWrite(ledPin3, 1);
}
if(ld3 == false){
digitalWrite(ledPin3, 0);
}
if(ld4 == true){
digitalWrite(ledPin4, 1);
}
if(ld4 == false){
digitalWrite(ledPin4, 0);
}}
//}
delay(1000);
}