【问题标题】:Arduino : RFID RC522 Stop reading card multiple timesArduino:RFID RC522 多次停止读卡
【发布时间】:2014-05-13 13:37:17
【问题描述】:

我刚刚拿到了用于 arduino 的 RC522 RFID 卡,并且正在使用提供的草图。 MiFare RFID-RC522

我坚持的是理解如何在读取卡片时暂停草图,以便只读取一次。在卡连接到 RDIF 读卡器的那一刻,草图将保持循环并每次读取卡。

可以设置一个延迟,但如果连接时间超过延迟,最终会再次读取卡。

我想说的是,当有卡连接时,只读取一次卡 ID,然后在卡连接断开时继续草图。

这是主要的草图部分:

    void loop()
{

    uchar status;
    uchar str[MAX_LEN];


    // Search card, return card types
    status = MFRC522_Request(PICC_REQIDL, str);
    if (status != MI_OK)
    {
        return;
    }


    // Show card type
    ShowCardType(str);

    //Prevent conflict, return the 4 bytes Serial number of the card
    status = MFRC522_Anticoll(str);

    // str[0..3]: serial number of the card
    // str[4]: XOR checksum of the SN.
    if (status == MI_OK)
    {
        Serial.print("The card's number is: ");
        memcpy(serNum, str, 5);
        ShowCardID(serNum);

        // Check people associated with card ID
        uchar* id = serNum;
        if (id[0] == 0x4B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Mary!");
        }
        else if (id[0] == 0x3B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Greg!");
        }
        else{
            Serial.println("Hello unkown guy!");
        }
    }


    MFRC522_Halt(); //command the card into sleep mode 

    delay(2000);
}

谢谢各位。

【问题讨论】:

    标签: arduino rfid mifare


    【解决方案1】:

    通过查看MFRC522_Request(PICC_REQIDL, str) 的返回值,我能够解决这个问题。

    我将 RFID_status 创建为 int,还将 RFID_read 创建为 int 并设置为 0。

    然后我使用 RFID_status 来测试是否有活动的卡连接。 如果是且 RFID_read 仍为 0,则读取卡并将 RFID_read 设置为 1。这样在下一个循环中将不会读取卡。

    一旦卡片被拿走,RFID_status 将被读取为 0(无连接)并且 RFID_read 将被设置回 0。

        int RFID_status;
        int RFID_read = 0;    
    
        void loop()
    {
    
        uchar status;
        uchar str[MAX_LEN];
    
    
        // Search card, return card types
        RFID_status = MFRC522_Request(PICC_REQIDL, str);
        if (RFID_status != 1)
        {
    
             RFID_read 0;
    
        }
    
    
        // Show card type
        ShowCardType(str);
    
        //Prevent conflict, return the 4 bytes Serial number of the card
        status = MFRC522_Anticoll(str);
    
        // str[0..3]: serial number of the card
        // str[4]: XOR checksum of the SN.
        if (RFID_status == 1 && RFID_read == 0)
        {
            Serial.print("The card's number is: ");
            memcpy(serNum, str, 5);
            ShowCardID(serNum);
            RFID_read = 1;
    
            // Check people associated with card ID
            uchar* id = serNum;
            if (id[0] == 0x4B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
                Serial.println("Hello Mary!");
                RFID_read = 1;
            }
            else if (id[0] == 0x3B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
                Serial.println("Hello Greg!");
                RFID_read = 1;
            }
            else{
                Serial.println("Hello unkown guy!");
                RFID_read = 1;
            }
        }
    
    
        MFRC522_Halt(); //command the card into sleep mode 
    
        delay(2000);
    }
    

    注意:这个草图中有很多我已经取出的代码。我只包含了 void 循环和我创建的两个全局变量。 原始草图可以在这里找到:MiFare RFID-RC522 Sketch

    【讨论】:

    • 我一直在尝试做同样的事情,但我在卡仍然存在时第二次调用循环的步骤遇到了问题。第一个循环有效,但即使卡仍然存在,MFRC522_Request 在第二个循环上立即返回 0 ......然后当然会重新运行所有代码。有什么想法吗?
    【解决方案2】:

    我是这样做的:

    // RFID Includes
    #include <SPI.h>
    #include <MFRC522.h>
    
    // RFID Defines
    #define RST_PIN    9
    #define SS_PIN    10
    
    const int NO_CARD_DETECTIONS_BEFORE_NEW_READ = 40;
    
    MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
    
    int noCardCount = 0;
    
    void setup() {
      // This is for Trinket 5V 16MHz
      #if defined (__AVR_ATtiny85__)
        if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
      #endif
      // End of trinket special code
    
      Serial.begin(9600); // Initialize serial communications with the PC
      while (!Serial); // Do nothing if no serial port is opened (needed for Arduinos based on ATMEGA32U4)
      SPI.begin(); // Init SPI bus
      mfrc522.PCD_Init(); // Init MFRC522
      Serial.println("Waiting for RFID-chip...");
    }
    
    void loop() {
      Serial.println(noCardCount);
      if (mfrc522.PICC_IsNewCardPresent()) {
        if(noCardCount > NO_CARD_DETECTIONS_BEFORE_NEW_READ){
          Serial.println("Card present!");
          //mfrc522.PICC_ReadCardSerial();
          //mfrc522.PICC_HaltA();
        }
        noCardCount = 0;
      }else{ // not present
        noCardCount++;
      }
    }
    

    基本上我只是在计算连续没有卡片的频率,当卡片放在读卡器上时,noCardCount 将保持低电平。只有在连续很多次没有检测到卡时,才会再次读取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 2012-12-02
      相关资源
      最近更新 更多