RC522读卡历程基于Arduino
- /*
- * --------------------------------------------------------------------------------------------------------------------
- * Example to change UID of changeable MIFARE card.
- * --------------------------------------------------------------------------------------------------------------------
- * This is a MFRC522 library example; for further details and other examples see: [url]https://github.com/miguelbalboa/rfid[/url]
- *
- * This sample shows how to set the UID on a UID changeable MIFARE card.
- * NOTE: for more informations read the README.rst
- *
- * @author Tom Clement
- * @license Released into the public domain.
- *
- * Typical pin layout used:
- * -----------------------------------------------------------------------------------------
- * MFRC522 Arduino Arduino Arduino Arduino Arduino
- * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
- * Signal Pin Pin Pin Pin Pin Pin
- * -----------------------------------------------------------------------------------------
- * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
- * SPI SS SDA(SS) 10 53 D10 10 10
- * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
- * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
- * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
- */
- /*
- Name: AccessSystem.ino
- Created: 2016/7/10 12:22:07
- Author: Fing
- */
- #include <SoftwareSerial.h>
- #include <Servo.h>
- #include <MFRC522.h>
- #include <SPI.h>
- /*自定义RC522的两个PIN口*/
- #define SS_PIN 10
- #define RST_PIN 9
- #define BT_RX 2 //蓝牙模块端口
- #define BT_TX 1
- #define SERVO_PIN 6 //舵机端口
- #define BUZZ_PIN 4
- #define CARDS 2 //已验证的卡片数目
- MFRC522 mfrc522(SS_PIN, RST_PIN); //新建RC522对象
- SoftwareSerial BT(BT_TX, BT_RX); //创建蓝牙软串口对象,避免冲突
- Servo myservo; //创建舵机对象
- bool isAuthed = false; //验证是否通过
- const byte AuthedID[1][4] = {0x66,0xA8,0x14,0xF9}; //可以保存多个卡片UID值
- char val; //用来存储蓝牙接收数据
- void setup() {
- Serial.begin(9600);//串口,波特率9600
- BT.begin(9600); //蓝牙串口,波特率9600
- myservo.attach(SERVO_PIN); //连接舵机
- SPI.begin(); //初始化SPI总线
- mfrc522.PCD_Init(); //初始化MFRC522卡
- pinMode(BUZZ_PIN, OUTPUT); //初始化蜂鸣器
- digitalWrite(BUZZ_PIN, HIGH);
- myservo.write(45);
- Serial.println(F("This code scan the MIFARE Classsic NUID."));
- Serial.print(F("Using the following key:"));
- }
- // the loop function runs over and over again until power down or reset
- void loop()
- {
- Authenticate();
- if (isAuthed) {
- OpenDoor();
- BeepChecked();
- delay(3000);
- isAuthed = false;
- }
- CloseDoor();
- isAuthed = false;
- BTCheck();
- delay(500);
- }
- //Servo开关门
- void OpenDoor()
- {
- myservo.write(140);
- }
- void CloseDoor()
- {
- myservo.write(45);
- }
- //验证卡片
- void Authenticate()
- {
- //检测是否有新卡片
- if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
- delay(50);
- return;
- }
- //进行卡片验证
- for (byte num = 0; num < CARDS; num++) //卡片循环
- {
- byte i;
- for (i = 0; i < mfrc522.uid.size; i++)
- {
- if (mfrc522.uid.uidByte[i] != AuthedID[num][i]) break;
- }
- Serial.println(F("The NUID tag is:"));
- Serial.print(F("In hex: "));
- printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
- Serial.println();
- Serial.print(F("In dec: "));
- printDec(mfrc522.uid.uidByte, mfrc522.uid.size);
- Serial.println();
- if (i == mfrc522.uid.size)
- {
- isAuthed = true; //验证通过
- break;
- }
- else if (num == CARDS - 1) {
- BeepFailed();
- }
- }
- }
- //蓝牙扫描
- void BTCheck() {
- if (BT.available()) {
- val = BT.read();
- if (val == '1') {
- OpenDoor();
- BeepChecked();
- delay(3000);
- isAuthed = false;
- }
- val = 0;
- }
- }
- void BeepChecked() {
- digitalWrite(BUZZ_PIN, LOW);
- delay(200);
- digitalWrite(BUZZ_PIN, HIGH);
- }
- void BeepFailed() {
- digitalWrite(BUZZ_PIN, LOW);
- delay(100);
- digitalWrite(BUZZ_PIN, HIGH);
- delay(100);
- digitalWrite(BUZZ_PIN, LOW);
- delay(100);
- digitalWrite(BUZZ_PIN, HIGH);
- }
- void printHex(byte *buffer, byte bufferSize) {
- for (byte a = 0; a < bufferSize; a++) {
- Serial.print(buffer[a] < 0x10 ? " 0" : " ");
- Serial.print(buffer[a], HEX);
- }
- }
- /**
- * Helper routine to dump a byte array as dec values to Serial.
- */
- void printDec(byte *buffer, byte bufferSize) {
- for (byte a = 0; a < bufferSize; a++) {
- Serial.print(buffer[a] < 0x10 ? " 0" : " ");
- Serial.print(buffer[a], DEC);
- }
- }
复制代码
|