//Welcome in the project Movh Rijki Supriyatna:(MMRS_Iky)
//Sistem Smart Lock Pintu Rumah Otomatis
#include <TM1637Display.h>
#include <IRremote.h>
#include <Servo.h>
// PIN SETUP
#define CLK 3
#define DIO 4
#define IR_PIN 2
#define BUZZER_PIN 5
#define SERVO_PIN 6
#define TRIG_PIN 7
#define ECHO_PIN 8
#define OVERRIDE_PIN 9 // Tombol simulasi fingerprint
TM1637Display display(CLK, DIO);
IRrecv irrecv(IR_PIN);
decode_results results;
Servo kunci;
String password = "123"; // Kode akses yang sah
String inputCode = "";
int salahCounter = 0;
bool aksesDiizinkan = false;
bool sistemAktif = false;
unsigned long waktuTerakhirAktif = 0;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(BUZZER_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(OVERRIDE_PIN, INPUT_PULLUP);
display.setBrightness(0x0f);
display.showNumberDec(0);
kunci.attach(SERVO_PIN);
kunci.write(0); // posisi terkunci
}
void loop() {
// Deteksi gerakan depan pintu
long durasi, jarak;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
durasi = pulseIn(ECHO_PIN, HIGH);
jarak = durasi * 0.034 / 2;
if (jarak < 30) {
sistemAktif = true;
waktuTerakhirAktif = millis();
tampilkanStatus("WAIT");
}
// Fingerprint override (simulasi tombol)
if (digitalRead(OVERRIDE_PIN) == LOW) {
aksesDiizinkan = true;
bukaKunci();
tampilkanStatus("OPEN");
delay(3000);
kunci.write(0);
tampilkanStatus("LOCK");
sistemAktif = false;
inputCode = "";
salahCounter = 0;
return;
}
if (sistemAktif && irrecv.decode(&results)) {
String tombol = decodeTombol(results.value);
Serial.println(tombol);
if (tombol == "OK") {
if (inputCode == password) {
aksesDiizinkan = true;
bukaKunci();
tampilkanStatus("OPEN");
delay(3000);
kunci.write(0);
tampilkanStatus("LOCK");
salahCounter = 0;
} else {
salahCounter++;
tone(BUZZER_PIN, 1000, 200);
tampilkanStatus("ERR");
delay(1000);
if (salahCounter >= 3) {
tampilkanStatus("LOCK");
delay(3000);
salahCounter = 0;
}
}
inputCode = "";
} else {
inputCode += tombol;
}
irrecv.resume();
}
// Reset sistem jika tidak aktif dalam 10 detik
if (sistemAktif && millis() - waktuTerakhirAktif > 10000) {
sistemAktif = false;
inputCode = "";
tampilkanStatus("LOCK");
}
}
String decodeTombol(unsigned long val) {
switch (val) {
case 0xFF30CF: return "1";
case 0xFF18E7: return "2";
case 0xFF7A85: return "3";
case 0xFF10EF: return "4";
case 0xFF38C7: return "5";
case 0xFF5AA5: return "6";
case 0xFF42BD: return "7";
case 0xFF4AB5: return "8";
case 0xFF52AD: return "9";
case 0xFF6897: return "0";
case 0xFF22DD: return "OK";
default: return "";
}
}
void bukaKunci() {
kunci.write(90);
}
void tampilkanStatus(String status) {
if (status == "OPEN") display.showNumberDec(1111, true);
else if (status == "LOCK") display.showNumberDec(0, true);
else if (status == "WAIT") display.showNumberDec(8888, true);
else if (status == "ERR") display.showNumberDec(9999, true);
}