#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
// Deklarasi jumlah baris dan kolom pada keypad
const byte row = 4;
const byte col = 4;
// Deklarasi posisi karakter keypad
const char arr[row][col] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Deklarasi pin keypad yang terhubung dengan Arduino
const byte pinRow[row] = {9, 8, 7, 6};
const byte pinCol[col] = {10, 11, 12, 13};
// Inisialisasi keypad
Keypad matriks = Keypad(makeKeymap(arr), pinRow, pinCol, row, col);
// OLED display width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Servo object
Servo myServo;
const byte relayPin1 = 5;
const byte relayPin2 = 4;
// Password yang diharapkan
const String correctPassword = "123456A";
// Buffer to store input keys
String inputKeys = "";
void setup() {
Serial.begin(9600);
myServo.attach(3); // Attach the servo to pin 3
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
pinMode(relayPin1, OUTPUT);
digitalWrite(relayPin1, LOW);
pinMode(relayPin2, OUTPUT);
digitalWrite(relayPin2, LOW);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.display();
myServo.write(90); // Set servo to closed position initially (assuming 90 means closed)
}
void loop() {
char key = matriks.getKey();
if (key != NO_KEY) {
inputKeys += key; // Append the key pressed to the input string
// Update display
display.clearDisplay();
display.setCursor(0, 0);
display.print("Input: ");
display.print(inputKeys);
display.display();
if (inputKeys.length() == correctPassword.length()) {
if (inputKeys == correctPassword) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Open");
display.display();
myServo.write(0); // Open the lock
} else {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Password salah");
display.display();
// Do not move the servo if the password is wrong
}
inputKeys = ""; // Reset input after checking
}
}
}