Convert for loop to string

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 :frowning:

  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
  }
}

Did you mean "+=" ?

(Also check the scope of "test")

Makes zero sense.
Also "string" and "String" are not the same thing.

1 Like

thanks for reply. How can I convert readbackblock[j] to String?

I have no idea because you didn't post enough code for me to tell what it is

1 Like

x
@ErfanDL

Post a full compileable sketch.
Explain what your sketch should do.
Tell, what the sketch does instead.

--> than you will get help.

1 Like

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");
}

Why not simply use memcmp?

  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];
  }
1 Like

you have the readed data in readbackblock as an array.

you can compare with memcmp


if (memcmp(readbackblock, "sm12345698798765", 16) == 0) 
{ 
  Serial.println(F(" data is similar")); 
}

edit, working example

byte readbackblock[18];

void read()  // simulate reading 
{
  memcpy (readbackblock, "sm12345698798765", 16 );
}

void compare()
{
  if (memcmp(readbackblock, "sm12345698798765", 16) == 0)
  {
    Serial.println(F(" data is similar"));
  }
}

void setup() {
  Serial.begin(115200);
  read();
  compare();
}

void loop() {
  // put your main code here, to run repeatedly:

}
1 Like

thanks so much. the problem was solved :heart_eyes:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.