// [][][][] [][][][] [] [] [][] [][][][]
// [] [] [] [] [] [] [] [] []
// [] [][] [] [] [] [] [] [] [] []
// [] [] [] [][][][] [][][][] [][][][] []
// [] [][] [] [] [] [] [] [] []
// [] [] [] [] [] [] [] []
// [][][][] [] [] [] [] [] []
// # คู่มือการใช้งานโปรแกรมล็อกรหัสด้วยแป้นพิมพ์และจอ OLED
// ## รายการอุปกรณ์
// 1. Arduino board (เช่น Arduino Uno)
// 2. แป้นพิมพ์ (Keypad) แบบ 4x4
// 3. จอแสดงผล OLED ขนาด 128x64 (Adafruit SSD1306)
// 4. สายไฟ (Jumper wires)
// ## การเชื่อมต่อฮาร์ดแวร์
// ### การเชื่อมต่อแป้นพิมพ์
// - **แถว (ROWS):**
// - Pin 23 (Arduino) -> Pin แถว 1 (Keypad)
// - Pin 19 (Arduino) -> Pin แถว 2 (Keypad)
// - Pin 18 (Arduino) -> Pin แถว 3 (Keypad)
// - Pin 5 (Arduino) -> Pin แถว 4 (Keypad)
// - **คอลัมน์ (COLS):**
// - Pin 4 (Arduino) -> Pin คอลัมน์ 1 (Keypad)
// - Pin 2 (Arduino) -> Pin คอลัมน์ 2 (Keypad)
// - Pin 15 (Arduino) -> Pin คอลัมน์ 3 (Keypad)
// - Pin 14 (Arduino) -> Pin คอลัมน์ 4 (Keypad)
// ### การเชื่อมต่อจอ OLED
// - **VCC** -> 3.3V หรือ 5V (ตามที่จอ OLED รองรับ)
// - **GND** -> GND (Arduino)
// - **SCL** -> SCL (Arduino)
// - **SDA** -> SDA (Arduino)
// ## การติดตั้งไลบรารี
// 1. เปิด Arduino IDE
// 2. ไปที่ Sketch -> Include Library -> Manage Libraries
// 3. ค้นหาและติดตั้งไลบรารี `Keypad` และ `Adafruit SSD1306`
// 4. ค้นหาและติดตั้งไลบรารี `Adafruit GFX` (ซึ่งเป็นไลบรารีที่จอ OLED ใช้)
// ## ขั้นตอนการใช้งานโปรแกรม
// 1. ต่อแผงวงจรตามการเชื่อมต่อที่ระบุ
// 2. เปิด Arduino IDE แล้วอัพโหลดโค้ดที่ให้ไปยังบอร์ด Arduino
// 3. เมื่อเปิดระบบ จะสามารถกดปุ่มบนแป้นพิมพ์เพื่อล็อกอินได้
// 4. กดปุ่มหมายเลขตามรหัสที่ต้องการป้อน
// 5. กด `#` เพื่อตรวจสอบรหัสที่ป้อน
// 6. หากรหัสถูกต้อง จะแสดง "Correct" บนจอ OLED และจะรอประมาณ 3 วินาทีเพื่อเคลียร์จอ
// 7. หากรหัสไม่ถูกต้อง จะแสดง "Wrong" บนจอ OLED และจะรอประมาณ 3 วินาทีเพื่อเคลียร์จอ
// 8. กด `D` เพื่อล้างรหัสที่ป้อนใหม่ในกรณีที่ต้องการเริ่มต้นใหม่
// โปรแกรมนี้ช่วยให้ผู้ใช้งานสามารถใช้แป้นพิมพ์และจอ OLED ในการล็อกอินด้วยรหัสผ่านที่กำหนดได้อย่างง่ายดาย
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 4, 2, 15, 14 };
uint8_t rowPins[ROWS] = { 23, 19, 18, 5 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String enteredCode = "";
const String correctCode = "1234";
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(0, 16);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
display.print(key);
display.display();
if (key == '#') {
if (enteredCode == correctCode) {
display.clearDisplay();
display.setCursor(0, 16);
display.println("Correct");
display.display();
delay(3000);
display.clearDisplay();
display.display();
enteredCode = "";
display.setCursor(0, 16);
} else {
display.clearDisplay();
display.setCursor(0, 16);
display.println("Wrong");
display.display();
delay(3000);
display.clearDisplay();
display.display();
enteredCode = "";
display.setCursor(0, 16);
}
} else if (key == 'D') {
display.clearDisplay();
display.display();
enteredCode = "";
display.setCursor(0, 16);
} else {
enteredCode += key;
}
}
}