Store and compare string

I have connected Arduino mega 2560 with rfid reader

Here is my sketch

int count = 0;                                         
char input[12];                                         
void setup()
{
   Serial.begin(9600);     
   Serial1.begin(9600);                                 
}
void loop()
{
   if(Serial1.available())
   {
      count = 0;
      while(Serial1.available() && count < 12)         
      {
         input[count] = Serial1.read();
         count++;
         delay(4);
      }   
      if  (count = 12){
        count = 0;
        String Value = input;
        Serial.println(Value);                     // Print RFID tag number
      }                             
   }

}

I get following output

20:38:54.795 -> FF0064F9D492
20:38:55.403 -> FF0064F6D41C

I want to give access to only five cards , if any other card is scanned except these , it should not get access

I want to gives access to following cards just for testing

FF0064F9D492
FF0064F9D492
FF0064F6D41C
FF0064F9D482
FF0064F6D47C

Here each value is string I don't understand how to store multiple strings and compare each with scan string in program ? Is there any library ?

 if  (count = 12)

use = to set a variable to a value
use == to compare a variable with a value

There may be other faults in the code but fix this common mistake first

Try changing to:
if(Serial1.available() >= 12)
This will wait for the entire message to arrive rather than waiting for one or more characters to arrive and then trying to read 12 characters.

1 Like

This check and compare only one tag


char tag[] ="51005F46642C"; // Replace with your own Tag ID
char input[12];        // A variable to store the Tag ID being presented
int count = 0;        // A counter variable to navigate through the input[] character array
boolean flag = 0;     // A variable to store the Tag match status
void setup()
{
  Serial.begin(9600);   // Initialise Serial Communication with the Serial Monitor
  Serial1.begin(9600); 
}
void loop()
{
  if(Serial1.available())// Check if there is incoming data in the RFID Reader Serial Buffer.
  {
    count = 0; // Reset the counter to zero
    /* Keep reading Byte by Byte from the Buffer till the RFID Reader Buffer is empty 
       or till 12 Bytes (the ID size of our Tag) is read */
    while(Serial1.available() && count < 12) 
    {
      input[count] = Serial1.read(); // Read 1 Byte of data and store it in the input[] variable
      count++; // increment counter
      delay(5);
    }
    /* When the counter reaches 12 (the size of the ID) we stop and compare each value 
        of the input[] to the corresponding stored value */
    if(count == 12) // 
    {
      count =0; // reset counter varibale to 0
      flag = 1;
      /* Iterate through each value and compare till either the 12 values are 
         all matching or till the first mistmatch occurs */
      while(count<12 && flag !=0)  
      {
        if(input[count]==tag[count])
        flag = 1; // everytime the values match, we set the flag variable to 1
        else
        flag= 0; 
                               /* if the ID values don't match, set flag variable to 0 and 
                                  stop comparing by exiting the while loop */
        count++; // increment i
      }
    }
    if(flag == 1) // If flag variable is 1, then it means the tags match
    {
      Serial.println("Access Allowed!");
 
    }
    else
    {
      Serial.println("Access Denied"); // Incorrect Tag Message
    }
    /* Fill the input variable array with a fixed value 'F' to overwrite 
    all values getting it empty for the next read cycle */
    for(count=0; count<12; count++) 
    {
      input[count]= 'F';
    }
    count = 0; // Reset counter variable
  }
}

I don't understand how to do for multiple tags ?

An array of tags, array access with indexing.

You should explain little bit more

This is how we declare char array in c
array[5] = { 'A', 'B', 'C', 'D', E};
or
array[5] = {"ABCDE");

but I need something like this
{" FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"}

That's why I am stuck

Yes, the thing you need is very common in C.

char *array = {" FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"};

I don't explain everything because I hope that my comments will trigger research.

You are trying to stuff 6 values into an array of length 5.

Did you mean to have an array of pointers?

char *array[] = {"FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"};

oops

You declared pointer to char type. Pointer is a variable that store address of another variable and that variable should be initialized

Yes, you can also:

char array[][] = {"FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"};

Nope. You can only leave the leftmost index empty. Since all of the strings are 13 bytes you could use:

char array[][13] = {"FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"};

1 Like

Subtle difference in the allocation, which creates an array of variable length elements in the case of:

char *array[] = { "short", "extremely-long", "so-so" };

if I am right, I have to compare new tag with five tags in the loop

That makes sense.

It sounds to me as if you need a for loop

not in the "loop()" though... it would be some while loop in your code inside loop(). Edit - Or a for loop, see post #17.

Also make your char arrays 'const'.

Yes that should run five times in while loop

I am bit struggling where to put loop and check tag in program

char tag[][13] = {"FF0064F9D492", "FF0064F9D492", "FF0064F6D41C", "FF0064F9D482", "FF0064F6D47C"};
char input[12];        // A variable to store the Tag ID being presented
int count = 0;        // A counter variable to navigate through the input[] character array
boolean flag = 0;     // A variable to store the Tag match status
void setup()
{
  Serial.begin(9600);   // Initialise Serial Communication with the Serial Monitor
  Serial1.begin(9600); 
}
void loop()
{
  if(Serial1.available())// Check if there is incoming data in the RFID Reader Serial Buffer.
  {
    count = 0; // Reset the counter to zero
    /* Keep reading Byte by Byte from the Buffer till the RFID Reader Buffer is empty 
       or till 12 Bytes (the ID size of our Tag) is read */
    while(Serial1.available() && count < 12) 
    {
      input[count] = Serial1.read(); // Read 1 Byte of data and store it in the input[] variable
      count++; // increment counter
      delay(5);
    }
    /* When the counter reaches 12 (the size of the ID) we stop and compare each value 
        of the input[] to the corresponding stored value */
    if(count == 12) // 
    {
      count =0; // reset counter varibale to 0
      flag = 1;
      /* Iterate through each value and compare till either the 12 values are 
         all matching or till the first mistmatch occurs */
      while(count<12 && flag !=0)  
      {
        if(input[count]==tag[count])
        flag = 1; // everytime the values match, we set the flag variable to 1
        else
        flag= 0; 
                               /* if the ID values don't match, set flag variable to 0 and 
                                  stop comparing by exiting the while loop */
        count++; // increment i
      }
    }
    if(flag == 1) // If flag variable is 1, then it means the tags match
    {
      Serial.println("Access Allowed!");
 
    }
    else
    {
      Serial.println("Access Denied"); // Incorrect Tag Message
    }
    /* Fill the input variable array with a fixed value 'F' to overwrite 
    all values getting it empty for the next read cycle */
    for(count=0; count<12; count++) 
    {
      input[count]= 'F';
    }
    count = 0; // Reset counter variable
  }
}

Why would it be in any place other than where you currently do the check?