#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte ROWS = 4; // Jumlah baris keypad
const byte COLS = 4; // Jumlah kolom keypad
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}; // R1, R2, R3, R4
byte colPins[COLS] = {5, 4, 2, 15}; // C1, C2, C3, C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char *password = "1234";
char enteredPassword[5];
int passwordIndex = 0;
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display(); // Show initial display buffer contents on the screen
display.clearDisplay(); // Clear the buffer
display.setTextSize(1); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color
display.setCursor(20,25); // Set cursor position
display.println("Selamat Datang"); // Display welcome message
display.display(); // Show initial display buffer contents on the screen
delay(2000); // Pause for 2 seconds
display.clearDisplay(); // Clear the buffer
display.display(); // Show initial display buffer contents on the screen
display.clearDisplay(); // Clear the buffer
display.setTextSize(1); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color
display.setCursor(0, 0); // Set cursor position
display.println("Input Password:"); // Display input password message
display.display(); // Show initial display buffer contents on the screen
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Tombol untuk memeriksa password
if (strcmp(enteredPassword, password) == 0) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Access Accepted");
display.display();
} else {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Invalid password");
display.display();
}
delay(2000);
display.clearDisplay();
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
display.setCursor(0, 0); // Set cursor position
display.println("Input Password:"); // Display input password message
display.display(); // Show initial display buffer contents on the screen
} else if (key == '*') { // Tombol untuk menghapus layar
display.clearDisplay();
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
display.setCursor(0, 0); // Set cursor position
display.println("Input Password:"); // Display input password message
display.display(); // Show initial display buffer contents on the screen
} else {
if (passwordIndex < 4) {
enteredPassword[passwordIndex] = key;
passwordIndex++;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Input Password:"); // Display input password message
for (int i = 0; i < passwordIndex; i++) {
display.print("*");
}
display.display(); // Show initial display buffer contents on the screen
}
}
}
}