From the sample code given, I only use Mode 2, which is getting NUID from Mifare card. It works as shown in the picture attached.
However, I would like to compare this NUID card number so that when I scan the right card, the LED will light up and buzzer will not sound to represent the door unlocks. On the other hand, when wrong card is detected, the LED will not light up and the buzzer sounds. This part does not work as my codes are not correct. Can anyone help me to correct my code?
This is the part that added after getting NUID from Mifare card:
The lcd_to_hex() function appears to print the number passed to it in HEX on the LCD and it does not return a value so it seems unlikely that the comparison above would ever do anything useful.
I am planning to use several Arduino Pro Minis to control locks.
I am putting token / card codes into an array - (the card details will be updated by plugging in via USB and re-installing the code).
I also hit the comparison issue so re-wrote the code so the card ids were in a two dimension array, I then use a nested loop to step through the array, comparing each byte read with each card.
One feature I am working on is to have separate cards that can disable the lock. This is cheaper than having a mechanical key switch.
I have attached my code - it is a work in progress as I want to improve how it works the LED and lock*, as well as adding an input for a press to exit button.
(*Using the method in the "severalthingsatthesametime" example).
I have proved the array works for storing cards, that it can read cards ok, compare them to the array and allow / reject access and if configured, disable/enable the lock.
Why bother ? Store the valid ID is an array and compare the contents one by one.
Here is a function that I wrote to demonstrate how to do it
void matchCard() //test if the card just read matches the previous one. TODO : return a boolean instead of using a global variable
{
sameCard = true; //set the test flag to true by default
for (int n = 0; n < 5; n++) //5 numbers
{
if (currentSerNum[n] != previousSerNum[n]) //if the number does not match the previous one read
{
sameCard = false; //if no match at this level then set the test flag to false
exit; //exit the for loop if no match. no point in looking further
}
}
}
This function is used to compare the card just read with the previous one but the principle is the same. The data read from each card is in an array and is compared to the card previously read character by character. If one does not match then that is flagged and the for loop, and hence the function, is exited.
There are neater ways to compare the 2 blocks of memory that the arrays use but this ways is very readable.
I agree, it is easiest to keep the comparison in the same format as read from the card - 4 bytes.
Behind the scenes the code that is used to compare two strings will step through the bytes comparing them in pairs.
My code uses 2 loops, outer one steps through the list of cards, the inner one the 4 bytes that need comparing. Simple, quick and reliable.
Wrong type of variable - bool is a logical value only - true or false.
The card contains 4 characters held in 4 bytes.
(Each byte contains a number 0-255 or hex 0x00 to 0xFF).
You appear to be trying to work with a string of numbers - the 4 bytes somehow combined / displayed without spaces between them.
Most people would code using the hex format as each byte is shown as 2 characters.
This creates an array with 4 bytes in it. If you do the same with value from the card, then a simple for loop can compare them.
e.g.
for (i=0; i <4 ; i =i+1) {
if (currentsernum == AllowedCard ) { this byte was ok } } (You would need to check all 4 bytes matched - it may be easier to code rejecting if any byte does not match).
Can I define sameCard, currentSerNum and previousSerNum like these?
No
In the forst place you would be wise to give the variables meaningful names for your program rather than using mine.
Secondly sameCard is not an array, simply a boolean variable
Thirdly, currentSerNum should be an array of chars like this
char currentSerNum[5]
It is the array that the number read from the card will be put in.
Fourth, previousSerNum should also be an array of chars containing the characters that you want to match with.