add array and check array - RFID

Hi,

i need a small programming help. i know only real basic about C++ programming. I’ve been trying to do it on my own since last week, but i’m unable to do it… Help please.

In the practical arduino book, there’s these 2 arrays

    // The tag database consists of two parts. The first part is an array of
        // tag values with each tag taking up 5 bytes. The second is a list of
        // names with one name for each tag (ie: group of 5 bytes).
        char* allowedTags[] = {
          "0104F5B523",         // Tag 1
          "04146E8BDD",         // Tag 2
          "0413BBBF23",         // Tag 3
        };

        // List of names to associate with the matching tag IDs
        char* tagName[] = {
          "Jonathan Oxer",      // Tag 1
          "Hugh Blemings",      // Tag 2
          "Dexter D Dog",       // Tag 3
        };

        // Check the number of tags defined
        int numberOfTags = sizeof(allowedTags)/sizeof(allowedTags[0]);

I want to add this “database” array to the code found at http://tronixstuff.com/2013/11/19/arduino-tutorials-chapter-15-rfid/ (below) :

    #include <SoftwareSerial.h>
    SoftwareSerial RFID(2, 3); // RX and TX

    int data1 = 0;
    int ok = -1;
    int yes = 13;
    int no = 12;

    // use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
    int tag1[11] = {49, 50, 48, 48, 48, 55, 49, 50, 51, 57, 13};
    int tag2[11] = {2,52,48,48,48,56,54,67,54,54,66};
    int newtag[11] = { 0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons

    void setup()
    {
      RFID.begin(9600);    // start serial to RFID reader
      Serial.begin(9600);  // start serial to PC
      pinMode(yes, OUTPUT); // for status LEDs
      pinMode(no, OUTPUT);
    }

    boolean comparetag(int aa[11], int bb[11])
    {
      boolean ff = false;
      int fg = 0;
      for (int cc = 0 ; cc < 11 ; cc++)
      {
        if (aa[cc] == bb[cc])
        {
          fg++;
        }
      }
      if (fg == 11)
      {
        ff = true;
      }
      return ff;
    }

    void checkmytags() // compares each tag against the tag just read
    {
      ok = 0; // this variable helps decision-making,
      // if it is 1 we have a match, zero is a read but no match,
      // -1 is no read attempt made
      if (comparetag(newtag, tag1) == true)
      {
        ok++;
      }
      if (comparetag(newtag, tag2) == true)
      {
        ok++;
      }
    }

    void readTags()
    {
      ok = -1;

      if (RFID.available() > 0)
      {
        // read tag numbers
        delay(100); // needed to allow time for the data to come in from the serial buffer.

        for (int z = 0 ; z < 11 ; z++) // read the rest of the tag
        {
          data1 = RFID.read();
          newtag[z] = data1;
        }
        RFID.flush(); // stops multiple reads

        // do the tags match up?
        checkmytags();
      }

      // now do something based on tag type
      if (ok > 0) // if we had a match
      {
        Serial.println("Accepted");
        digitalWrite(yes, HIGH);
        delay(1000);
        digitalWrite(yes, LOW);

        ok = -1;
      }
      else if (ok == 0) // if we didn't have a match
      {
        Serial.println("Rejected");
        digitalWrite(no, HIGH);
        delay(1000);
        digitalWrite(no, LOW);

        ok = -1;
      }
    }

    void loop()
    {
      readTags();
    }

I need it to be like when the card is read, (49 50 48 48 48 55 49 50 51 57 13 my card), it looks for the number (49 50 48 48 48 55 49 50 51 57 13) in the database array.

if number found in array, it Serial.println welcome to the corresponding user found in the tagName array. ; sends high to yes. else if number not found, it sends high to no.

I want it in this database format so that i can add users easily ( many users) , and also so that it prints the corresponding name of the tag’s user. in the code above, i can’t add users from time to time.

Please help friends… :confused: :confused:
Thanks in advance

// Use allowedTags[0] in place of tag1 and allowedTags[1] in place of tag2, etc

       int allowedTags[][11] = {
         {49, 50, 48, 48, 48, 55, 49, 50, 51, 57, 13},         // Tag 1
         {2, 52, 48, 48, 48, 56, 54, 67, 54, 54, 66}         // Tag 2
       };

// The number of entries is:
const int tagCount = sizeof allowedTags / sizeof allowedTags[0];

Thanks very much mate, it worked ;D …

I have another small issue.

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

#define PIN_SCE 11
#define PIN_RESET 12
#define PIN_DC 10
#define PIN_SDIN 9
#define PIN_SCLK 8
#define LCD_C     LOW
#define LCD_D     HIGH
#define LCD_X     84
#define LCD_Y     48

int data1 = 0;

int yes = 9;
int no = 3;
int buzzer = 6;
char Str6[15] = "Swipe Card"; // What to display on LCD by default


// Use allowedTags[0] in place of tag1 and allowedTags[1] in place of tag2, etc

       int allowedTags[][11] = {
         {49, 50, 48, 48, 48, 55, 49, 50, 51, 57, 13},         // Tag 1
         {2, 52, 48, 48, 48, 56, 54, 67, 54, 54, 66}         // Tag 2
       };
	   
       int newtag[11] = { 0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
	   
	   
	   char* tagName[] = {
      "Hanish",      // Tag 1
      "Hugh Blemings",      // Tag 2
      "Dexter D Dog",       // Tag 3
    };
	   
// The number of entries is:
const int tagCount = sizeof allowedTags / sizeof allowedTags[0];


void setup()
{
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC 
  
  pinMode(yes, OUTPUT); // for status LEDs
  pinMode(no, OUTPUT);
  
  pinMode(buzzer, OUTPUT);   // sets the pin as output
  
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
      
}


void readTags()
{
 

  if (RFID.available() > 0) 
  {
    // read tag numbers
    delay(100); // needed to allow time for the data to come in from the serial buffer.


    Serial.print("Tag id is :");
    
    for (int z = 0 ; z < 11 ; z++) // read the rest of the tag
    {
      data1 = RFID.read();
      newtag[z] = data1;
      
      Serial.print(newtag[z]);

    }
    
    Serial.println(" ");
    
    RFID.flush(); // stops multiple reads

    // Search the tag database for this particular tag
    int tagId = findTag( newtag );
		  
    if( tagId > 0 )
          {
	    cardAccepted();
          } 
          
          else 
          {
            
            Serial.println("Tag not authorized");
	    cardRejected ();
          
          }
          Serial.println();     // Blank separator line in output
        }
 }



boolean comparetag(int aa[11], int bb[11])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 11 ; cc++)
  {
    if (aa[cc] == bb[cc])
    {
      fg++;
    }
  }
  if (fg == 11)
  {
    ff = true;
  }
  return ff;
}



 /**
     * Search for a specific tag in the database
     */
