RFID multiple tag code

Hi Everyone!

I am working on a code to control an automated door and want to allow multiple users access while denying others. I also want to be showing messages on the serial monitor each time to confirm functionality and be able to add different LEDs to turn on and off. I have the following code that works but do not know how to add the items in the /* comment section*/ when added I continue to get "tag has failed".

Thoughts?

Thanks!

void setup()
{
  Serial.begin(9600);
}

// Global variables -- don't get destroyed
byte goodcode[12] = {
  52, 49, 48, 48, 51, 68, 54, 65, 68, 55, 67, 49};
byte humancode[12] = { 
  51, 68, 48, 48, 52, 53, 69, 52, 49, 53, 56, 57};
int x = 0;
int human;
int dog;
boolean GOOD = 0;

void loop()
{
  byte c;
  // IF NORMAL SERIAL IS AVAILABLE
  if(Serial.available())
  {

    c = Serial.read();

    // The RFID reader sends a "2" for a new tag.
    // This code resets everything
    if(c == 2)
    {
      // Add code for the start of a new
      x = 0;
      GOOD = true;
    }

    // If the RFID tag reads in anything other than a new tag
    // Or end of the tag
    else if (c != 3)
    {
      // This code is for troubleshooting
      Serial.print("Comparing ");
      Serial.print(c);
      Serial.print(" to ");
      Serial.print(goodcode[x]);
      Serial.print(" for an x value of ");
      Serial.println(x);

      // Compares the current byte to what we want
      if(c == goodcode[x])
      {dog = 1;
        //STILL GOOD 
      }
      
      
      /////////////////////////////////////////////////////////
      /* TRYING TO ADD FUNTION THAT FOLLOWS THE FOLLOWING LOGIC
      if(c == humancode[x])
      {human = 1;
        //STILL GOOD 
      }

      */
      /////////////////////////////////////////////////////////
      
      else
      {
        // Tag has failed
        GOOD = false; 
      }
      //Whether it fails or not, increment x
      x++;
    }


    // Code executes when a "3" is sent (end of RFID)
    else
    {
      if(GOOD && (dog == 1))
      { 
        // Tag is good
        Serial.println("DogTag has passed"); 
      }    

    /////////////////////////////////////////////////////////////
    /* 
    
      if(GOOD && (human == 1))
      { 
        // Tag is good
        Serial.println("HumanTag has passed"); 
      }  
      
      */
      
      ///////////////////////////////////////////////////////////

      else 
      {
        // Tag is bad
        Serial.println("Tag has failed"); 
      } 
    }


    //Serial.println(c);
  }
}

Without wading through all that stuff, I wouldn't do it that way. You seem to be comparing on-the-fly as each byte arrives.

You want to just save the characters and compare when the 0x03 arrives, something like this:

void setup()
{
  Serial.begin(9600);
}

#define MAX_ID 20

char cur_id [MAX_ID] = "";
int len = 0;

void loop()
{
  char inByte;
  
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    
    switch (inByte)
    {
     // start of code, reset to zero length so far
      case 2:   len = 0;
                break;
      
      // end of code, check it
      case 3:  cur_id [len] = 0;
               Serial.println (cur_id);
               if (strcmp (cur_id, "5387BF327F2B") == 0)
                  {
                  //  got good code
                  delay (1000);
                  }     
                  else
                  {
                  // got bad code
                  delay (1000);
                  }     
              break;
              
        // ignore carriage return or linefeed
        case 10:
        case 13:
             break;
          
        // anything else, add to end of saved code
        default:
             if (len >= (MAX_ID - 1))
               break;
             
             cur_id [len++] = inByte;
             break;
             
      
    }  // end of switch
    
  }  // end of incoming data
}  // end of loop

Thank you for such a quick response!!!

I am new to this and sorry for being so naive, but where would I put the different "good" ID codes to store in the memory to be compared to?

If you have half-a-dozen the crude but effective way would be to make a big "if":

if (strcmp (cur_id, "5387BF327F2B") == 0 ||
    strcmp (cur_id, "12345678ABCD") == 0 ||
    strcmp (cur_id, "543254354323") == 0 ||
    strcmp (cur_id, "ABCD45454548") == 0 )
                  {

Otherwise, make an array of strings, and iterate through the array, comparing one-by-one.

Thank you ! That work perfectly!