#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#include <Servo.h>
// Deklarasi jumlah baris dan kolom pada keypad;
const byte row = 4;
const byte col = 4;
//Servo
Servo servo;
const int relayPin = 5;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Deklarasi posisi karakter pada keypad
const char arr[row][col] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Deklarasi
byte pinRow[row] = {13, 12, 11, 10};
byte pinCol[col] = {9, 8, 7, 6};
Keypad matriks = Keypad(makeKeymap(arr), pinRow, pinCol, row, col);
String inputPassword = ""; // Inisialisasi string untuk menyimpan input password
const String password = "1234567";
void setup() {
Serial.begin(9600);
// Inisialisasi layar OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
// Setup Servo
servo.attach(5); // Assign pin servo
servo.write(0);
// Setup Relay
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
}
void loop() {
char btn = matriks.getKey();
if (btn != NO_KEY && inputPassword.length() < 7) {
inputPassword += btn;
Serial.println(inputPassword);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Code acces:");
display.println(inputPassword);
display.display();
delay(1000);
}
if (inputPassword.length() == 7) {
if (inputPassword == password) {
Serial.println("password is correct and Open the door");
servo.write(90);
}else {
Serial.println("incorrect password");
}
inputPassword = "";
}
}