//len
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
int urutan = 1;
const int PINLED = 12;
const byte ROWS = 4;
const byte COLS = 4;
char Keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte rowPins[ROWS] = { 9, 8, 7, 6 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad customKEY = Keypad(makeKeymap(Keys), rowPins,
colPins, ROWS, COLS);
String INPUT_PW = "";
String PW[3] = { "1122", "2233", "3311" };
int trigPin = 11;
int echoPin = 10;
void setup() {
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(PINLED, OUTPUT);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("PASSWORD ");
lcd.print(urutan);
lcd.print(": ");
char KEY = customKEY.getKey();
if (KEY >= '0' && KEY <= '9') {
INPUT_PW += KEY;
lcd.setCursor(0, 1);
lcd.print(INPUT_PW);
}
if (KEY == '#') {
if (INPUT_PW == PW[urutan - 1]) {
lcd.setCursor(0, 2);
lcd.print("PASSWORD BENAR");
delay(1000);
lcd.clear();
INPUT_PW = "";
urutan++;
if (urutan == 4) {
lcd.setCursor(0, 0);
lcd.print("SENSOR WATERLEVEL");
lcd.setCursor(0, 1);
lcd.print("SIAP MEMONITORING");
delay(1000);
lcd.clear();
while (true) {
float distance = calculateDistance();
lcd.setCursor(0, 0);
lcd.print("JARAK: ");
lcd.print(distance);
lcd.setCursor(10, 0);
lcd.print(" cm");
delay(200);
KEY = customKEY.getKey();
if (KEY == '*') {
lcd.clear();
urutan = 1;
break;
}
}
}
} else {
lcd.setCursor(0, 2);
lcd.print("PASSWORD SALAH");
digitalWrite(PINLED, HIGH);
delay(1000);
digitalWrite(PINLED, LOW);
INPUT_PW = "";
lcd.clear();
}
}
if (KEY == '*') {
INPUT_PW = "";
lcd.clear();
urutan = 1;
}
}
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn(echoPin, HIGH);
float distance = duration / 2 * 0.034;
return distance;
}