Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

#include <SPI.

h>
#include <MFRC522.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <Adafruit_Fingerprint.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySerial(A2, A3);

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key_rfid;
byte nuidPICC[4];
byte chuoi_the_dich[50];
byte the_can_tim[5];
byte the_nguon[5];

char chuoi[17];
int dia_chi_pass = 200;
int dia_chi_the=400;
// khao bao phim ma tran
const int rows = 4; //so hang
const int columns = 4; //so cot

byte holdDelay = 50; //time tao tre nhan nut, tranh nhieu nut nhan
byte n = 10; // thoi gian nhan giu nut
byte state = 0; // state =0 ko nhan nut,state =1 nhan thoi gian nhan , state = 2
nhan giu nut
byte key = 0; // gia tri nut khi chua dc nhan
byte temp=0; // bien de doc gia tri nut
//dinh nghia cac nut nhan tra ve
char keys[rows][columns] =
{
{"-x+%"},
{"=963"},
{"0852"},
{"A741"}
};
//Cach noi chan voi Arduino
byte rowPins[rows] = {0, 1, 2, 3};
byte columnPins[columns] = {4, 5, 6, 7};
//khai bao khoi tao keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);
// put your setup code here, to run once:
#define mo_khoa 0
#define them_nguoi_dung 1
#define xoa_ma_nv 2
#define cai_dat_he_thong 2

const int coi_bao = A1;


const int sv = A0;
byte che_do=0;
byte cai_dat=0;
byte lua_chon=0;
byte pass_ok;
byte check_pass;
byte so_ki_tu,ki_tu;
byte so_lan_con_lai;

