#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#include <ESP32Servo.h>

#define TINGGI_LAYAR 64 
#define LEBAR_LAYAR 128 

Servo myservo;
const byte ROWS = 4;    
const byte COLS = 4;    

int buzzer = 25;

Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, & Wire, -1);

char Keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 14, 12, 19, 18 }; 
byte colPins[COLS] = { 5, 4, 2, 15 };

Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);   


char customKey;         
int number = 0;         
int password = 1009;    

void setup() {
  Serial.begin(9600);
  myservo.attach(26);


  
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
  }
  oled.clearDisplay();
  
  oled.setTextSize(1.5);        
  oled.setTextColor(WHITE);    
  oled.setCursor(0, 10);       
  oled.println("SMART LOCKER  PAHRUL ROZI"); 
  delay(1000);
  oled.display(); 
  oled.clearDisplay();
}

void loop() {
  oled.setTextSize(1);
  oled.setTextColor(WHITE); 
  oled.setCursor(0,0);
  oled.print("LOCKER PAHRUL ROZI");
  oled.setCursor(0,15);
  oled.print("MASUKKAN PASSWORD");      
  
  customKey = customKeypad.getKey();    

 
  switch(customKey){
    case '0' ... '9':
      oled.setTextSize(1);
      oled.setTextColor(WHITE); 
      oled.setCursor(0,25);
      number = number * 10 + (customKey - '0');
      oled.print(number);
      oled.display();
      oled.clearDisplay(); 
    break;

   
    case '#':
      if(number == password){       
        oled.setTextSize(1);
        oled.setTextColor(WHITE); 
        oled.setCursor(0,25);
        oled.print("PASSWORD DITERIMA ");  
        oled.display();
        oled.clearDisplay();
        number = 0;
        myservo.write(180);
        delay(5000);
        myservo.write(90);
        oled.display();
        oled.clearDisplay();
        
      }
      else{                             
        oled.setTextSize(1);
        oled.setTextColor(WHITE); 
        oled.setCursor(0,25);
        oled.print("PASSWORD DITOLAK");  
        oled.display();
        oled.clearDisplay();
      }
    break;

   
    case '*':
      number = 0;
      oled.clearDisplay();
    break;
  }
}