help with for loop

i am running this code to receive rfid data, its not the whole code, when i run this code it gives me output like (below the code).
where it finds the code it writes there(if the code placed 1st in array it displays there).
how can i receive “Authorize” “not Authorize” only when it finds whether it is in array or not

boolean asd;
for(int l=0;l<5;l++)
{
for(int o=0;o<5;o++)
{

if(tagIn[o]==data[l][o])
{
asd=true;

}
else
{
asd=false;
}

}

switch(asd)
{
case true:
{
for(int k=0;k<5;k++)
Serial.print(tagIn[k]);
Serial.println("\nAuthorize");
break;

}

case false:
{
Serial.print("\nnot Authorize\n");
break;
}

}
}

not Authorize
14522294116101
Authorize

not Authorize

not Authorize

not Authorize

Hi, welcome to the forum.

Please click the “Modify” on your first post and add [code][/code] tags around your code.

  case true:
      for(int k=0;k<5;k++)
     {
        Serial.print(tagIn[k]);
        Serial.println("\nAuthorize");
     }
     break;
#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 5

RFID rfid(SS_PIN, RST_PIN); 


int data[5][5]={{0x54,0x98,0x4C,0xCD,0x4D},
                {0x91,0xDE,0x5E,0x74,0x65}};
unsigned int tagIn[5];
void setup()
{ 
  pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
  Serial.begin(115200);
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{

    if (rfid.isCard()) {

     rfid.readCardSerial();
    
for (int j=0; j<5; j++) 
        tagIn[j] = rfid.serNum[j];

boolean asd;
boolean f;
for(int l=0;l<5;l++)
{
        for(int o=0;o<5;o++)
            {

  
  if(tagIn[o]==data[l][o])
  {
    asd=true;
    
  }
  else
  {
    asd=false;
   }

            }

  
  switch(asd)
  {
  case true:
  {
      for(int k=0;k<5;k++)
  Serial.print(tagIn[k]);
  Serial.println("\nAuthorize");
  break;
  
  }
  
  case false:
  {
  Serial.print("\nnot Authorize\n");
  break;
    }
  
  }
}

          
        

              digitalWrite(8,HIGH);
              delay(200);
              digitalWrite(8,LOW);
            
            
    
    
    rfid.halt();
}

thanks for your concern :slight_smile:
its the whole code.

i save my tag id in multi-array, i just want the code to search in that array and tell me whether it is in that array or not, kindly help....

thanks

Did you see my reply? Because you don't made the change that I point.

i did that change in the code, and i get the following result.

not Authorize 145 Authorize 222 Authorize 94 Authorize 116 Authorize 101 Authorize

not Authorize

not Authorize

not Authorize

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 5

RFID rfid(SS_PIN, RST_PIN); 


int data[5][5]={{0x54,0x98,0x4C,0xCD,0x4D},
                {0x91,0xDE,0x5E,0x74,0x65}};
unsigned int tagIn[5];
void setup()
{ 
  pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
  Serial.begin(115200);
  SPI.begin(); 
  rfid.init();
  
}

void loop()
{

    if (rfid.isCard()) {

     rfid.readCardSerial();
    
for (int j=0; j<5; j++) 
        tagIn[j] = rfid.serNum[j];

boolean asd;
boolean f;
for(int l=0;l<5;l++)
{
        for(int o=0;o<5;o++)
            {

  
  if(tagIn[o]==data[l][o])
  {
    asd=true;
    
  }
  else
  {
    asd=false;
   }

            }

  
  switch(asd)
  {
  case true:
       for(int k=0;k<5;k++)
{  
Serial.print(tagIn[k]);
  Serial.println("\nAuthorize");
}  
break;
  
  }
  
  case false:
  
  Serial.print("\nnot Authorize\n");
  break;
   
  
  }
}

          
        

              digitalWrite(8,HIGH);
              delay(200);
              digitalWrite(8,LOW);
            
            
    
    
    rfid.halt();
}

Nope. That piss-poorly indented code is just to hard to read.

Tools + Auto Format BEFORE you post code!

#include <SPI.h>
#include <RFID.h>

int data[5][5]={
  {
    0x54,0x98,0x4C,0xCD,0x4D  }
  ,
  {
    0x91,0xDE,0x5E,0x74,0x65  }
};

#define SS_PIN 10
#define RST_PIN 5

RFID rfid(SS_PIN, RST_PIN); 


unsigned int tagIn[5];
void setup()
{ 
  pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
  Serial.begin(115200);
  SPI.begin(); 
  rfid.init();

}

void loop()
{

  if (rfid.isCard()) {

    rfid.readCardSerial();

    for (int j=0; j<5; j++) 
      tagIn[j] = rfid.serNum[j];



    boolean asd;
    boolean f;
    for(int l=0;l<5;l++)
    {
      for(int o=0;o<5;o++)
      {


        if(tagIn[o]==data[l][o])
        {
          asd=true;

        }
        else
        {
          asd=false;
        }

      }


      switch(asd)
      {
      case true:
        for(int k=0;k<5;k++)
        {
          Serial.print(tagIn[k]);
          Serial.println("\nAuthorize");
        }
        break;



      case false:

        Serial.print("\nnot Authorize\n");
        break;


      }
    }


  

    digitalWrite(8,HIGH);
    delay(200);
    digitalWrite(8,LOW);

   
  }


  rfid.halt();
}
      switch(asd)
      {
      case true:
        for(int k=0;k<5;k++)
        {
          Serial.print(tagIn[k]);
          Serial.println("\nAuthorize");
        }
        break;
      case false:

        Serial.print("\nnot Authorize\n");
        break;
      }

What, no “default” case?

    for (int j=0; j<5; j++) 
      tagIn[j] = rfid.serNum[j];

There is no need to waste memory making a copy of that array.

        for(int k=0;k<5;k++)
        {
          Serial.print(tagIn[k]);
          Serial.println("\nAuthorize");
        }

Printing Authorize just once isn’t sufficient?

    for(int l=0;l<5;l++)
    {
      for(int o=0;o<5;o++)
      {

If the first entry in the 2D array matches, is it reasonable to check the second?

what to write in a default case??? :o

i want to write authorize once or if not found in 2d array "not authorize " once. thats what i am asking

Get rid of this:

      switch(asd)
      {
      case true:
        for(int k=0;k<5;k++)
        {
          Serial.print(tagIn[k]);
          Serial.println("\nAuthorize");
        }
        break;
      case false:

        Serial.print("\nnot Authorize\n");
        break;
      }

and replace it with this:

      if (asd == true) {                                 // A match?
        for(int k=0;k<5;k++)                             // Yes
        {
          Serial.print(tagIn[k]);
          Serial.print('\t');
        }
        Serial.println("\nAuthorize");
      } else {                                           // No
        Serial.print("\nnot Authorize\n");
      }

what i need to do is, i want program to search the 2d array for a specific tag number and if find in that array then print “Authorize” if not then print “not Authorize”.

now i think by replacing the code suggested by econjack i m bit closer to what i need.

but the problem is my card tag number is placed on the second position of array, when loop search that number in array, at first position it print “not Authorize” and when find in the second position then print “Authorize”.
so kindly help

Now I understand the problem!
So you need to run through all the positions of the array. If you find the tag it means that is “Authorize” and you stop. If you reach the end of the array without find the tag then (and only then) you can write “not Authorize”.

Can you translate this to code, or you need help for that?

off course i need help :~

The original code is:

#include <SPI.h>
#include <RFID.h>

int data[5][5] = {
  {
    0x54, 0x98, 0x4C, 0xCD, 0x4D
  }
  ,
  {
    0x91, 0xDE, 0x5E, 0x74, 0x65
  }
};

#define SS_PIN 10
#define RST_PIN 5

RFID rfid(SS_PIN, RST_PIN);


unsigned int tagIn[5];
void setup()
{
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  Serial.begin(115200);
  SPI.begin();
  rfid.init();

}

void loop()
{
   
   int i, j;

  if (rfid.isCard()) {

    rfid.readCardSerial();

    for (i = 0; i < 5; i++)
      tagIn[i] = rfid.serNum[i];

    boolean asd;
    boolean f;

    for (j = 0; j < 5; j++) {
      for (i = 0; i < 5; i++) {
        if (tagIn[i] == data[j][i]) {
          asd = true;
        }
        else {
          asd = false;
        }
      }

      // A match?
      if (asd == true) {                             // Yes
        for (i = 0; i < 5; i++) {                    // Print it
          Serial.print(tagIn[i]);
          Serial.print('\t');
        }
        Serial.println("\nAuthorize");              // Print "Authorize"
      } 
      else {                                        // No
        Serial.print("\nnot Authorize\n");          // Print "not Authorize"
      }
    }

    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(8, LOW);
  }

  rfid.halt();
}

with the modifications suggested by encojak.

My suggestion is:

#include <SPI.h>
#include <RFID.h>

int data[5][5] = {
  {
    0x54, 0x98, 0x4C, 0xCD, 0x4D
  }
  ,
  {
    0x91, 0xDE, 0x5E, 0x74, 0x65
  }
};

#define SS_PIN 10
#define RST_PIN 5

RFID rfid(SS_PIN, RST_PIN);


unsigned int tagIn[5];
void setup()
{
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  Serial.begin(115200);
  SPI.begin();
  rfid.init();

}

void loop()
{
   
   int i, j;

  if (rfid.isCard()) {

    rfid.readCardSerial();

    for (i = 0; i < 5; i++)
      tagIn[i] = rfid.serNum[i];

    boolean asd;
    boolean f;

    for (j = 0; j < 5; j++) {
      for (i = 0; i < 5; i++) {
        if (tagIn[i] == data[j][i]) {
          asd = true;
        }
        else {
          asd = false;
          break;                                    // if only one position is different the tag is different
        }                                           // and we can exit from the verification  
      }

      // A match?
      if (asd == true) {                             // Yes
        for (i = 0; i < 5; i++) {                    // Print it
          Serial.print(tagIn[i]);
          Serial.print('\t');
        }
        Serial.println("\nAuthorize");              // Print "Authorize"
        break;                                      // one match was found... exit from the verification.
      } 
    }
    
    if (asd == false) {                           // If there is no match
      Serial.print("\nnot Authorize\n");          // Print "not Authorize"
    }

    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(8, LOW);
  }

  rfid.halt();
}

I don’t test it, so I don’t have 100% sure that it will work.

thanks to all of u :)

finally got it :) :)