RFID Check keys in ARRAY

Hi, how´s going everyone?

Sorry about my limitations on C. But I´m totaly newbie =p
Please, anybody knows how to read all array of char values and check if it´s true?

I´m sure that there is a better (right) way to do this =D but...
I was trying this (simplified the code for this post):

// array with authorized keys

char* cartAut[] = {
   "16 C5 BA 2D", //key 1
   "XX XX XX XX", //key 2
   "XX XX XX XX", //key 3
   "XX XX XX XX", //key 4
   "XX XX XX XX", //key 5
};

// check if key is present

  if (conteudo.substring(1) == cartAut[0] || cartAut[1] || cartAut[2] || cartAut[3] || cartAut[4])
  {
    Serial.println("Acesso Liberado... ");
    Serial.print("Bem vindo: ");
    Serial.print(cartName[0]);
  }

I´m missing something? The grammar is correct?
Thank you very much.

Regards
Raphael Trajano

Please, anybody knows how to read all array of char values and check if it´s true?

I think what you want to do is check some set of values in the array against another set of values, and report whether or not all values in the sets match.

That is far simpler to do with a 2D array than it is with a 1D array.

Each row, or column, of the 2D array would contain one set of values.

A simple for loop to iterate over the values in the row (or column) counting the number of matches would result in a value that was the same as the number of values in the row (they all match) OR a smaller number (one or more didn't match).

That if statement is all wrong. You can't invent shortcuts like that.

The collection of values in one set is "16 C5 BA 2D", which is the least efficient way possible to store byte values.

PaulS, not realy.
Im trying to compare the value of a variable conteudo.substring(1) with all values of this array cartAut[].

If found: do
If not: there are other elses... lol

I need to compare with AUTHORIZED and BLOCKED Keys...

Here is the entire original code for better understanding:

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

// ******* CHAVES AUTORIZADAS *******
char* cartAut[] = {
  /*0*/ "16 C5 BA 2D", //CHAVE 1 (este é o chaveiro)
  /*1*/ "XX XX XX XX", //CHAVE 2
  /*2*/ "XX XX XX XX", //CHAVE 3
  /*3*/ "XX XX XX XX", //CHAVE 4
  /*4*/ "XX XX XX XX", //CHAVE 5
};

// ******* NOME DOS USUÁRIOS  *******
char* cartName[] = {
  /*0*/ "RAPHAEL", // USUARIO 1
  /*1*/ "NOME DO USUARIO", // USUARIO 2
  /*2*/ "NOME DO USUARIO", // USUARIO 3
  /*3*/ "NOME DO USUARIO", // USUARIO 4
  /*4*/ "NOME DO USUARIO", // USUARIO 5
};

// ******* CHAVES BLOQUEADAS *******
char* cartBlock[] = {
  /*0*/ "42 F9 1C 2B", //CHAVE BLOQUEADA 1  (este é o cartão)
  /*1*/ "XX XX XX XX", //CHAVE BLOQUEADA 2
  /*2*/ "XX XX XX XX", //CHAVE BLOQUEADA 3
  /*3*/ "XX XX XX XX", //CHAVE BLOQUEADA 4
  /*4*/ "XX XX XX XX", //CHAVE BLOQUEADA 5
};

char st[20];

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  mensageminicial();
}

void loop()
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  
  // CONVERTE O ID DO CARTÃO PARA HEXADECIMAL MAIÚSCULO
  String conteudo = "";
  byte letra;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  conteudo.toUpperCase();
  
  // ******  CHECA AUTORIZADOS *******
  if (conteudo.substring(1) == cartAut[0] || cartAut[1])
  {
    Serial.println("Acesso Liberado... ");
    Serial.print("Bem vindo: ");
    Serial.print(cartName[0]);
    Serial.println();
    delay(3000);
    mensageminicial();
  }
  
  // ******  CHECA BLOQUEADOS *******
  else if (conteudo.substring(1) == cartBlock[0])
  {
    Serial.println("Desculpe, este cartao foi bloqueado!");
    Serial.println();
    delay(3000);
  }

  // ******  AVISA QUE CARTÃO NÃO ESTÁ AUTORIZADO E IMPRIME ID *******
  else //(conteudo.substring(1) != cartAut[0])
  {
    Serial.println("Acesso negado!");
    Serial.print("UID: ");
    Serial.print(conteudo);
    Serial.println();
    delay(3000);
    mensageminicial();
  }
}

// MENSAGEM INICIAL
void mensageminicial()
{
  Serial.println();
  Serial.println("Ola! Aproxime o seu cartao do leitor...");
  Serial.println();
}

Thank you

Hello,

No the "grammar" is not correct, you could do like this:

if (conteudo.substring(1) == cartAut[0] || conteudo.substring(1) == cartAut[1] || ... )

Or, use a for loop:

for ( uint8_t i = 0; i < sizeof( cartAut ) / sizeof( cartAut[0] ); i++ )
{
  if ( conteudo.substring(1) == cartAut[ i ] )
  {
    ...
    break;
  }
}

Or, better, do not use Strings or char arrays, just use numbers.

Im trying to compare the value of a variable conteudo.substring(1)

Why are you storing the 4 bytes of RFID tag data in a String? That is the least efficient way of storing 4 bytes of data.

Comparing the 4 bytes of data AS BYTES, to 4 bytes in a row of a 2D array is trivial.

Comparing the 4 bytes of data as 11 characters is possible, but nowhere near as easy.

PaulS:
Why are you storing the 4 bytes of RFID tag data in a String? That is the least efficient way of storing 4 bytes of data.

Comparing the 4 bytes of data AS BYTES, to 4 bytes in a row of a 2D array is trivial.

Comparing the 4 bytes of data as 11 characters is possible, but nowhere near as easy.

Well I told I was newbie =p
I take this code in a tutorial, and just made some modifications for better, I supose.

I don´t image how to use this code using numbers or natural hexadecimal.
It´s simple to change this? Can you give me a north?

Thank you.

guix:
Hello,

No the "grammar" is not correct, you could do like this:

if (conteudo.substring(1) == cartAut[0] || conteudo.substring(1) == cartAut[1] || ... )

Or, use a for loop:

for ( uint8_t i = 0; i < sizeof( cartAut ) / sizeof( cartAut[0] ); i++ )

{
 if ( conteudo.substring(1) == cartAut[ i ] )
 {
   ...
   break;
 }
}





Or, better, do not use Strings or char arrays, just use numbers.

Hey guix!
Thank you for the reply. I´m going to try youy sugestion,while I learn more about "natural HEX" manipulation. =p

mfrc522.uid.uidByte is an array of bytes. There is no reason to convert this array of bytes to a String. Do NOT do so.

byte knownCards[4][] =
{
  {0x16, 0xC5, 0xBA, 0x2D},
  // Other tag values
}

   int matched = -1;
   for(byte c=0; c<sizeof(knownCards)/sizeof(knownCards[0]); c++)
   {
      if(memcmp(knownCards[c], mfrc522.uid.uidByte, sizeof(knownCards[0]) == 0)
      {
         // The scanned card matches card c
         matched = c;
         break;
      }
   }

   if(matched > 0)
      // A match was found...

Thank you for your reply.
I´ll try the sugestions this week. And return with feedback.

Thank you again.
Regards.