#include <Keypad.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
uint8_t colPins[COLS] = { 15, 2, 4 }; // Pins connected to C1, C2, C3
uint8_t rowPins[ROWS] = { 27, 26, 25, 32 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Adafruit_SSD1306 oled = Adafruit_SSD1306(128, 64, &Wire);
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 1379; //Password
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.clearDisplay();
oled.setTextSize(1); // Atur ukuran text
oled.setTextColor(WHITE); // Atur warna text
oled.setCursor(20, 20); // Atur posisi text pada display
oled.println("SELAMAT DATANG"); // Text yang dicetak
delay(2000);
oled.display();
oled.clearDisplay();// menampilkan display OLED
}
void loop() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,0);
oled.print("Input Password"); //Tampilan pada layar LCD
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();
oled.clearDisplay();
break;
//------------Jika input '#' maka cek password------------//
case '#':
if(number == password){ //Jika password benar, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
oled.print("Access Accepted "); //Tampilan LCD
number = 0;
oled.display();
oled.clearDisplay();
}
else{ //Jika salah, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(0,10);
oled.print("Invalid Password"); //Tampilan LCD
oled.display();
delay(2000);
number = 0;
oled.clearDisplay();
}
break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
oled.clearDisplay();
oled.setCursor(0,0);
oled.print("Input Password");
oled.display();
break;
}
}/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */