I am trying to make an RFID reader which will change the LED output depending on if the card matches what is programmed into the sketch.
My problems seems to mainly be arising with the referencing of the char array. From my understanding arrays are always 0 referenced. This was represented when I created the array ( the first position was made using i-1 where i started at 1 and so on). At this point all seemed to go as expected.
However, I then went to Serial.print the RFID reading I created and there was a mysterious extra line underneath so it looked like this:
The testcard is: 6F0065D360B9
<-- Random space
Cards 1 and 2 are below:
6F0065D360B9
6F00663196AE
Denied
After seeing this and doing some research I inputted a null terminator. I oddly had to put it after the last character by using:
test_tag[13] = '\0';
(I thought it would be 12 and not 13)
Using 13 the space issue seems to be fixed.
Now onto the comparison between two strings.
In order to check whether the RFID reading matches the preload code I used:
String testcard=String(test_tag); // change the char array to a String
if (testcard.equals(Card1)) // test if the read card matches with the preloaded (card1) String
However, after playing around with this for a few days now I cannot get them to match!!!
Any ideas would be extremely appreciated. I've attached my code.
Thank you,
Kyle
Door_Lock.ino (1.82 KB)
char incomingByte; //For incoming serial data
char test_tag[13]; // compares to swiped RFID
String testcard;
int i=1;
char x;
// All the RFID's Add new ones to code readtag
String Card1 = "6F0065D360B9";
String Card2 = "6F00663196AE";
void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
pinMode(8, OUTPUT);
}
void loop()
{
readtag();
}
void Approved() // Action if card is approved
{
Serial.println("Approved"); // Checking code
digitalWrite(8,HIGH);
delay(5000);
digitalWrite(8,LOW);
}
void Denied() // Action if card is denied
{
Serial.println("Denied"); // Checking code
digitalWrite(8,HIGH);
delay(750);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(750);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(750);
digitalWrite(8,LOW);
}
void readtag()
{
if (Serial.available() > 0)
{
// Serial.print(i); //used to check code
incomingByte = Serial.read();
test_tag[i-1] = incomingByte;
i=i++;
delay(5); // gives enough time to make sure serial.available works
if (Serial.available()<1) // end of data
{
//x = test_tag[1]; Used to test reference- seems characters are 1 referenced
// Serial.println(x);
test_tag[13] = '\0';
String testcard=String(test_tag); // change the char array to a String
Serial.print("The testcard is: ");
Serial.println(testcard); //For debugging/checking
Serial.println("Cards 1 and 2 are below:");
Serial.println(Card1);
Serial.println(Card2);
i=1;
if (testcard.equals(Card1)) // Equals card one. Do this for each card/RFID
{
Approved();
}
else if (testcard.equals(Card2)) // Equals card two. Do this for each card/RFID
{
Approved();
}
else
{
Denied();
}
}
else
{}
}
}
It's much simpler if you just post your code.
incomingByte = Serial.read();
test_tag[i-1] = incomingByte;
i=i++;
What's that all about?
The RFID reader (RFID Reader ID-20LA (125 kHz) - SEN-11828 - SparkFun Electronics) sends each part of its ID through the serial.read(). It does this one byte at a time. I called this incomingByte. I then organized these in a char array called test_tag. At least thats what I was trying to do...
Firstly, why not start with i = 0 ? It would be much neater
Secondly
i=i++;
Very odd, to say the least. Either you are setting i equal to some value or incrementing the value of i. Which is it ? Why not justi++;
Thirdly, you are using Strings for the card IDs and a string for test_tag. You need to make your mind up one way or the other and preferably use strings (note the lowercase s) for all of them. Then you can use strcmp() to compare them.
UKHeliBob,
I finally got it to work. The problem was the RFID reader first sends out a blank output which explains all the referencing weirdness.
I also made my code cleaner per your suggestions. New to programming.
Thank you so much for your help.
Kyle