#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
// --- CẤU HÌNH CHÂN ---
#define SS_PIN 10
#define RST_PIN 9
#define PIR_PIN 2
#define SERVO_PIN 3
#define BUZZER_PIN 8
#define LIMIT_SW_PIN A0
// --- CẤU HÌNH KEYPAD 4x3 ---
const byte ROWS = 4;
const byte COLS = 3;
byte rowPins[ROWS] = {7, 6, 5, 4};
byte colPins[COLS] = {A3,A2,A1}; // Đảm bảo chân A0 cho Cột 1
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
MFRC522 rfid(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myServo;
String masterUID = "01 02 03 04";
String password = "123";
String inputPassword = "";
unsigned long previousMillis = 0;
const long interval = 10000;
bool hasPerson = false;
// --- NHẠC MARIO ---
#define NOTE_E7 2637
#define NOTE_C7 2093
#define NOTE_G7 3136
#define NOTE_G6 1568
int melody[] = {NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, NOTE_G6, 0};
int durations[] = {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LIMIT_SW_PIN, INPUT_PULLUP);
myServo.attach(SERVO_PIN);
myServo.write(0);
lcd.init();
lcd.backlight();
lcd.print("READY...");
delay(1000);
lcd.clear();
lcd.noBacklight();
}
void openDoor() {
lcd.backlight();
lcd.clear(); lcd.print("MO CUA...");
myServo.write(90);
for (int i = 0; i < 13; i++) {
int d = 1000 / durations[i];
tone(BUZZER_PIN, melody[i], d);
delay(d * 1.3);
noTone(BUZZER_PIN);
}
delay(2000);
lcd.clear(); lcd.print("DANG DONG CUA...");
myServo.write(0);
unsigned long start = millis();
while(digitalRead(LIMIT_SW_PIN) == HIGH && (millis() - start < 3000)) delay(10);
lcd.clear(); lcd.noBacklight();
inputPassword = "";
hasPerson = false;
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(PIR_PIN) == HIGH) {
previousMillis = currentMillis;
if (!hasPerson) {
lcd.backlight();
lcd.clear(); lcd.print("NHAP MA:");
hasPerson = true;
}
}
if (hasPerson) {
// 1. RFID
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
uid.concat(String(rfid.uid.uidByte[i], HEX));
}
uid.toUpperCase(); uid = uid.substring(1);
if (uid == masterUID) openDoor();
else { lcd.clear();
lcd.print("SAI THE!");
tone(BUZZER_PIN, 150, 500);
delay(1000); lcd.clear();
lcd.print("NHAP MA:"); }
rfid.PICC_HaltA();
}
// 2. KEYPAD - HIỂN THỊ KÝ TỰ THẬT
char key = keypad.getKey();
if (key) {
Serial.print("Bam phim: "); Serial.println(key);
if (key == '#') {
if (inputPassword == password) openDoor();
else { lcd.clear();
lcd.print("SAI MA!");
inputPassword = "";
delay(1000); lcd.clear();
lcd.print("NHAP MA:"); }
} else if (key == '*') {
inputPassword = "";
lcd.setCursor(0, 1); lcd.print(" "); // Xóa dòng 2
} else {
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword); // Hiển thị chuỗi mật khẩu đang nhập thay vì dấu *
}
}
if (currentMillis - previousMillis > interval) {
lcd.clear();
lcd.noBacklight();
hasPerson = false;
inputPassword = "";
}
}
}Loading
mfrc522
mfrc522