//https://drive.google.com/file/d/1MsZ4-idp3vIWSuvRrYgbxPfuDfzOzJSL/view?usp=sharing#include <Wire.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#include <IRremote.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
//LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define TFT_DC 22
#define TFT_CS 53
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//LED
const int greenLedPin = 7; // Pin connected to the green LED
const int yellowLedPin = 8; // Pin connected to the yellow LED
const int redLedPin = 9; // Pin connected to the red LED
const int lmpaLedPin = 10;
const int lmpbLedPin = 11;
const int lmpcLedPin = 12;
const int lmpdLedPin = 13;
int kondisi;
//KEYPAD
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte pin_rows[ROW_NUM] = {23, 25, 27, 29}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {31, 33, 35, 37}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//SERVO
Servo myservo;
//BUZZER
const int buzzerPin = 4; // Pin yang terhubung dengan buzzer
//ULTRASONIC
const int trigPin = 6;
const int echoPin = 5;
unsigned long previousMillis = 0;
const long interval = 100;
//deklarasi poten
int pot = A0;
float nilaiPot;
float nilaiMappingPot;
//LOGIN
String password = "0296";
String inputPassword = "";
//irremote
#define PIN_RECEIVER 2
IRrecv receiver(PIN_RECEIVER);
decode_results results;
bool lmpastate=LOW;
bool lmpbstate=LOW;
bool lmpcstate=LOW;
bool lmpdstate=LOW;
void setup() {
myservo.attach(3);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(lmpaLedPin, OUTPUT);
pinMode(lmpbLedPin, OUTPUT);
pinMode(lmpcLedPin, OUTPUT);
pinMode(lmpdLedPin, OUTPUT);
pinMode(pot, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
receiver.enableIRIn();
myservo.write(0);
lcd.init();
lcd.backlight();
lcd.setCursor(4, 1);
lcd.print(" - SISTEM - ");
lcd.setCursor(2, 2);
lcd.print(" - SMART HOME - ");
awal();
delay(200);
lcd.clear();
}
void loop() {
tahap();
}
void password1()
{
lcd.init();
int startColumn = (20 - password.length()) / 2; // Calculate starting column position
lcd.setCursor(startColumn, 1);
lcd.setCursor(2, 1);
lcd.print("Enter Password:");
while (inputPassword.length() < password.length())
{
char key = keypad.getKey();
if (key != NO_KEY)
{
inputPassword += key;
lcd.setCursor(startColumn + inputPassword.length() -1 ,2); // Set cursor position to the same column as the current length of inputPassword string minus 1, and row 1 (the second row)
lcd.print("*"); // Display character '*' at cursor position
}
}
if (inputPassword == password)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("####################");
lcd.setCursor(3,1);
lcd.print("Access Granted");
lcd.setCursor(2,2);
lcd.print("- Welcome Home -");
lcd.setCursor(0,3);
lcd.print("####################");
delay(3000);
pilih();
inputPassword="";
lcd.clear();
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("====================");
lcd.setCursor(3,1);
lcd.print("Access Denied");
lcd.setCursor(4,2);
lcd.print("unauthorized");
lcd.setCursor(7,3);
lcd.print("person");
delay(3000);
inputPassword="";
}
lcd.clear();
}
void awal()
{
for (int i = 0; i < 2; i++) {
digitalWrite(redLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(redLedPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
digitalWrite(yellowLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(yellowLedPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
digitalWrite(greenLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(greenLedPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
}
}
void pilih() {
tulisanlcd();
awalpilih:
lcd.clear(); // Clear the LCD
lcd.setCursor(0,0);
lcd.print("A : Buka pagar");
lcd.setCursor(0,1);
lcd.print("B : Buka pintu");
lcd.setCursor(0,2);
lcd.print("C : Sudah dlm rmh");
lcd.setCursor(0,3);
lcd.print("# : Proses deteksi");
bool inMenu = true;
bool conditionA = false;
bool conditionB = false;
bool conditionC = false;
while (inMenu) {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == 'A') {
if (conditionB) {
// do nothing
}
else if (conditionC) {
// do nothing
}
else{
conditionA = true;
lcd.clear();
kondisi = 1;
while(kondisi == true)
{
bukapagar();
goto awalpilih;
}
}
} else if (key == 'B') {
if (conditionA) {
// do nothing
}
else if (conditionC) {
// do nothing
} else {
conditionB = true;
lcd.clear();
kondisi = 1;
while(kondisi == true)
{
pintu();
goto awalpilih;
}
}
} else if (key == 'C') {
if (conditionA) {
// do nothing
}
else if (conditionB) {
// do nothing
} else {
conditionC = true;
lcd.clear();
kondisi = 1;
while(kondisi == true)
{
tulisan();
dlmrumah();
}
}
}else if (key == '#') {
inMenu = false;
}
}
}
}
void tahap()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 10) {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("- Ada Orang -");
lcd.setCursor(5,2);
lcd.print("<= 10 cm");
delay(3000);
password1();
//delay(3000);
} else {
lcd.setCursor(3,1);
lcd.print("Tidak ada orang");
lcd.setCursor(5,2);
lcd.print("- >= 10 cm -");
//delay(3000);
}
}
}
void bukapagar()
{
lcd.setCursor(0,0);
lcd.print("####################");
lcd.setCursor(1,1);
lcd.print("- Pagar dibuka -");
for (int i = 5; i >= 0; i--) {
lcd.setCursor(4, 2);
lcd.print("Countdown: ");
lcd.setCursor(9, 3);
lcd.print(i);
digitalWrite(greenLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(greenLedPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("####################");
lcd.setCursor(1,1);
lcd.print("- Pagar ditutup -");
for (int a = 5; a >= 0; a--) {
lcd.setCursor(4, 2);
lcd.print("Countdown");
lcd.setCursor(9, 3);
lcd.print(a);
digitalWrite(redLedPin, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(redLedPin, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
}
}
void pintu()
{
lcd.setCursor(0,0);
lcd.print("*******************");
lcd.setCursor(0,1);
lcd.print("- Silahkan masuk -");
myservo.write(90); // posisi servo ketika tombol B ditekan
for (int c = 5; c >= 0; c--) {
lcd.setCursor(4, 2);
lcd.print("Countdown: ");
lcd.setCursor(9, 3);
lcd.print(c);
delay(1000);
}
myservo.write(0); // kembali ke posisi awal
}
void dlmrumah()
{
if (receiver.decode()) {
switch (receiver.decodedIRData.command) {
case 48:
digitalWrite(lmpaLedPin,HIGH);
if(lmpastate = !lmpastate)
{
digitalWrite(lmpaLedPin,LOW);
}
break;
case 24:
digitalWrite(lmpbLedPin, HIGH);
if(lmpbstate = !lmpbstate)
{
digitalWrite(lmpbLedPin,LOW);
}
break;
case 122:
digitalWrite(lmpcLedPin, HIGH);
if(lmpcstate = !lmpcstate)
{
digitalWrite(lmpcLedPin,LOW);
}
break;
case 16:
digitalWrite(lmpdLedPin, HIGH);
if(lmpdstate = !lmpdstate)
{
digitalWrite(lmpdLedPin,LOW);
}
break;
case 194:
pilih();
break;
}
receiver.resume();
}
}
void tulisan()
{
lcd.setCursor(1,1);
lcd.print("Sudah di rumah");
}
void tulisanlcd()
{
tft.begin();
tft.setCursor(90, 1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("DAFTAR");
tft.setCursor(90, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("TOMBOL");
tft.setCursor(60, 60);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("1 > lampu A");
tft.setCursor(60, 100);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("2 > lampu B");
tft.setCursor(60, 140);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("3 > lampu C");
tft.setCursor(60, 180);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("4 > lampu D");
tft.setCursor(60, 220);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.println("++ NOTE ++");
tft.setCursor(90, 250);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.println("Pilih");
tft.setCursor(80, 270);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.println("Tekan C");
tft.setCursor(40, 300);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.println("Terlebih dulu");
}