#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
// LiquidCrystal_I2C lcd(0x27,16,2); //Alamat I2C
int ledPin1 = 5;
int ledPin2 = 18;
int buzzer = 19;
#define TINGGI_LAYAR 64 // Tinggi layar OLED yang digunakan
#define LEBAR_LAYAR 128 // Lebar layar OLED yang digunakan
// int led1 = 5;
// int led2 = 34;
Adafruit_SSD1306 oled(LEBAR_LAYAR, TINGGI_LAYAR, &Wire, -1);
const byte ROWS = 4; //Jumlah baris keypad
const byte COLS = 4; //Jumlah kolom keypad
char Keys[ROWS][COLS] = { //Membuat array keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13,12,14,27};//connect to the row pinouts of the keypad
byte colPins[COLS] = {26,25,33,32};
Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); //Masukkan info keypad pada library
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 251199; //Password
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
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();
tone(buzzer, 5000);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(200);
noTone(buzzer);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(300);
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.setCursor(30, 30); // Atur posisi text pada display
oled.println("Firmansyah"); // Text yang dicetak
delay(2000);
oled.display();
oled.clearDisplay();// menampilkan display OLED
}
void loop() {
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(10,20);
oled.print("Masukkan 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(10,30);
number = number * 10 + (customKey - '0');
oled.print(number);
oled.display();
oled.clearDisplay();
break;
//------------Jika input '#' maka cek password------------//
case '#':
if(number == password){
tone(buzzer, 5000);
digitalWrite(ledPin1, HIGH);
delay(200);
noTone(buzzer);
digitalWrite(ledPin1, LOW);
delay(300);
Serial.println("Silahkan Masuk"); //Jika password benar, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(10,30);
oled.print("Silahkan Masuk"); //Tampilan Oled
number = 0;
oled.display();
oled.clearDisplay();
}
else{
tone(buzzer, 1000);
digitalWrite(ledPin2, HIGH);
delay(200);
noTone(buzzer);
digitalWrite(ledPin2, LOW);
delay(300);
tone(buzzer, 1000);
digitalWrite(ledPin2, HIGH);
delay(200);
noTone(buzzer);
digitalWrite(ledPin2, LOW);
delay(300);
tone(buzzer, 1000);
digitalWrite(ledPin2, HIGH);
delay(200);
noTone(buzzer);
digitalWrite(ledPin2, LOW);
delay(300);
Serial.println("Password Salah"); //Jika salah, maka
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.setCursor(10,30);
oled.print("Password Salah"); //Tampilan Oled
oled.display();
delay(2000);
number = 0;
oled.clearDisplay();
}
break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
oled.clearDisplay();
oled.setTextSize(1);
oled.setCursor(10, 20);
oled.print("Masukkan Password");
oled.display();
delay(100);
oled.clearDisplay();
break;
}
}/* Program Kunci Pintu dengan password input Keypad 4x4 dibuat oleh Indobot */