#include <Wire.h>
#include <RTClib.h>
#include <Keypad.h>
RTC_DS1307 rtc;
const int lampuLuar[] = {12, 13, A0};
const int lampuDalam[] = {A1, A2, A3};
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
const int correctPassword = 1234;
long int enteredPassword = 0;
bool isLocked = true;
unsigned long unlockTime = 0;
void setup() {
Serial.begin(115200);
for (int i = 0; i < 3; i++) {
pinMode(lampuLuar[i], OUTPUT);
pinMode(lampuDalam[i], OUTPUT);
}
Wire.begin();
if (!rtc.begin()) {
Serial.println("Model RTC tidak terdeteksi");
while (1);
}
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(11, HIGH); // LED 11 selalu menyala pada awalnya
}
void loop() {
DateTime now = rtc.now();
int jam = now.hour();
int menit = now.minute();
int detik = now.second();
// Keterangan waktu di Serial Monitor
Serial.print("Waktu: ");
Serial.print(jam);
Serial.print(":");
Serial.print(menit);
Serial.print(":");
Serial.println(detik);
// Delay 1 detik
delay(1000);
// Lampu luar selalu menyala
for (int i = 0; i < 3; i++) {
digitalWrite(lampuLuar[i], HIGH);
}
// Kondisi untuk lampu dalam
bool lampuRuangTamuHidup = (detik >= 19 && detik < 20);
bool lampuKamarHidup = (detik >= 18 && detik < 21);
bool lampuDapurHidup = (detik >= 18 && detik < 20);
digitalWrite(lampuDalam[0], lampuRuangTamuHidup ? HIGH : LOW);
digitalWrite(lampuDalam[1], lampuKamarHidup ? HIGH : LOW);
digitalWrite(lampuDalam[2], lampuDapurHidup ? HIGH : LOW);
char key = keypad.getKey();
unsigned long currentTime = millis();
// Penanganan keypad
if (key >= '0' && key <= '9') {
int digit = key - '0';
enteredPassword = enteredPassword * 10 + digit;
Serial.println(enteredPassword);
if (enteredPassword == correctPassword) {
if (isLocked) {
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
isLocked = false;
unlockTime = currentTime;
}
enteredPassword = 0;
} else if (enteredPassword > 9999) {
enteredPassword = 0;
if (!isLocked) {
digitalWrite(10, LOW);
}
}
}
if (!isLocked && currentTime - unlockTime > 5000) {
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
isLocked = true;
}
}