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
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..
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.
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)
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.
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.