char pass_temp[4];
char pass[4];
char pass_moi[4];
char ten_temp[17],ma_nv_temp[3],ma_nv;
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
uint8_t id;
byte them_thanh_cong,xoa_thanh_cong,da_quet_van_tay;
void setup() {

SPI.begin(); // Init SPI bus


rfid.PCD_Init(); // Init MFRC522

for (byte i = 0; i < 6; i++) {


key_rfid.keyByte[i] = 0xFF;
}

/*
finger.emptyDatabase();
for(so_ki_tu=0;so_ki_tu<500;so_ki_tu++)
{
EEPROM.write(so_ki_tu,0);
}
*/
/*
for (byte i = 0; i < 50; i++) {
EEPROM.write(dia_chi_the+i,0);
}

*/
pinMode(coi_bao, OUTPUT);
pinMode(sv, OUTPUT);
// pinMode(guong_mat, INPUT);
lcd.begin();
finger.begin(57600);
if (finger.verifyPassword())
{
lcd.setCursor(0, 0);
lcd.print(F(" HE THONG OK! "));
lcd.setCursor(0, 1);
lcd.print(F("SAN SANG H.DONG!"));
bao_dong_coi(2,300);

lcd.setCursor(0, 0);
lcd.print(F("QUET V.TAY DE MO"));
lcd.setCursor(0, 1);
lcd.print(F("PASS: "));
}

else
{
lcd.setCursor(0, 0);
lcd.print(F("KIEM TRA C.BIEN!"));
lcd.setCursor(0, 1);
lcd.print(F(" VA K.DONG LAI! "));
bao_dong_coi(1,2000);
while(1);
}

che_do = mo_khoa;

for(so_ki_tu=0;so_ki_tu<4;so_ki_tu++)
{
pass[so_ki_tu] = EEPROM.read(dia_chi_pass+so_ki_tu);
if(pass[so_ki_tu]<0||pass[so_ki_tu]>9)
{
pass[so_ki_tu] =0;
}
}
// EEPROM.write(dia_chi_pass+4,5);
so_lan_con_lai=EEPROM.read(dia_chi_pass+4);
if(so_lan_con_lai>5)
so_lan_con_lai=5;
else if(so_lan_con_lai==0)
so_lan_con_lai=1;
dk_sv(0,100);

void loop() {

if(che_do==mo_khoa)
{
ctr_mo_khoa();
da_quet_van_tay=0;
}
else if(che_do==them_nguoi_dung)
{
ctr_them_nguoi_dung();
}
else if(che_do==xoa_ma_nv)
{
ctr_xoa_ma_nv();
}

quet_phim();
if(key==37) // nhan giu nut menu
{
hien_thi_lua_chon();
}
if( (key>47 && key<58) || (key>77 && key<88) )
{
xac_nhan_pass();
if(pass_ok==1)
{
lcd.setCursor(0, 0);
lcd.print(F("PASS CHINH XAC! "));
lcd.setCursor(0, 1);
lcd.print(F(" XIN MOI VAO "));
bao_dong_coi(2,200);
dk_sv(1,3000);
dk_sv(0,1000);
lcd.setCursor(0, 0);
lcd.print(F("QUET V.TAY DE MO"));
lcd.setCursor(0, 1);
lcd.print(F("PASS: "));
}
}

void bao_dong_coi(int lan_bao,int thoi_gian_bao){


for(byte i=0;i<lan_bao;i++)
{
digitalWrite(coi_bao, HIGH);
delay(thoi_gian_bao);
digitalWrite(coi_bao, LOW);
delay(thoi_gian_bao);
}
}
void quet_phim(){
temp = keypad.getKey();
if ((int)keypad.getState() == PRESSED) {
if (temp != 0) {
key = temp;
}
}

if ((int)keypad.getState() == HOLD) {
if (state==0){
key = key+n;
state=n;
}
delay(holdDelay);
}

if ((int)keypad.getState() == RELEASED) {
state = 0;// chuan bi cho lan nhan de nut tiep theo
key=0;
}
delay(50);
}
void hien_thi_lua_chon(){
che_do=cai_dat_he_thong;
lcd.clear();
cai_dat=1; // set vao che do cai dat
lua_chon=2;// lua chon cai dat che do, 1 la lua chon cai dat mat khau
while(cai_dat==1){ // hien thi 1 so lua chon cho ng dung lua chon
quet_phim();
if(key==65) { // nhan nut on/ac
lua_chon--;
if(lua_chon==0)
lua_chon=2;
key=0;
}
else if(key==61) { // nhan nut =
lua_chon++;
if(lua_chon>2)
lua_chon=1;
key=0;
}
else if(key==120) { // nhan nut x => thoat
che_do=mo_khoa;
lcd.clear();
lua_chon=0;
cai_dat=0;
key=0;
}
else if(key==45) { // nhan nut ok
key=0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" NHAP MAT KHAU! ");
xac_nhan_pass();
if(lua_chon==2){
lcd.clear();
lua_chon=4;// di toi cai dat che do
cai_dat=2;// lua chon che do hoat dong
}
else if(lua_chon==1){
lcd.clear();
lua_chon=3;// di toi cai dat mk
cai_dat=3;// thay doi mat khau
}

if(lua_chon==2){ // hien thi cai dat che do


lcd.setCursor(0, 0);
lcd.print("=> C.DAT CHE DO ");
lcd.setCursor(0, 1);
lcd.print(" C.DAT M.KHAU ");
}
else if(lua_chon==1){ // hien thi cai dat mat khau
lcd.setCursor(0, 0);
lcd.print(" C.DAT CHE DO ");
lcd.setCursor(0, 1);
lcd.print("=> C.DAT M.KHAU ");
}

while(cai_dat==2){ // them hoac xoa ma nv


quet_phim();
if(key==65) { // nhan nut on/ac
lua_chon--;
if(lua_chon==3)
lua_chon=5;
key=0;
}
else if(key==61) { // nhan nut =
lua_chon++;
if(lua_chon>5)
lua_chon=4;
key=0;
}
else if(key==120) { // nhan nut x => thoat
che_do=mo_khoa;
lcd.clear();
lua_chon=0;
cai_dat=0;
key=0;
pass_ok=0;
}
else if(key==45) { // nhan nut ok
lcd.clear();
if(lua_chon==4){
che_do=them_nguoi_dung;
}
else if(lua_chon==5){
che_do=xoa_ma_nv;
}

lua_chon=0;
cai_dat=0;
key=0;
pass_ok=0;
}

if(lua_chon==4){ // bat che do TU DONG


lcd.setCursor(0, 0);
lcd.print("=>THEM MA ND ");
lcd.setCursor(0, 1);
lcd.print(" XOA MA ND ");
}
else if(lua_chon==5){ // bat che do DK TAY
lcd.setCursor(0, 0);
lcd.print(" THEM MA ND ");
lcd.setCursor(0, 1);
lcd.print("=>XOA MA ND ");
}

while(cai_dat==3){ // thay doi mk


lcd.setCursor(0, 0);
lcd.print(" NHAP PASS MOI! ");
so_ki_tu=0;
sprintf(pass_moi," ");// reset pass_temp
nhap_pass_moi();
}

}
}
void nhap_pass_moi(){

int xac_nhan=0;
int pass_moi_ok=0;
so_ki_tu=0;
while(so_ki_tu<4){
quet_phim();

if(key==120) { // nhan nut x => thoat


che_do=mo_khoa;
lcd.clear();
lua_chon=2;
cai_dat=1;
so_ki_tu=5;//thoat while
key=0;
pass_ok=0;
}
if( (key>47 && key<58) || (key>77 && key<88) ){ // du lieu nhap vao la so
so_ki_tu++;
lcd.setCursor(5+so_ki_tu, 1);
lcd.print("*");
if(xac_nhan==0){
pass_moi[so_ki_tu-1]=key-48;
if(key>57){
pass_moi[so_ki_tu-1]=key-n-48;
}
}
else if(xac_nhan==1){
pass_temp[so_ki_tu-1]=key-48;
if(key>57){
pass_temp[so_ki_tu-1]=key-n-48;
}
}
key=0;
if(so_ki_tu==4 && xac_nhan==0){
lcd.setCursor(0, 0);
lcd.print("XAC NHAN MK MOI");
lcd.setCursor(0, 1);
lcd.print(" ");
so_ki_tu=0;
xac_nhan=1;
}
if(so_ki_tu==4 && xac_nhan==1){
for(ki_tu=0;ki_tu<so_ki_tu;ki_tu++){
if(pass_temp[ki_tu]==pass_moi[ki_tu]){
pass_moi_ok=1;
goto tiep;
}
else { // mk sai
sprintf(chuoi,"CON %d LAN NHAP",so_lan_con_lai);
lcd.setCursor(0, 1);
lcd.print("MK 2 LAN K.GIONG");
pass_moi_ok=0;
so_ki_tu=5;// thoat while;
delay(1000);
lcd.clear();
break;
}
tiep:;
}
if (pass_moi_ok==1){ // thuc hien het vong lap for ma k phat hien
pass sai=> pass dung
lcd.setCursor(0, 1);
lcd.print("DA DOI MAT KHAU");
for(so_ki_tu=0;so_ki_tu<4;so_ki_tu++){
EEPROM.write(dia_chi_pass+so_ki_tu,pass_moi[so_ki_tu]);
pass[so_ki_tu]=pass_moi[so_ki_tu];
}
so_ki_tu=5;// thoat vong lap while
}

}
}
if( key==43 ){ // nut +=> clr
if(so_ki_tu>0){
lcd.setCursor(5+so_ki_tu, 1);
lcd.print(" ");
if(xac_nhan==1)
pass_temp[so_ki_tu]=10;
else
pass_moi[so_ki_tu]=10;
so_ki_tu--;
}
key=0;
}
}

}
void xac_nhan_pass(){

pass_ok=0;
check_pass=0;
so_ki_tu=0;
sprintf(pass_temp," ");// reset pass_temp
while(!check_pass){
if(che_do==mo_khoa)
{
ctr_mo_khoa();
if(da_quet_van_tay==1)
{
da_quet_van_tay=0;
return;
}
}
quet_phim();
if( (key>47 && key<58) || (key>77 && key<88) )
{ // du lieu nhap vao la so
so_ki_tu++;
lcd.setCursor(5+so_ki_tu, 1);
lcd.print("*");
pass_temp[so_ki_tu-1]=key-48;
if(key>57){
pass_temp[so_ki_tu-1]=key-n-48;
}
key=0;
if(so_ki_tu==4){

for(ki_tu=0;ki_tu<so_ki_tu;ki_tu++){
if(pass_temp[ki_tu]==pass[ki_tu]){
pass_ok=1;
goto next;
}
else { // mk sai
if(so_lan_con_lai>0){
so_lan_con_lai--;
sprintf(chuoi,"CON %d LAN NHAP",so_lan_con_lai);
lcd.setCursor(0, 1);
lcd.print(chuoi);
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" ");
}
EEPROM.write(dia_chi_pass+4,so_lan_con_lai);
if(so_lan_con_lai==0){ // nhap sai mk 5 lan
lcd.setCursor(0, 1);
lcd.print("XIN L.HE NHA SX!");
}
pass_ok=0;
so_ki_tu=0;
break;
}
next:;
}
if (pass_ok==1){ // thuc hien het vong lap for ma k phat hien pass
sai=> pass dung
so_lan_con_lai=5;
check_pass=1;
so_ki_tu=0;
EEPROM.write(dia_chi_pass+4,so_lan_con_lai);
}
}
}
if( key==43 ){ // nut +=> clr
if(so_ki_tu>0){
lcd.setCursor(5+so_ki_tu, 1);
lcd.print(" ");
pass_temp[so_ki_tu]=10;
so_ki_tu--;
}
key=0;
}

if(key==120|| key==37 ) { // nhan nut x => thoat


che_do=mo_khoa;
lcd.clear();
lua_chon=0;
check_pass=1;
cai_dat=0;
key=0;
}

void ctr_mo_khoa(){
byte len;
/*
EEPROM.write(dia_chi_the,0x65);
EEPROM.write(dia_chi_the+1,0x4A);
EEPROM.write(dia_chi_the+2,0x09);
EEPROM.write(dia_chi_the+3,0xAD);
EEPROM.write(dia_chi_the+4,3);

EEPROM.write(dia_chi_the+5,0x02);
EEPROM.write(dia_chi_the+6,0x3D);
EEPROM.write(dia_chi_the+7,0xD9);
EEPROM.write(dia_chi_the+8,0x1B);
EEPROM.write(dia_chi_the+9,2);
*/
if(rfid.PICC_IsNewCardPresent())
{
// Verify if the NUID has been readed
rfid.PICC_ReadCardSerial();
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
//lcd.print(nuidPICC[i] < 0x10 ? " 0" : " ");
//lcd.print(nuidPICC[i], HEX);
}
id=tim_ma_the();
if(id>0){
lcd.clear();
lcd.setCursor(0, 0);
sprintf(chuoi,"NGUOI DUNG:%02d ",id);
lcd.print(chuoi);

for(len=0;len<16;len++)
{
chuoi[len]=EEPROM.read((id-1)*20+len+1);
if(chuoi[len]=='\0')
break;
}
lcd.setCursor(0, 1);
lcd.print(chuoi);

bao_dong_coi(2,200);
dk_sv(1,3000);
dk_sv(0,1000);

}
else
{
lcd.setCursor(0, 1);
lcd.print(F("CHUA DUOC LUU! "));
bao_dong_coi(1,1000);
lcd.setCursor(0, 1);
lcd.print(F("PASS "));
da_quet_van_tay=1;
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();

if(check_van_tay()>0)
{
id=finger.fingerID;
ma_nv=EEPROM.read((id-1)*20);
if(ma_nv>0 && ma_nv!='\0')
{
lcd.clear();
lcd.setCursor(0, 0);
sprintf(chuoi,"NGUOI DUNG:%02d ",id);
lcd.print(chuoi);
for(len=0;len<16;len++)
{
chuoi[len]=EEPROM.read((id-1)*20+len+1);
if(chuoi[len]=='\0')
break;
}
lcd.setCursor(0, 1);
lcd.print(chuoi);

bao_dong_coi(2,200);
dk_sv(1,3000);
dk_sv(0,1000);

}
else
{
lcd.setCursor(0, 1);
lcd.print(F(" XIN THU LAI! "));
bao_dong_coi(1,1000);
lcd.setCursor(0, 1);
lcd.print(F("PASS "));
da_quet_van_tay=1;
}

}
}
int check_van_tay()
{
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK)
{
lcd.setCursor(0, 0);
lcd.print(F("QUET V.TAY DE MO"));
lcd.setCursor(0, 1);
lcd.print(F("PASS:"));

return -1;

p = finger.image2Tz();
if (p != FINGERPRINT_OK)
{
lcd.setCursor(0, 1);
lcd.print(F(" XIN THU LAI! "));
bao_dong_coi(1,1000);
lcd.setCursor(0, 1);
lcd.print(F("PASS: "));
da_quet_van_tay=1;
return -1;
}

p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK)
{
lcd.setCursor(0, 1);
lcd.print(F(" XIN THU LAI! "));
bao_dong_coi(1,1000);
lcd.setCursor(0, 1);
lcd.print(F("PASS: "));
da_quet_van_tay=1;
return -1;
}

// found a match!
da_quet_van_tay=1;
return finger.fingerID;

}
void ctr_them_nguoi_dung()
{
byte len;
byte i=1;
byte vi_tri=0;
char data,so_cong;
char p=-1;
byte da_ghi_data=0;;
// lua chon them bang van tay hay the tu
lcd.clear();

lua_chon=6;
while(lua_chon>0){
if(lua_chon==7)
{
lua_chon=7;
lcd.setCursor(0, 0);
lcd.print(F(" THEM MA V.TAY"));
lcd.setCursor(0, 1);
lcd.print(F("=> THEM MA THE "));
}
else if(lua_chon==6)
{
lua_chon=6;
lcd.setCursor(0, 0);
lcd.print(F("=> THEM MA V.TAY"));
lcd.setCursor(0, 1);
lcd.print(F(" THEM MA THE "));
}
quet_phim();
if(key==65) { // nhan nut on/ac
lua_chon--;
if(lua_chon==5)
lua_chon=7;
key=0;
}
else if(key==61) { // nhan nut =
lua_chon++;
if(lua_chon>7)
lua_chon=6;
key=0;
}

else if(key==120) { // nhan nut x => thoat


che_do=mo_khoa;
lcd.clear();
lua_chon=0;
cai_dat=0;
key=0;
}
else if(key==45) { // nhan nut ok
them_moi:;
key=0;
vi_tri=0;
so_cong=0;
da_ghi_data=0;
i=1;
p=-1;
ma_nv=0;
sprintf(ma_nv_temp,'\0');
sprintf(ten_temp,'\0');
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TEN NGUOI DUNG:");
while(i==1)
{
quet_phim();
if(key>=48 && key<=52)
so_cong=17;
else if(key>=53 && key<=55)
so_cong=18;
else if(key>=56 && key<=57)
so_cong=19;
if(key>=78 && key<=87 && key!=73)
so_cong=-1;
if(key==65)
so_cong=23;
else if(key==95)
so_cong=-6;
else if(key==61)
so_cong=-29;
else if(key==91)
so_cong=-59;
if( key==43)// nut +=> clr
{
if(vi_tri>0)
{
lcd.setCursor(vi_tri-1, 1);
lcd.print(" ");
ten_temp[vi_tri]=" ";
vi_tri--;
}
key=0;
so_cong=0;
}
if(key>0 && vi_tri<16)
{
data=key+so_cong;
ten_temp[vi_tri]=data;
lcd.setCursor(vi_tri, 1);
lcd.print(data);
da_ghi_data=1;
if(vi_tri==0 && key<20)
{
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
if(da_ghi_data==1 && key==0)
{
da_ghi_data=0;
if(vi_tri<16 && so_cong!=0)
{
vi_tri++;
}

}
if(key==120 || key==150 || key==37 || key==67) // nhan nut x => thoat
{
lcd.clear();
i=0;
key=0;
che_do = mo_khoa;
}
if(key==45 || key==75) // nhan nut ok
{
ten_temp[vi_tri]='\0';
lcd.setCursor(0, 0);
lcd.print(F("MA NGUOI DUNG: "));
lcd.setCursor(0, 1);
lcd.print(F(" N.D "));
i=2;
key=0;
vi_tri=8;
ma_nv=0;
sprintf(ma_nv_temp,'\0');
}
}

while(i==2)
{
quet_phim();
if( ( (key>=48 && key<=57) || (key>=78 && key<=87) ) && vi_tri<10)
{
if(key>57)
data=key-30;
else
data=key;
lcd.setCursor(vi_tri, 1);
lcd.print(data);
ma_nv_temp[vi_tri-8]=data;
vi_tri++;
if(vi_tri==9)
ma_nv=ma_nv_temp[0]-48;
else if (vi_tri==10)
ma_nv= 10*(ma_nv_temp[0]-48) + ma_nv_temp[1]-48;
key=0;
if(ma_nv>10)
{
ma_nv=10;
lcd.setCursor(0, 1);
lcd.print(F(" N.D10 "));
}
}

if(key==120 || key==150 || key==37 || key==67) // nhan nut x => thoat


{
lcd.clear();
i=0;
key=0;
che_do = mo_khoa;
}

if( key==43)// nut +=> clr


{
if(vi_tri>8)
{
lcd.setCursor(vi_tri-1, 1);
lcd.print(" ");
ma_nv_temp[vi_tri-8]=" ";
vi_tri--;

}
key=0;
}
if(key==45) // nhan nut ok
{
ma_nv=0;
if(vi_tri==9)
ma_nv=ma_nv_temp[0]-48;
else if (vi_tri==10)
ma_nv= 10*(ma_nv_temp[0]-48) + ma_nv_temp[1]-48;
ma_nv_temp[vi_tri]='\0';
if(ma_nv>0)
{

if(check_ma_nv())
{
lcd.setCursor(0, 0);
lcd.print(F("MA NV DA TON TAI"));
lcd.setCursor(0, 1);
lcd.print(F(" XIN NHAP LAI! "));
delay(1500);
ten_temp[vi_tri]='\0';
lcd.setCursor(0, 0);
lcd.print(F(" NHAP MA NV: "));
lcd.setCursor(0, 1);
lcd.print(F(" NV "));
i=2;
key=0;
vi_tri=8;
ma_nv=0;
sprintf(ma_nv_temp,'\0');
}
else
{
thuc_hien_lai:;
lcd.setCursor(0, 0);
lcd.print(F(" THEM MA ND "));
id=ma_nv;
them_thanh_cong=0;
if(lua_chon==6)
{
lcd.setCursor(0, 1);
lcd.print(F("MOI QUET VAN TAY"));
p=them_ma_van_tay();

}// ket thuc if(lua_chon==6)


if(lua_chon==7)
{
lcd.setCursor(0, 1);
lcd.print("MOI QUET THE MOI");
them_ma_the_tu();
}
if(them_thanh_cong==1)
{
key=0;
EEPROM.write((id-1)*20,id);
for(len=0;len<strlen(ten_temp);len++)
{
EEPROM.write((id-1)*20+len+1,ten_temp[len]);
}
EEPROM.write((id-1)*20+len+1,'\0');

sprintf(chuoi,"THEM NV%02d T.CONG",id);


lcd.setCursor(0, 1);
lcd.print(chuoi);
bao_dong_coi(2,300);
delay(1000);
key=0;
goto them_moi;
}
else
{
if(che_do==mo_khoa)
{
i=0;
key=0;
return;
}
sprintf(chuoi,"THEM NV%02d T.BAI!",id);
lcd.setCursor(0, 1);
lcd.print(chuoi);
bao_dong_coi(1,1000);
goto thuc_hien_lai;
}
}

}// ket thuc neu ma nv >0


}
}// ket thuc while(i==2)
}// ket thuc chuong trinh khi nhan nut ok trong cai dat che do them nguoi
dung
}

}
void ctr_xoa_ma_nv()
{
char vi_tri,data,len;
lcd.setCursor(0, 0);
lcd.print(" MA NV CAN XOA: ");
lcd.setCursor(0, 1);
lcd.print(" NV ");
key=0;
vi_tri=8;
ma_nv=0;
sprintf(ma_nv_temp,'\0');
while(1)
{
quet_phim();
if( ( (key>=48 && key<=57) || (key>=78 && key<=87) ) && vi_tri<10)
{
if(key>57)
data=key-30;
else
data=key;
lcd.setCursor(vi_tri, 1);
lcd.print(data);
ma_nv_temp[vi_tri-8]=data;
vi_tri++;
if(vi_tri==9)
ma_nv=ma_nv_temp[0]-48;
else if (vi_tri==10)
ma_nv= 10*(ma_nv_temp[0]-48) + ma_nv_temp[1]-48;
key=0;
if(ma_nv>50)
{
ma_nv=50;
lcd.setCursor(0, 1);
lcd.print(" NV50 ");
}
}
else if(key==120 || key==150) // nhan nut x => thoat
{
lcd.clear();
key=0;
che_do = mo_khoa;
break;
}

if( key==43 || key==73)// nut +=> clr


{
if(vi_tri>8)
{
lcd.setCursor(vi_tri-1, 1);
lcd.print(" ");

ma_nv_temp[vi_tri-8]=" ";
vi_tri--;

}
key=0;
}
else if(key==45 || key==75) // nhan nut ok
{
ma_nv=0;
if(vi_tri==9)
ma_nv=ma_nv_temp[0]-48;
else if (vi_tri==10)
ma_nv= 10*(ma_nv_temp[0]-48) + ma_nv_temp[1]-48;
ma_nv_temp[vi_tri]='\0';
if(ma_nv>0)
{
id=ma_nv;
xoa_thanh_cong=0;
xoa_ma_van_tay(id);
if(xoa_thanh_cong)
{
for(len=0;len<20;len++)
{
EEPROM.write((id-1)*20+len,'\0');
}
sprintf(chuoi,"XOA NV%02d T.CONG!",id);
lcd.setCursor(0, 1);
lcd.print(chuoi);
bao_dong_coi(2,300);
}
else
{
sprintf(chuoi,"XOA NV%02d T.BAI! ",id);
lcd.setCursor(0, 1);
lcd.print(chuoi);
bao_dong_coi(1,1000);
}
return;
}
}
}
}
char check_ma_nv()
{
char so_luong;
char ma;
for(so_luong=1;so_luong<=10;so_luong++)
{
ma = EEPROM.read((so_luong-1)*20);
if (ma==ma_nv)
return 1;
}
return 0;
}

uint8_t them_ma_van_tay() {

int p = -1;
while (p != FINGERPRINT_OK) {
quet_phim();
if(key==120 || key==150) // nhan nut x => thoat
{
lcd.clear();
key=0;
che_do = mo_khoa;
return;
}
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
lcd.setCursor(0, 1);
lcd.print(" LAY MAU 1 OK! ");
break;
case FINGERPRINT_NOFINGER:
break;
case FINGERPRINT_PACKETRECIEVEERR:
lcd.setCursor(0, 1);
lcd.print("KIEM TRA C.BIEN!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
break;
case FINGERPRINT_IMAGEFAIL:
lcd.setCursor(0, 1);
lcd.print("LAY MAU 1 T.BAI!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
break;
}
}
// OK success!

p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_IMAGEMESS:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
lcd.setCursor(0, 1);
lcd.print("KIEM TRA C.BIEN!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
return p;
case FINGERPRINT_FEATUREFAIL:
return p;
case FINGERPRINT_INVALIDIMAGE:
return p;
default:
return p;
}

lcd.setCursor(0, 1);
lcd.print("XIN NHAC TAY RA!");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
p = 0;
while (p != FINGERPRINT_NOFINGER) {
quet_phim();
if(key==120 || key==150) // nhan nut x => thoat
{
che_do = mo_khoa;
return;
}
p = finger.getImage();
}
p = -1;
while (p != FINGERPRINT_OK) {
quet_phim();
if(key==120 || key==150) // nhan nut x => thoat
{
che_do = mo_khoa;
return;
}
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
lcd.setCursor(0, 1);
lcd.print(" LAY MAU 2 OK! ");
break;
case FINGERPRINT_NOFINGER:
break;
case FINGERPRINT_PACKETRECIEVEERR:
lcd.setCursor(0, 1);
lcd.print("KIEM TRA C.BIEN!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
break;
case FINGERPRINT_IMAGEFAIL:
lcd.setCursor(0, 1);
lcd.print("LAY MAU 1 T.BAI!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
break;
}
}

// OK success!

p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_IMAGEMESS:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
lcd.setCursor(0, 1);
lcd.print("KIEM TRA C.BIEN!");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("MOI QUET VAN TAY");
return p;
case FINGERPRINT_FEATUREFAIL:
return p;
case FINGERPRINT_INVALIDIMAGE:
return p;
default:
return p;
}

// OK converted!
p = finger.createModel();
if (p == FINGERPRINT_OK) {
lcd.setCursor(0, 1);
lcd.print("DANG LUU VAN TAY");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
lcd.setCursor(0, 1);
lcd.print(F("KIEM TRA C.BIEN!"));
delay(1000);
lcd.setCursor(0, 1);
lcd.print(F("MOI QUET VAN TAY"));
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
lcd.setCursor(0, 1);
lcd.print(F("2 LAN KHAC NHAU!"));
delay(1000);
lcd.setCursor(0, 1);
lcd.print(F("MOI QUET VAN TAY"));
return p;
} else {
return p;
}

p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
them_thanh_cong=1;
delay(1000);
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
lcd.setCursor(0, 1);
lcd.print(F("KIEM TRA C.BIEN!"));
delay(1000);
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
lcd.setCursor(0, 1);
lcd.print(F("LUU V.TAY T.BAI!"));
delay(1000);
return p;
} else if (p == FINGERPRINT_FLASHERR) {
return p;
} else {
return p;
}
}

uint8_t xoa_ma_van_tay(uint8_t ma_id) {


uint8_t p = -1;
p = finger.deleteModel(ma_id);

if (p == FINGERPRINT_OK) {
xoa_thanh_cong=1;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
lcd.setCursor(0, 1);
lcd.print(F("KIEM TRA C.BIEN!"));
delay(1000);
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
return p;
} else if (p == FINGERPRINT_FLASHERR) {
return p;
} else {
return p;
}
}

void them_ma_the_tu(){
while(1)
{
if(rfid.PICC_IsNewCardPresent())
{
lcd.clear();
// Verify if the NUID has been readed
rfid.PICC_ReadCardSerial();
lcd.setCursor(0, 0);
lcd.print(F("MA THE HIEN TAI:"));
lcd.setCursor(0, 1);
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
lcd.print(nuidPICC[i] < 0x10 ? " 0" : " ");
lcd.print(nuidPICC[i], HEX);

// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
break;
}
}
delay(3000);
them_thanh_cong=1;
}
void xoa_ma_the_tu(){
}
void dk_sv(int on_off,int time_tre)
{
int lap;
if(on_off==0)
{
for(lap=0;lap<10;lap++)
{
digitalWrite(sv,1);
delay(1);
delayMicroseconds(700);
digitalWrite(sv,0);
delay(18);
delayMicroseconds(300);
}
}
else
{
for(lap=0;lap<10;lap++)
{
digitalWrite(sv,1);
delayMicroseconds(700);
digitalWrite(sv,0);
delay(19);
delayMicroseconds(300);
}
}
delay(time_tre);

uint8_t tim_ma_the()
{
byte i,k,m=0;

int vi_tri_tim;
int vi_tri_thay;

for (i = 0; i < 50; i++) {


chuoi_the_dich[i]=EEPROM.read(dia_chi_the+i);
}
chuoi_the_dich[i]='\0';
for(i=0;i<4;i++)
{
the_can_tim[i]=nuidPICC[i];
}
the_can_tim[i]='\0';

for(k=0;k<10;k++) // vong lap de tim toi da 10 the tu


{
vi_tri_tim=5*k;
for(m=vi_tri_tim;m<vi_tri_tim+4;m++)
{
the_nguon[m-vi_tri_tim]=chuoi_the_dich[m];
}
the_nguon[m]='\0';
vi_tri_thay=strstr(the_nguon,the_can_tim)-strstr(the_nguon,the_nguon);
if(vi_tri_thay>=0)
{
return chuoi_the_dich[m];
}
}
return 0;
}

You might also like