[kenrobot_code]/*
RC522门禁系统V2.0
by ac-dc
接线:
RC522: ARDUINO
SDA<------->D10
SCK<------->D13
MOSI<------>D11
MISO<------>D12
RST<------->D9
IRQ<------->不接
3V3<------->3.3V(千万不能接5V)
GND<------->GND
LCD: ARDUINO
SCL<------->SCL
SDA<------->SDA LED&BUZZER:
RED<------->D3
GREEN<-->D2
BUZZER<>D4
*/
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <RFID.h>
RFID rfid(10, 9); //实例化一个读卡器,10是读卡器SS引脚、9是读卡器RST引脚
LiquidCrystal_I2C lcd(0x3F, 16, 2);//实例化一个LCD1602(不要问我为什么不是2004),地址是0x3F,一排可以显示16个字符,可以显示两排
#define Host 2//要修改人员数量就修改这里
boolean u = 0;//程序需要(下面会讲)
int host[Host][5] = { //人员数据
{0x24, 0xA7, 0x49, 0xF2, 0x38},
{0xBA, 0xE9, 0x94, 0x59, 0x9E},
};
void setup() {
// put your setup code here, to run once:
pinMode(2, OUTPUT); //绿灯(如果读出的卡是对的,那么就亮绿灯)
pinMode(3, OUTPUT);//红灯(如果读出的卡是错的,那么就亮红灯)
pinMode(4, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.init();
lcd.init();
lcd.backlight();
lcd.print(" Welcome to use ");
delay(700);
lcd.clear();
lcd.print("Put card here");
}
void loop() {
if (rfid.isCard()) {//读到了卡
Serial.println("Find the card!");
//读取卡序列号
if (rfid.readCardSerial()) {
Serial.print("The card's number is : ");
Serial.print(rfid.serNum[0], HEX);
Serial.print(" ");
Serial.print(rfid.serNum[1], HEX);
Serial.print(" ");
Serial.print(rfid.serNum[2], HEX);
Serial.print(" ");
Serial.print(rfid.serNum[3], HEX);
Serial.print(" ");
Serial.print(rfid.serNum[4], HEX);
Serial.println(" ");
for (int i = 0; i < Host; i++) {
if (rfid.serNum[0] == host[0] && rfid.serNum[1] == host[1] && rfid.serNum[2] == host[2] && rfid.serNum[3] == host[3] && rfid.serNum[4] == host[4] ) {
digitalWrite(2, HIGH);//亮绿灯,放行
u = 1;//这里为了标记读到了列表中的卡
lcd.clear();
lcd.home();
lcd.print(" The Host");
lcd.print(i);
lcd.setCursor(0, 1);
lcd.print("Welcome go back!");
delay(10);
tone(4, 300);
delay(200);
tone(4, 1200);
delay(200);
noTone(4);
digitalWrite(3, LOW);
delay(590);
lcd.clear();
lcd.home();
lcd.print("Put card here");
digitalWrite(2, LOW);//绿灯灭
}
}
if (!u) { //如果读到的卡不在列表中,那么执行下面程序
digitalWrite(3, HIGH);//红灯亮,不放行(哈哈哈)
tone(4, 200);//播放错误声(有点像答错了的声音)
lcd.clear();//清屏,为了报错
lcd.home();
lcd.print("Unkonw card");
delay(700);
digitalWrite(3, LOW);
noTone(4);
delay(300);
lcd.clear();
lcd.home();
lcd.print("Put card here");
}
rfid.selectTag(rfid.serNum);//选卡,可返回卡容量(锁定卡片,防止多数读取),去掉本行将连续读卡
}
rfid.halt();
u = 0; //为了下一次读卡准备
}
} |