#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan
#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
#define red 25
#define green 27
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
const byte BARIS = 4; //Jumlah Baris Keypad
const byte KOLOM = 4; //Jumlah Kolom Keypad
char hexaKeys[BARIS][KOLOM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
uint8_t colPins[KOLOM] = { 5, 4, 2, 15 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[BARIS] = { 22, 21, 19, 18 }; // Pins connected to R1, R2, R3, R4
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, BARIS, KOLOM);
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 1379;
int password2 = 2222; //Password
void setup() {
Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.display();
delay(2000);
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(20, 20); // Atur posisi text pada display
oled.print("SELAMAT DATANG"); // Text yang dicetak
oled.display();
delay(2000);
oled.clearDisplay();// menampilkan display OLED
oled.display();
}
void loop() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.print("Input Password"); //Tampilan pada layar LCD
oled.display();
customKey = customKeypad.getKey(); //Baca input keypad
//------------Prosedur jika input berupa angka------------//
switch(customKey){
case '0' ... '9':
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
number = number * 10 + (customKey - '0');
oled.print(number);
oled.display();
break;
//------------Jika input '#' maka cek password------------//
case '#':
if(number == password||number == password2){ //Jika password benar, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.clearDisplay();
oled.display();
delay(10);
oled.setCursor(0,10);
oled.print("Access Accepted "); //Tampilan LCD
oled.display();
digitalWrite(green, HIGH);
delay(2000);
digitalWrite(green, LOW);
number = 0;
oled.clearDisplay();
oled.display();
}
else{ //Jika salah, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.clearDisplay();
oled.display();
oled.setCursor(0,10);
oled.print("Invalid Password"); //Tampilan LCD
oled.display();
digitalWrite(red, HIGH);
oled.display();
delay(2000);
digitalWrite(red, LOW);
number = 0;
oled.clearDisplay();
oled.display();
}
break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
oled.clearDisplay();
break;
}
}/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */