#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <ESP32Servo.h>
#define LEBAR 128
#define TINGGI 64
#define SERVO 18
#define BUZZ 19
Adafruit_SSD1306 oled(LEBAR,TINGGI, &Wire, -1);
Servo servo;
const int BARIS = 4;
const int KOLOM = 4;
char keys[BARIS][KOLOM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
// R1 = 23. R2 = 19, R3 = 18, R4 = 5
// C1 = 4. C2 = 2, C3 = 15, C4 = 13
byte rowPins[BARIS] = {13,12,14,27};
byte colPins[KOLOM] = {5,4,2,15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, BARIS, KOLOM);
char keyInput;
int number = 0;
int password = 98;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
servo.attach(SERVO);
pinMode(BUZZ, OUTPUT);
servo.write(90);
if(!oled.begin(SSD1306_SWITCHCAPVCC, 0x3c)){
Serial.println(F("Falied to start OLED"));
while(1);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,16);
oled.println("Hello Security");
oled.display();
delay(2000);
oled.clearDisplay();
}
void loginOLED(){
keyInput = keypad.getKey();
oled.setTextSize(2);
oled.setTextColor(1);
oled.setCursor(0,0);
oled.print("Input pass: ");
switch(keyInput){
case '0' ... '9':
oled.setTextSize(2);
oled.setTextColor(1);
oled.setCursor(0,20);
number = number * 10 + (keyInput - '0');
oled.print(number);
oled.display();
oled.clearDisplay();
break;
case '#':
if(number == password){
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0,20);
oled.print("Access Accepted "); //Tampilan LCD
servo.write(0);
number = 0;
oled.display();
oled.clearDisplay();
delay(2000);
servo.write(90);
}else{
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0,20);
oled.print("Access Denied "); //Tampilan LCD
tone(BUZZ, 150, 100);
number = 0;
oled.display();
oled.clearDisplay();
}
break;
case '*':
number = 0;
oled.clearDisplay();
break;
}
}
void loop() {
loginOLED();
}