Trang chủ Liên hệ

Tự Lắp Khóa Mật Mã Với Arduino Nano Và Phím Ma Trận 4X4| Điện tử DAT

Nguyễn Chí 13/05/2022

Sơ Đồ Lắp :

Video Hướng Dẫn :

Code Arduino : 

#include <Keypad.h>
#include <EEPROM.h>
int address = 0;
const byte rows = 4; 
const byte columns = 4; 
int holdDelay = 700; 
int n = 3; 
int state = 0; 
char key = 0;
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};
byte columnPins[columns] = {6, 7, 8, 9};
  unsigned long number2;
 int number1;
 int coi=11;
 int kich=13;
 int biendem=0;
 int khoa=0;
 unsigned long pass1=123456; //thay đổi mật khẩu admin
 unsigned long pass2;
 int set=0;
int tuhoi=0;
int i=0;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows, columns);

void setup() {
  Serial.begin(9600);
 pinMode(coi, OUTPUT);
 pinMode(kich, OUTPUT);
 number2= EEPROM.read(address);
}
void loop() {  
  char temp = keypad.getKey();
 
  if ((int)keypad.getState() ==  PRESSED) {
    if (temp != 0) {
      key = temp;
    }
  }
  if ((int)keypad.getState() ==  HOLD) {
    state++;
    state = constrain(state, 1, n-1);
    delay(holdDelay);
  }
 
  if ((int)keypad.getState() ==  RELEASED) {
    key += state;
    state = 0;
    delay(100); 
 int number1 = key - '0';
 if (number1==20){number2=0;number1=0;}
 number2=(number2*10)+number1;
    Serial.println(number2);
    Serial.println(pass1);
    Serial.println(pass2);
 digitalWrite(coi,LOW);
tuhoi=60;
khoa=1;//cho tu hoi bằng 0 khi mỗi lần nhấn remote
  }
  delay(100);
  digitalWrite(coi, HIGH);
  
if(number2>=99999){
  if ((biendem>=5)&&(set==0)){
  number2=0;
  number1=0;
  khoa=1;
  biendem=0;
  digitalWrite(coi,LOW);
  delay(500);
  digitalWrite(coi,HIGH);}
  
if (set==1){
  pass2=number2;
  EEPROM.write(address,number2);
  digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
    digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
    digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
  biendem=0;
  number2=0;
  number1=0;
  khoa=1;
  set=0;
  tuhoi=60;}
  
  if(number2==pass1){
    set=1;digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
    digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
    digitalWrite(coi,LOW);
    delay(500);
    digitalWrite(coi,HIGH);
    delay(500);
    number2=0;
  
    };
   
    biendem++;
   if((number2==pass2)&&(set==0)) {
    khoa=1;digitalWrite(kich, HIGH);
    tuhoi=50; // thay đổi thời gia giữ khóa
    number2=0;
    i=1;
    }
   }

//if(khoa==1){tuhoi--;}
if((tuhoi<=1)&&(set==0)){digitalWrite(kich, LOW);i=0;number2=0;number1=0;khoa=0;set=0;tuhoi=60;
digitalWrite(coi,LOW);
  delay(500);
  digitalWrite(coi,HIGH);}
if((number2>=1)&&(khoa==1)){tuhoi=tuhoi-1;}
if(i==1){tuhoi=tuhoi-1;}

  Serial.println(tuhoi);
}

 

Bài viết liên quan