/*#include <Keypad.h>
#include <LiquidCrystal_I2C.h> // Khai báo thư viện LCD sử dụng I2C
#include <Wire.h>
#include <EEPROM.h>
#define BUZZER_PIN 12
#define LED_PIN 13
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 địa chỉ LCD, 16 cột và 2 hàng
const byte rows = 4; //số hàng
const byte columns = 4; //số cột
char STR[4] = {'1', '2', '3', '4'}; // Cài đặt mật khẩu tùy ý
char str[4] = {' ', ' ', ' ', ' '};
int i = 0, j = 0, count = 0;
int countFailedPassword = 0;
int countFailedFingerPr = 0;
bool passwordTrue = false;
bool fingerTrue = false;
//Định nghĩa các giá trị trả về
char keys[rows][columns] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[rows] = {2, 3, 4, 5}; //Cách nối chân với Arduino
byte columnPins[columns] = {15, 16, 17, 18};
//cài đặt thư viện keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);
void setup() {
Serial.begin(9600);//bật serial, baudrate 9600=
lcd.init(); // Khai báo sử dụng LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.print(" Enter Password");
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
buzzerBeep();
Serial.println(key);
// for (i ==0; i<4;i++){
// str[0] = key;
// lcd.setCursor(6, i);
// lcd.print(str[0]);
// delay(500);
// lcd.setCursor(6, i);
// lcd.print("*");
// }
// Nhập mật khẩu
if (i == 0) {
str[0] = key;
lcd.setCursor(6, 1);
lcd.print(str[0]);
delay(500); // Ký tự hiển thị trên màn hình LCD trong 1s
lcd.setCursor(6, 1);
lcd.print("*"); // Ký tự được che bởi dấu *
}
if (i == 1) {
str[1] = key;
lcd.setCursor(7, 1);
lcd.print(str[1]);
delay(500);
lcd.setCursor(7, 1);
lcd.print("*");
}
if (i == 2) {
str[2] = key;
lcd.setCursor(8, 1);
lcd.print(str[2]);
delay(500);
lcd.setCursor(8, 1);
lcd.print("*");
}
if (i == 3) {
str[3] = key;
lcd.setCursor(9, 1);
lcd.print(str[3]);
delay(500);
lcd.setCursor(9, 1);
lcd.print("*");
count = 1;
}
i = i + 1;
}
if (count == 1) {
if (str[0] == STR[0] && str[1] == STR[1] && str[2] == STR[2] &&
str[3] == STR[3]) {
lcd.clear();
lcd.print(" Correct!");
buzzerTruePassword();
countFailedPassword = 0;
passwordTrue = true;
digitalWrite(LED_PIN, HIGH);
delay(5000);
digitalWrite(LED_PIN, LOW);
lcd.clear();
lcd.print(" Closed!");
delay(2000);
lcd.clear();
lcd.print(" Enter Password");
i = -1;
// lcd.clear();
// lcd.print(" *: Open");
// if(key != NO_KEY){
// if (key == '*'){
// digitalWrite(LED_PIN, HIGH);
// }
// }
// delay(5000);
// lcd.clear();
// lcd.print(" Enter Password");
// digitalWrite(LED_PIN, LOW);
i = 0;
count = 0;
} else {
lcd.clear();
lcd.print(" Incorrect!");
buzzerFailedPassword();
countFailedPassword++;
// Serial.println(countFailedPassword);
if (countFailedPassword >= 3) {
buzzerLongBeep();
}
delay(1000);
lcd.clear();
lcd.print(" Try Again!");
delay(2000);
lcd.clear();
lcd.print(" Enter Password");
i = 0;
count = 0;
}
}
// if (passwordTrue){
// lcd.clear();
// lcd.print(" *: Open");
// if (key == '*'){
// digitalWrite(LED_PIN, HIGH);
// }
// lcd.clear();
// lcd.print(" Enter Password");
// digitalWrite(LED_PIN, LOW);
// }
// void changePassword(){
// lcd.clear();
// lcd.setCursor(0,0);
// lcd.print("Enter new password:");
// while(1){
// char key = keypad.getKey();
// if(key != NO_KEY){
// }
// }
}
void buzzerBeep() {
tone(BUZZER_PIN, 1000, 100);
}
void buzzerTruePassword() {
tone(BUZZER_PIN, 1000, 100);
delay(200);
tone(BUZZER_PIN, 1000, 100);
}
// Hàm kêu buzzer khi nhập sai mật khẩu
void buzzerFailedPassword() {
for (int i = 0; i < 3; i++) {
tone(BUZZER_PIN, 1500, 200);
delay(300);
}
}
// Hàm kêu buzzer khi nhập đúng mật khẩu
void buzzerLongBeep() {
for (int i = 0; i < 10; i++) {
tone(BUZZER_PIN, 1500, 500);
delay(600);
}
}
*/
//--------------------------------Include-------------------------------------------//
#include <Keypad.h>
#include <LiquidCrystal_I2C.h> // Khai báo thư viện LCD sử dụng I2C
#include <Wire.h>
#include <EEPROM.h>
//----------------------------------------------------------------------------------//
LiquidCrystal_I2C lcd(0x27, 16, 2); // 0x27 địa chỉ LCD, 16 cột và 2 hàng
//----------------------------------------------------------------------------------//
#define BUZZER_PIN 12
#define LED_PIN 13
#define PASSWORD_LENGTH 4
#define EEPROM_ADDRESS 0
//----------------------------------------------------------------------------------//
//--------------------------------Variable------------------------------------------//
const byte rows = 4; //số hàng
const byte columns = 4; //số cột
int i= 0;
char key_pressed=0;
char password[4];
char initial_password[4],new_password[4];
byte rowPins[rows] = {2, 3, 4, 5}; //Cách nối chân với Arduino
byte columnPins[columns] = {15, 16, 17, 18};
char keys[rows][columns] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', ' '},
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);
//----------------------------------------------------------------------------------//
//----------------------------------------------------------------------------------//
void setup() {
Serial.begin(9600);//bật serial, baudrate 9600=
lcd.init(); // Khai báo sử dụng LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
lcd.print(" Enter Password");
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
lcd.setCursor(0,1);
initialpassword();
Serial.println(EEPROM.read(0));
Serial.println(EEPROM.read(1));
Serial.println(EEPROM.read(2));
Serial.println(EEPROM.read(3));
Serial.println(initial_password[0]);
Serial.println(initial_password[1]);
Serial.println(initial_password[2]);
Serial.println(initial_password[3]);
}
//----------------------------------------------------------------------------------//
void loop(){
key_pressed = keypad.getKey();
if(key_pressed=='#')
change();
if (key_pressed)
{
password[i]=key_pressed;
Serial.println(i);
Serial.println(key_pressed);
lcd.print(key_pressed);
i++;
}
if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);
if(!(strncmp(password, initial_password,4))){
lcd.clear();
lcd.print("Pass Accepted");
delay(2000);
lcd.setCursor(0,0);
lcd.print("Pres >START< to");
lcd.setCursor(0,1);
lcd.print("change the pass");
delay(3000);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0,1);
i=0;
}
else{
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,0);
lcd.print("Pres >#< to");
lcd.setCursor(0,1);
lcd.print("change the pass");
delay(2000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
i=0;
}}}
void change(){
int j=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0,1);
while(j<4){
char key=keypad.getKey();
if(key)
{
new_password[j++]=key;
lcd.print(key);
}
key=0;}
delay(500);
if((strncmp(new_password, initial_password, 4))){
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Try Again");
delay(1000);}
else{
j=0;
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0,1);
while(j<4){
char key=keypad.getKey();
if(key)
{
initial_password[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;}}
lcd.print("Pass Changed");
delay(1000);}
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
key_pressed=0;
}
void initialpassword(){
int j;
for(j=0;j<4;j++)
EEPROM.write(j,0);
for(j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);}