Hi guys and happy crystmas.
How can I convert for loop to string and compare the result with another string ?
here is my code but it wont working
for (int j = 0; j < 16; j++) {
Serial.write(readbackblock[j]);
String test = readbackblock[j];
}
if (test == "sm12345698798765") {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
here is my full sketch. this code reading data from MFRC522 NFC tags and print the data to serial monitor. I want to compare the data read from the NFC card with a string.
#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 9 //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
//this is the block number we will write into and then read.
int block = 2;
int myTimeout = 0;
//This array is used for reading out a block.
byte readbackblock[18];
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
pinMode(LED_BUILTIN, OUTPUT);
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");
//Serial.setTimeout(myTimeout);
// Prepare the security key for the read and write functions.
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
}
}
void loop() {
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println("card selected");
//the blockcontent array is written into the card block
//read the block back
readBlock(block, readbackblock);
//uncomment below line if you want to see the entire 1k memory with the block written into it.
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
//print the block contents
Serial.print("read block: ");
for (int j = 0; j < 16; j++) {
Serial.write(readbackblock[j]);
}
}
//Read specific block
int readBlock(int blockNumber, byte arrayAddress[]) {
int largestModulo4Number = blockNumber / 4 * 4;
int trailerBlock = largestModulo4Number + 3; //determine trailer block for the sector
//authentication of the desired block for access
byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print("PCD_Authenticate() failed (read): ");
//Serial.println(mfrc522.GetStatusCodeName(status));
}
//reading a block
byte buffersize = 18; //we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size...
status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize); //&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
if (status != MFRC522::STATUS_OK) {
Serial.print("MIFARE_read() failed: ");
//Serial.println(mfrc522.GetStatusCodeName(status));
}
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
Serial.println("block was read");
}
String test = "";
for (int j = 0; j < 16; j++) {
Serial.write(readbackblock[j]);
// Add the character to the end of the String
test += readbackblock[j];
}