int findTag( int newtag[11] ) {
      for (int thisCard = 0; thisCard < tagCount; thisCard++) {
        // Check if the tag value matches this row in the tag database
        if(comparetag(newtag, allowedTags[thisCard]) == true)
        {
          // The row in the database starts at 0, so add 1 to the result so
          // that the card ID starts from 1 instead (0 represents "no match")
          return(thisCard + 1);
        }
      }
      // If we don't find the tag return a tag ID of 0 to show there was no match
      return(0);
    }




void cardAccepted(void)      //Function when card accepted
{
  
    int tagId = findTag( newtag );
    
    Serial.print("Name : ");
    Serial.println(tagName[tagId - 1]); 
    Serial.println("Accepted");
    
    LcdInitialise();
    LcdClear();
    LcdString("Hello ");
    LcdString(tagName[tagId - 1]);
    
    analogWrite(buzzer,100);
    digitalWrite(yes, HIGH);
    
    delay(600);
    
    digitalWrite(buzzer, LOW);
    digitalWrite(yes, LOW);
    
    delay(1000);
    
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
    

    
}


void cardRejected(void)        //Function when card rejected
{
    Serial.println("Rejected");
    digitalWrite(no, HIGH);
    
    LcdInitialise();
    LcdClear();
    LcdString("NO ACCESS ");
    
    analogWrite(buzzer,10);
    delay(100);
    
    digitalWrite(no, LOW);
   
    analogWrite(buzzer,50);
    delay(500);
    digitalWrite(buzzer, LOW);
   
    digitalWrite(no, HIGH);
    delay(400);
    digitalWrite(no, LOW);
    delay(1000);
    
    LcdInitialise();
    LcdClear();
    LcdString(Str6);
    digitalWrite(no, LOW);
    
}  




static const byte ASCII[][5] =         //For LCD
{
  .
  .
  .
};

void LcdCharacter(char character)
{
  LcdWrite(LCD_D, 0x00);
  for (int index = 0; index < 5; index++)
  {
    LcdWrite(LCD_D, ASCII[character - 0x20][index]);
  }
  LcdWrite(LCD_D, 0x00);
}

void LcdClear(void)
{
  for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
  {
    LcdWrite(LCD_D, 0x00);
  }
}

void LcdInitialise(void)
{
  .
  .
  .
}

void LcdString(char *characters)
{
  while (*characters)
  {
    LcdCharacter(*characters++);
  }
}

void LcdWrite(byte dc, byte data)
{
  .
  .
  .
}


void loop()
{
  readTags();
}

int no=3;
The LED in pin 3 is always being HIGH. even when i tried to put digitalWrite(no, LOW);
I tried switching pins too.

Thank you