#include <Keypad.h>
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define ROW_NUM    4  
#define COLUMN_NUM 4   
#define SERVO_PIN  A0 

Servo servo;  

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1', '2', '3', 'A'}, //  '1','4','7','*'
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6};  
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};  

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

const String password = "ABC";  //bisa diganti password 
String input_password;

int angle = 0;  
unsigned long lastTime;

void setup() {
  Serial.begin(9600);
  input_password.reserve(32);  

  servo.attach(SERVO_PIN);
  servo.write(0); 
  lastTime = millis();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  display.clearDisplay(); 
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Serial.println(key);

    if (key == '*') {
      input_password = "";  
    } else if (key == '#') {
      if (input_password == password) {
        Serial.println("Password Benar");
        angle = 90;
        servo.write(angle);
        lastTime = millis();
        display.setCursor(0,0);
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.println("Password Benar");
        display.display();
        display.clearDisplay();
      } else {
        Serial.println("Password salah");
        display.setCursor(0,0);
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.println("Password  Salah");
        display.display();
        display.clearDisplay();
      }

      input_password = ""; 
    } else {
      input_password += key;  
    }
  }

  if (angle == 90 && (millis() - lastTime) > 5000) {  
    angle = 0;
    servo.write(angle);
    display.setCursor(0,0);
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.print("Masukkan Password");
    display.display();
    display.clearDisplay();
  }
}