BASIC4MCU | 질문게시판 | 아두이노 도어락 질문드립니다 ㅜㅜ
페이지 정보
작성자 후미 작성일2022-11-16 01:41 조회770회 댓글0건본문
안녕하세요 4X4 키패드와 RFID를 활용하여 도어락을 만들고자 합니다!
추가적으로 비밀번호 변경하는 소스를 추가하고 싶은데.. 도저히 갈피를 못잡겠습니다 ㅜㅜ 도와 주세요!
--------------------소스---------------------
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define RST_PIN 9
#define SS_PIN 10
MFRC522 rfid(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
const char keymap[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const int rpin[] = {5, 4, 3, 2};
const int cpin[] = {A3, A2, A1, A0};
char newKey = 0;
char oldKey = 0;
int lockNunlock = 0;
char pass0 = '0';
char pass1 = '9';
char pass2 = '1';
char pass3 = '9';
byte rfidPass0 = 0x63;
byte rfidPass1 = 0xDD;
byte rfidPass2 = 0x51;
byte rfidPass3 = 0x16;
char passInBuff[4];
int passCnt = 0;
#define BUZZER_PIN 8
#define SERVO_PIN 7
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 4; i++)
{
pinMode(rpin[i], OUTPUT);
pinMode(cpin[i], INPUT_PULLUP);
}
myservo.attach(SERVO_PIN);
myservo.write(90);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
SPI.begin();
rfid.PCD_Init();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PASS:");
}
void loop()
{
newKey = getkey();
if (newKey != oldKey)
{
oldKey = newKey;
if (newKey != 0)
{
Serial.println(newKey);
passInBuff[passCnt++] = newKey;
lcd.print("*");
if (passCnt >= 4)
{
lcd.clear();
lcd.print("PASS:");
passCnt = 0;
if (passInBuff[0] == pass0 && passInBuff[1] == pass1 && passInBuff[2] == pass2 && passInBuff[3] == pass3)
{
Serial.println("ok");
lockNunlock = 1;
}
}
}
delay(50);
}
if (rfid.PICC_IsNewCardPresent())
{
if (rfid.PICC_ReadCardSerial())
{
Serial.print("value = ");
Serial.print(rfid.uid.uidByte[0], HEX);
Serial.print(rfid.uid.uidByte[1], HEX);
Serial.print(rfid.uid.uidByte[2], HEX);
Serial.print(rfid.uid.uidByte[3], HEX);
Serial.println();
if (rfid.uid.uidByte[0] == rfidPass0 && rfid.uid.uidByte[1] == rfidPass1 && rfid.uid.uidByte[2] == rfidPass2 && rfid.uid.uidByte[3] == rfidPass3)
{
Serial.println("rfid ok");
lockNunlock = 1;
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
}
if (lockNunlock == 1)
{
lockNunlock = 0;
myservo.write(180);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(5000);
}
else
{
myservo.write(90);
}
}
char getkey()
{
char key = 0;
for (int i = 0; i < 4; i++)
{
digitalWrite(rpin[i], LOW);
for (int j = 0; j < 4; j++)
{
if (digitalRead(cpin[j]) == LOW)
key = keymap[i][j];
}
digitalWrite(rpin[i], HIGH);
}
return key;
}
댓글 0
조회수 770등록된 댓글이 없습니다.