我有一个问题。我有一个RFID 522,我用它来通过“串行”通信。当我刷卡时,它存储了100美元的价值,每次我再次刷卡,它总是增加+100美元。然而,当我重置Arduino控制器(ESP32)并再次刷卡时,价值恢复到100美元,而不是继续积累与以前的余额。我想知道如何永久存储卡上的余额,并使用它每当我想要的。代码工作,但它只是没有将该值永久存储在卡上的细节。
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t RST_PIN = 17; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 5; // Configurable, see typical pin layout above
int currentBalance = 0;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
char c;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
}
void loop() {
// Keep the program in a continuous loop, waiting for the card to be swiped
int newCard = Read(); // Attempt to read the card
if (newCard != 0) {
currentBalance += 100; // Increment the balance by 100
Write(currentBalance); // Write the new balance to the card
Serial.print("New balance: ");
Serial.println(currentBalance);
}
}
void Write(int balance) {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
// Look for new cards
while (!mfrc522.PICC_IsNewCardPresent()) {
//return;
}
// Select one of the cards
while (!mfrc522.PICC_ReadCardSerial()) {
//return;
}
Serial.print(F("Card UID:")); //Dump UID
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.print(F(" PICC type: ")); // Dump PICC type
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
byte buffer[34];
byte block = 1;
sprintf((char *)buffer, "%d", balance);
Serial.println(balance);
MFRC522::StatusCode status;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println(F("PCD_Authenticate() success: "));
}
// Write block
status = mfrc522.MIFARE_Write(block, buffer, 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
} else {
Serial.println(F("MIFARE_Write() success: "));
}
Serial.println(" ");
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}
int Read() {
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
//-------------------------------------------
// Look for new cards
while (!mfrc522.PICC_IsNewCardPresent()) {
//return;
}
// Select one of the cards
while (!mfrc522.PICC_ReadCardSerial()) {
//return;
}
Serial.println(F("**Card Detected:**"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
len = 18;
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return currentBalance;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return currentBalance;
}
String text = "";
for (uint8_t i = 0; i < 16; i++) {
text += buffer2[i];
}
Serial.println(text);
int currentBalance = text.toInt();
currentBalance = 1;
//----------------------------------------
Serial.println(F("\n**End Reading**\n"));
delay(1000); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
return currentBalance;
}
字符串
我尝试创建新的函数、变量和其他东西,但不幸的是,我做不到。我使用了人工智能和几个论坛,但它们没有产生结果,所以我回到了我的基本代码。
1条答案
按热度按时间lc8prwob1#
你的变数太乱了。
字符串
在这里,您将从卡中读取的余额分配到
newCard
中,但不使用它。相反,您总是递增全局变量。从卡中读取的任何内容都将在此处丢失。尝试型
而不是.
在
Read
函数中,您会完全迷失:型
如果出现错误,你返回全局变量的当前值。在你的循环中,它被解释为一个新值,余额将增加。我不确定这是否是故意的。我会假设一个错误意味着你不能增加任何东西。你应该返回
0
。型
这将创建一个新的局部变量来遮蔽全局变量。无论您在此处分配什么,都不会在全局变量中结束。此外,无论您从卡中读取了什么,也会丢失,因为您立即用
1
覆盖了该值。删除该行。混合使用同名的全局变量和局部变量非常容易混淆,不应该这样做。