#include <Password.h>
#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 21);
const char ROWS = 4;
const char COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPin[ROWS] = {9, 8, 7, 6};
byte colPin[COLS] = {5, 4, 3};
Keypad keypad = Keypad(makeKeymap(keys), rowPin, colPin, ROWS, COLS);
char initial_password[4],new_password[4];
char password[4];
int l=0;
char key_pressed=0;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.backlight(); // เปิดไฟ backlight
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
initialpassword();
}
void loop()
{
key_pressed = keypad.getKey();
if(key_pressed=='#')
change();
if (key_pressed)
{
password[l++]=key_pressed;
lcd.print(key_pressed);
}
if(l==4)
{
delay(200);
for(int p=0;p<4;p++)
initial_password[p]=EEPROM.read(p);
if(!(strncmp(password, initial_password,4)))
{
lcd.clear();
lcd.print("Pass Accepted");
delay(2000);
lcd.setCursor(0,1);
lcd.print("Pres # to change");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0,1);
l=0;
}
else
{
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Pres # to Change");
delay(2000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
l=0;
}
}
}
void change()
{
int p=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0,1);
while(p<4)
{
char key=keypad.getKey();
if(key)
{
new_password[p++]=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
{
p=0;
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0,1);
while(p<4)
{
char key=keypad.getKey();
if(key)
{
initial_password[p]=key;
lcd.print(key);
EEPROM.write(p,key);
p++;
}
}
lcd.print("Pass Changed");
delay(1000);
}
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
key_pressed=0;
}
void initialpassword(){
for(int p=0;p<4;p++)
EEPROM.write(p, p+49);
for(int p=0;p<4;p++)
initial_password[p]=EEPROM.read(p);
}
พอหยุดจ่ายไฟ EEPROM ไม่จำค่า
#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 21);
const char ROWS = 4;
const char COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPin[ROWS] = {9, 8, 7, 6};
byte colPin[COLS] = {5, 4, 3};
Keypad keypad = Keypad(makeKeymap(keys), rowPin, colPin, ROWS, COLS);
char initial_password[4],new_password[4];
char password[4];
int l=0;
char key_pressed=0;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.backlight(); // เปิดไฟ backlight
lcd.setCursor(0, 0);
lcd.print("Enter Password");
lcd.setCursor(0, 1);
initialpassword();
}
void loop()
{
key_pressed = keypad.getKey();
if(key_pressed=='#')
change();
if (key_pressed)
{
password[l++]=key_pressed;
lcd.print(key_pressed);
}
if(l==4)
{
delay(200);
for(int p=0;p<4;p++)
initial_password[p]=EEPROM.read(p);
if(!(strncmp(password, initial_password,4)))
{
lcd.clear();
lcd.print("Pass Accepted");
delay(2000);
lcd.setCursor(0,1);
lcd.print("Pres # to change");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0,1);
l=0;
}
else
{
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Pres # to Change");
delay(2000);
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
l=0;
}
}
}
void change()
{
int p=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0,1);
while(p<4)
{
char key=keypad.getKey();
if(key)
{
new_password[p++]=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
{
p=0;
lcd.clear();
lcd.print("New Password:");
lcd.setCursor(0,1);
while(p<4)
{
char key=keypad.getKey();
if(key)
{
initial_password[p]=key;
lcd.print(key);
EEPROM.write(p,key);
p++;
}
}
lcd.print("Pass Changed");
delay(1000);
}
lcd.clear();
lcd.print("Enter Password");
lcd.setCursor(0,1);
key_pressed=0;
}
void initialpassword(){
for(int p=0;p<4;p++)
EEPROM.write(p, p+49);
for(int p=0;p<4;p++)
initial_password[p]=EEPROM.read(p);
}