C++ Arduino Void loop()不循环

C++ Arduino Void loop()不循环,c++,arduino-uno,C++,Arduino Uno,我对Arduino不熟悉。我在看 我把脚本贴在下面。基本上,它检测到一张卡,然后将一些数据写入块2中 在编译并上传代码后,我把一张卡放到RFID阅读器上,它可以工作——但只有一次。当我拿出第二张卡片时,什么也没发生。为什么? 我将此脚本与mfrc522库中的其他示例脚本进行了比较,它们非常相似-在void loop()部分,它检查NewCardPresent。。。读卡序列号。。。然后继续运行预期的操作。我已经尝试过这些示例脚本,我可以继续向读者展示一张新卡,脚本将再次运行 然而,在这个草图中,循

我对Arduino不熟悉。我在看

我把脚本贴在下面。基本上,它检测到一张卡,然后将一些数据写入块2中

在编译并上传代码后,我把一张卡放到RFID阅读器上,它可以工作——但只有一次。当我拿出第二张卡片时,什么也没发生。为什么?

我将此脚本与mfrc522库中的其他示例脚本进行了比较,它们非常相似-在void loop()部分,它检查NewCardPresent。。。读卡序列号。。。然后继续运行预期的操作。我已经尝试过这些示例脚本,我可以继续向读者展示一张新卡,脚本将再次运行

然而,在这个草图中,循环只运行了一次。是结构吗?如何编辑它以使其连续运行?如果答案很简单,我很抱歉:/

#include <SPI.h>//include the SPI bus library
#include <MFRC522.h>//include the RFID reader library

#define SS_PIN 10  //slave select pin
#define RST_PIN 5  //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN);        // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information


void setup() {
        Serial.begin(9600);        // Initialize serial communications with the PC
        SPI.begin();               // Init SPI bus
        mfrc522.PCD_Init();        // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
        Serial.println("Scan a MIFARE Classic card");

        // Prepare the security key for the read and write functions - all six key bytes are set to 0xFF at chip delivery from the factory.
        // Since the cards in the kit are new and the keys were never defined, they are 0xFF
        // if we had a card that was programmed by someone else, we would need to know the key to be able to access it. This key would then need to be stored in 'key' instead.

        for (byte i = 0; i < 6; i++) {
                key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
        }

}

int block=2;//this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.

byte blockcontent[16] = {"makecourse_____"};//an array with 16 bytes to be written into one of the 64 card blocks is defined
//byte blockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
byte readbackblock[18];//This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.

void loop()
{

        /*****************************************establishing contact with a tag/card**********************************************************************/

    // Look for new cards (in case you wonder what PICC means: proximity integrated circuit card)
    if ( ! mfrc522.PICC_IsNewCardPresent()) {//if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
        return;//if it did not find a new card is returns a '0' and we return to the start of the loop
    }

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial()) {//if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
        return;//if it returns a '0' something went wrong and we return to the start of the loop
    }

        // Among other things, the PICC_ReadCardSerial() method reads the UID and the SAK (Select acknowledge) into the mfrc522.uid struct, which is also instantiated
        // during this process.
        // The UID is needed during the authentication process
            //The Uid struct:
            //typedef struct {
        //byte      size;           // Number of bytes in the UID. 4, 7 or 10.
        //byte      uidByte[10];            //the user ID in 10 bytes.
        //byte      sak;            // The SAK (Select acknowledge) byte returned from the PICC after successful selection.
            //} Uid;

         Serial.println("card selected");

         /*****************************************writing and reading a block on the card**********************************************************************/

         writeBlock(block, blockcontent);//the blockcontent array is written into the card block
         //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

         //The 'PICC_DumpToSerial' method 'dumps' the entire MIFARE data block into the serial monitor. Very useful while programming a sketch with the RFID reader...
         //Notes:
         //(1) MIFARE cards conceal key A in all trailer blocks, and shows 0x00 instead of 0xFF. This is a secutiry feature. Key B appears to be public by default.
         //(2) The card needs to be on the reader for the entire duration of the dump. If it is removed prematurely, the dump interrupts and an error message will appear.
         //(3) The dump takes longer than the time alloted for interaction per pairing between reader and card, i.e. the readBlock function below will produce a timeout if
         //    the dump is used.

     //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));//uncomment this if you want to see the entire 1k memory with the block written into it.

         readBlock(block, readbackblock);//read the block back
         Serial.print("read block: ");
         for (int j=0 ; j<16 ; j++)//print the block contents
         {
           Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
         }
         Serial.println("");


}
Arduino Loop()应该被无限地调用,而不像您所观察到的那样。 因此,您需要检查以下两种可能性。 1) 任何返回都发生在循环()的开头。我看到了两个返回声明。您最好在它们之间插入调试消息,以便在返回此函数之前知道达到了什么程度。 2) 循环()中正在发生任何阻塞。我不知道,但你最好也检查一下。

Arduino循环()应该被无限调用,不像你观察到的那样。 因此,您需要检查以下两种可能性。 1) 任何返回都发生在循环()的开头。我看到了两个返回声明。您最好在它们之间插入调试消息,以便在返回此函数之前知道达到了什么程度。
2) 循环()中正在发生任何阻塞。我不知道,但你最好也检查一下。

事实证明,在脚本末尾添加这几行可以解决问题:

delay(1000);

mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();

:)

在脚本末尾添加这几行就解决了问题:

delay(1000);

mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();

:)

我认为问题不在于循环。它在不断地毁灭。您可以在循环开始时使用一些串行打印进行检查。问题是在第一次检测到一张卡后,这个程序第二次并没有识别出一张卡。所以请关注这一点。循环很好。

我认为问题不在于循环。它在不断地毁灭。您可以在循环开始时使用一些串行打印进行检查。问题是在第一次检测到一张卡后,这个程序第二次并没有识别出一张卡。所以请关注这一点。循环很好。

我建议读一读a,因为它看起来根本不像C。仅仅因为你把一个函数命名为“循环”,它就不会变成循环。你如何调用这个函数?@Lundin因为他们使用的是arduino,所以arduinos标准库在
中调用循环,而(true)
@UKMonkey不是arduino编程语言吗?这是从南佛罗里达州大学的网络教程中抄袭的。你知道如何让循环持续运行吗?@tkausl是的!这也是我的理解。那么你知道如何让void loop()连续运行吗?我建议你读一读a,因为它看起来根本不像C。仅仅因为你把一个函数命名为“loop”,它就不会变成循环。你如何调用这个函数?@Lundin因为他们使用的是arduino,所以arduinos标准库在
中调用循环,而(true)
@UKMonkey不是arduino编程语言吗?这是从南佛罗里达州大学的网络教程中抄袭的。你知道如何让循环持续运行吗?@tkausl是的!这也是我的理解。那么您知道如何使void loop()连续运行吗?谢谢您的建议!我输入调试消息。似乎在第一次成功循环之后。当我向读卡器提供第二张卡时,tmfrc522.PICC_ReadCardSerial()返回“0”而不是“1”。为什么会这样?你最好查一下库中的源代码。我不是这个图书馆的专家。但是,您肯定应该查看“PICC_Select()”函数来检查所有返回错误的可能性。找到答案:)谢谢您的建议!我输入调试消息。似乎在第一次成功循环之后。当我向读卡器提供第二张卡时,tmfrc522.PICC_ReadCardSerial()返回“0”而不是“1”。为什么会这样?你最好查一下库中的源代码。我不是这个图书馆的专家。但肯定的是,您应该查看“PICC_Select()”函数来检查所有返回错误的可能性。找到答案:)