Char comperation for RFID

sorry for the repost, I just noticed it was posted in the wrong subforum

Hello,

For school we are trying to make two doors unlock depending on what RFID tag is presented to the reader.

but for some reason we can't seem to get the program to correctly compare the tag code to the codes stored in the program memory.

We have 5 tags, 4 of which will be included in the program memory, one will not be and this should result in a "invalid/access denied" output

what we want to do is that when classmate1 presents his tag, the arduino compares it to a list of stored tags, if it is a valid tag it will then show his name on a display and turn a servo (which one depends on who's tag is presented)

the code that I have so far is this one:

#include <SoftwareSerial.h>

int  val = 0; 
char a_code[10]; 
int bytesread = 0; 
int i = 10; 
char a_remco[] = {3,8,0,0,2,1,2,4,0xf,0xa};  

#define rxPin 8
#define txPin 9
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8

void setup()
{ 
  Serial.begin(9600);  // Hardware serial for Monitor 9600bps

  pinMode(2,OUTPUT);       // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
  digitalWrite(2, LOW);    // Activate the RFID reader 
}


void loop()
{

  SoftwareSerial RFID = SoftwareSerial(rxPin,txPin); 
  RFID.begin(2400);

  if((val = RFID.read()) == 10)
  {   // check for header 
    bytesread = 0; 
    while(bytesread<10)
    {  // read 10 digit code 
      val = RFID.read(); 
      if((val == 10)||(val == 13))
      {  // if header or stop bytes before the 10 digit reading 
        break;                       // stop reading 
      } 
      a_code[bytesread] = val;         // add the digit           
      bytesread++;                   // ready to read next digit  
    } 

    if(bytesread == 10)
    {  // if 10 digit read is complete 
    
    //  Serial.print("TAG code is: ");   // possibly a good TAG 
    //  Serial.println(a_code);            // print the TAG code 
      while(i--) { if (a_code[i] == a_remco[i]) {Serial.print("Remco");} else { Serial.print("Invalid"); }} ;
      i=10;
    }else{
     Serial.println("invalid");
    bytesread = 0;  
    }
     
    delay(500);                       // wait for a second
  } 
}

this is a problem that has been driving us up the wall for the past 3 to 4 weeks. we know the solution is there and shouldn't be too complicated.
the original code came from the arduino playground

while(i--) { if (a_code[i] == a_remco[i]) {Serial.print("Remco");} else { Serial.print("Invalid"); }} ;
      i=10;

probably worth re-examining this section of code.

Plus, I'm not an RFID expert, but why is the RFID setup in "loop ()"?

for some reason we can't seem to get the program to correctly compare the tag code to the codes stored in the program memory.

This isn't very helpful.
Presumably you've scattered serial prints at various stages?
What did they tell you?
Wanna share?

Try changing:

while (i--)

to

while(--i >= 0)

You're starting at i=10 but C arrays start at 0 so your elements are numbered 0 through 9.

You're starting at i=10 but C arrays start at 0 so your elements are numbered 0 through 9.

So, initially "i=10", so after "i--", "i" will be 9, which is the index of the last element of the array. Not a problem until you get to the other end..

It's a "while" loop, not a "do..while"

Or you could use a "for" loop with a "break"

at some point we got it to send out Remco across serial. but it did that 10 times, or "invalid" but again ten times. but right now all it sends out is invalid.

What do you mean by a for loop with a break?
about the why the setup is in the loop, I don't know actually. this is propably how I found the code. is it a problem if it is in the void loop?

As far as I can tell the only thing that it is doing is tell the arduino what it is communicating with and how fast.

Sadly I can't test any of the suggestions untill tomorrow at school. since that is where the RFID reader is.

But I will read all replies when I can.

Thank you so far, if there anything I can do to help you help me, then just let me know.

Taking those things one at a time:

at some point we got it to send out Remco across serial. but it did that 10 times, or "invalid" but again ten times

So, for each character in the read string that matches "REMCO", you print "REMCO". That's expected behaviour. Look at your program.

However:

What do you mean by a for loop with a break?

There's nothing in that program that will trigger anything on a string match.
One way of matching strings (apart from using the "string" library and "strcmp", or "memcmp") is to use a "for" loop. On the first character that doesn't match, you "break".
Now, if the "for" loop control variable does not match the terminal value for the loop, the strings don't match.

about the why the setup is in the loop, I don't know actually. this is propably how I found the code. is it a problem if it is in the void loop?
As far as I can tell the only thing that it is doing is tell the arduino what it is communicating with and how fast

...and probably reinitialising the character buffer every time you pass through "loop". (It tells the Arduino nothing about what it is communicating with, just how fast)

An example of a "for" loop, with a "break"

for (bytesread = 0; bytesread<10; ++bytesread)
    {  // read 10 digit code
      val = RFID.read();
      if((val == CR)||(val == LF))
      {  // if header or stop bytes before the 10 digit reading
        break;                       // stop reading
      }
      a_code[bytesread] = val;
    }

Hello,

Thank you for your help but I guess I'm just thick in the head as I still can't seem to get it working.

I don't really understand how we should use the "for" loop with a "break"

I found pastebin and I put my code up onto it.

http://www.arduino.pastebin.com/ma10c393

I also heard of something called multidimentional arrays? and a lookup command? I saw a code that should have done everything that we want of this but the programmer that made it deleted that section and forgot to save it. so I can only remember a few key words.

has any of you ever heard of these commands? becouse they don't seem to exist in arduino.

Kind regards and thank you ahead,
Alex de Jonge.

if(bytesread == 10)
    {  // if 10 digit read is complete

    //  Serial.print("TAG code is: ");   // possibly a good TAG
    //  Serial.println(a_code);            // print the TAG code
      while(i--) { if (a_code[i] == a_remco[i]) {Serial.print("Remco");} else { Serial.print("Invalid"); }} ;
      i=10;
    }else{
     Serial.println("invalid");
    bytesread = 0;
    }

Your code steps backwards a character at a time through "a_code" and "a_remco", comparing each character. If corresponding characters match, it prints "Remco", otherwise it prints "Invalid", but there is nothing to indicate that the whole string has matched.

If you google "memcmp", or better still, implement it yourself, part of your problem is solved.