#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //กำหนดค่า Address ของจอ LCD
#define ledRED 11
#define ledGREEN 12
#define buzzerPin 10
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {9,8,7,6}; // กำหนดขาที่เชื่อมต่อกับ Keypad
byte colPins[COLS] = {5,4,3,2}; //
Keypad keypad = Keypad(makeKeymap(keys),rowPins,
colPins, ROWS, COLS);
char PIN[6] = {'1','2','3','4','5','6'};
char attempt[6] = {0,0,0,0,0,0}; //หนว่ยความจำอาร์เรย์สำหรับเก็บค่าที่ป้อนเข้าไป
int z = 0;
bool state;
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(ledRED, OUTPUT);
pinMode(ledGREEN, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledGREEN, 0);
digitalWrite(ledRED, 0);
}
void correctPIN()
{ //ทำงานเมื่อการกดคีย์ถูกต้อง
Serial.println("Correct PIN"); //แสดงผลว่าถูกต้อง
Serial.println();
//lcd.clear();
lcd.setCursor(0,1);
lcd.print("Correct PIN");
digitalWrite(ledGREEN, 1);
correct_sound();
delay(2000);
noTone(buzzerPin);
digitalWrite(ledGREEN, 0);
}
void incorrectPIN()//ทำงานเมื่อการกดคีย์ไม่ถูกต้อง
{
Serial.println("Incorrect PIN!"); //แสดงผลว่ารหัสผิดพลาด
Serial.println();
//lcd.clear();
lcd.setCursor(0,1);
lcd.print("Incorrect PIN!");
digitalWrite(ledRED, 1);
incorrect_sound();
delay(2000);
digitalWrite(ledRED, 0);
}
void loop() {
state = true;
Serial.print("Enter PIN : ");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("PIN :");
while(state == true)
{
readKeypad(); //เรียกใช้ฟังก์ชันอ่านค่าคีย์
}
}
void checkPIN() //ตรวจสอบรหัสที่กดเข้าไปว่าตรงกับรหัสที่ตั้งไว้หรือไม่
{
int correct = 0;
int i;
if(z > 6){
incorrectPIN();
}
else{
for(i = 0; i< 6; i++){ //ทำซ้ำ 6 ครั้งตามจำนวนรหัสผ่าน
if(attempt[i] == PIN[i]){ //เปรียบเทียบค่าที่ป้อนเข้ามาทีละหลัก
correct++; //ถ้าตรงเพิ่มค่าตัวนับ
}
}
if(correct == 6){ //ถ้าตัวนับเท่ากับ 6 แสดงว่าถูกต้อง
correctPIN(); //เรียกฟังก์ชันแสดงว่าถูกต้อง
}
else
{
incorrectPIN(); //ถ้าไม่เท่ากับ 6 แสดงว่าผิดพลาด
}
}
Reset_value();
}
void readKeypad()//อ่านค่าคีย์รหัสผ่านที่กดเข้ามาทีละคีย์
{
char key = keypad.getKey(); //อ่านค่าครั้งละคีย์
if( key != NO_KEY ){
delay(200);
switch(key) //ตรวจสอบคีย์
{
case '*': //ถ้ากด * ให้เริ่มใหม่ค่าแรก
z = 0;
Reset_value();
Serial.println("Reset value.......");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Reset value.......");
reset_sound();
state = false;
break;
case '#': //ถ้ากดคีย์ # หมายวามว่าป้อนรหัสครบหรือยืนยัน
delay(100);
Serial.println();
checkPIN(); //ตรวจสอบคีย์ที่
state = false;
break;
case 'D': //ถ้ากดคีย์ # หมายวามว่าป้อนรหัสครบหรือยืนยัน
delay(100);
attempt[z] = ""; //นำแต่ละคีย์เก็บในหน่วยความจำ
Serial.print(attempt[z]);
lcd.setCursor((z+5),0);
lcd.print(" ");
z--;
if(z < 0 ){
z = 0;
}
if(z > 6 ){
z = 6;
}
Serial.println(z);
break;
default:
delay(100);
attempt[z] = key; //นำแต่ละคีย์เก็บในหน่วยความจำ
z++;
Serial.print(key);
lcd.setCursor((z+5),0);
lcd.print(key);
tone(buzzerPin, 523, 100);
//noTone(buzzerPin);
}
}
}
void Reset_value(){
int t;
for(t = 0; t <= 6; t++){ //เคลียร์ค่าในหน่วยความจำให้เป็น 0
attempt[t] = 0;
}
z = 0;
}