使用RFID卡授予访问权限

时间:2015-11-12 22:57:32

标签: c arduino

我的arduino程序出了问题。我使用RFID阅读器扫描卡片(这部分完美无瑕)。我希望程序授予使用某些按钮的权限(说实话并不重要)。使用经过验证的卡的用户可以使用这些功能,直到他再次扫描卡。我删除了一些不必要的代码部分。

#include <SPI.h>
#include <MFRC522.h>

String read_rfid;
String ok_rfid_1="a3f90f7"; //ID of the verified CARD
const int buzzer=8; //Buzzer
const int redLed=7; 
const int greenLed=6; 
const int yellowLed=5;
byte access=false;       // 

void setup() {
Serial.begin(9600);         // Initialize serial communications with the PC
SPI.begin();                // Init SPI bus
mfrc522.PCD_Init();         // Init MFRC522 card
}

/*
* Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
    read_rfid=read_rfid + String(buffer[i], HEX);
}
}

void granted() {    }  //it lights up the green led

void denied() {    } //it lights up the red led

void login() {
if (read_rfid==ok_rfid_1) {
  granted();
  access=true;
  } else { 
  denied();
  }
 delay(1000);
}

void logout() {
if (read_rfid==ok_rfid_1 && access==1) {
  access=false;
  Serial.println("Logout ");
  }
}

void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
    return;

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
login();
   while(access==true)
     {logout();};

}

登录部分运行良好,但会自动注销。正如我观察到的问题可能是if(read_rfid==ok_rfid_1 && access==1)始终为真,因为read_rfid不会改变。你有什么想法来解决我的困境吗?

0 个答案:

没有答案
相关问题