I'm working on RFID project to handle multiple cards.
I did most of the things OK. But now I have one problem. Which is the following:
- The RFID code is 5 bytes, so I need an array of 5 bytes. If more cards then the array size = number of card * 5.
- For my example I'm working with 2 cards. So array size is 10 bytes.
- I can store the first card in the first 5 bytes.
The problem: I can't store the 2nd card in the next 5 bytes in the array. I don't know why.
This is my only test code:
/****************************** LIBRARIES ******************************/
#include <SPI.h>
#include <RFID.h>
/****************************** DEFINITIONS ******************************/
// pins definitions
#define SS_PIN 10
#define RST_PIN 9
// setup definitions
#define CARDS 2
#define MXCD CARDS*5
/****************************** VARIABLES ******************************/
// rfid variables:
byte crd_buf[MXCD],card_no;
byte register_lock,check_lock,print_lock;
/****************************** FUNCTIONES ******************************/
// rfid functions
void register_accepted_card(void);
void record_new_cards(void);
void check_card(void);
void print_registered_cards(void);
// objects
RFID rfid(SS_PIN, RST_PIN);
//////////////////////////////////////////////////////////////////////
void setup(){
Serial.begin(9600);
SPI.begin();
rfid.init();
}
void loop(){
register_accepted_card(); // apply only once
}
void register_accepted_card(void){
if (!register_lock) {
if (rfid.isCard()) {
if(rfid.readCardSerial()){
if(card_no>0){
for(byte i = 0; i<card_no; i++){
if(!memcmp((crd_buf+(i*5)),rfid.serNum,5)){
Serial.println("already card is");
for(byte p = i*5; p<(i*5)+5; p++){
Serial.print(crd_buf[p],HEX);Serial.print(", ");}
Serial.println();
lcd_state = ALREADY_MSG;
register_lock = 1;
}
}
}
//////////////////////////
// problem is in this part
//////////////////////////
if(!register_lock){
for(byte i=card_no*5;i<(card_no*5)+5;i++){
crd_buf[i] = rfid.serNum[i];}
Serial.print("loop#"); Serial.println(card_no);
Serial.print("start index ");Serial.println(card_no*5);
Serial.print("end index ");Serial.println((card_no*5)+5);
Serial.println();
Serial.print("card#"); Serial.println(card_no+1);
for(byte i=card_no*5;i<(card_no*5)+5;i++){
Serial.print(crd_buf[i],HEX);Serial.print(", ");}
Serial.println();Serial.println("//////////////////////////");
card_no++;
if(card_no >= CARDS){lcd_state = FINISH_MSG;}
else{lcd_state = REGISTER_MSG;}
register_lock = 1;
}
}
}
rfid.halt();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
My second question, for the part of checking the cards after finishing registering the cards to be accepted.
I did a for loop for the number of cards to get the card that is registered in the array and send a message that the card is stored and accepted, otherwise, it returns a denied message that the card isn't stored in the array with alarm buzzer.
The problem that is the for loop checks the card for all the indexes in the array which is what I want but the result is mixed with accepted and denied.
The question is how to get out of the for loop?
void check_card(void){
if (check_lock){
if (rfid.isCard()){
if (rfid.readCardSerial()){
for(byte i=0;i<card_no;i++){
// start of for loop
Serial.print("check# ");Serial.println(i);
if(!memcmp(crd_buf+(i*5),rfid.serNum,5)){
Serial.println("card accepted");
digitalWrite(ACCEPT_LED,HIGH);
digitalWrite(DENIED_LED,LOW);
digitalWrite(BUZZER,LOW);
check_lock = 0;
sys_state = 1;
lcd_state = ACCEPTED_MSG;
}
else{
Serial.println("card denied");
digitalWrite(ACCEPT_LED,LOW);
digitalWrite(DENIED_LED,HIGH);
digitalWrite(BUZZER,HIGH);
check_lock = 0;
sys_state = 1;
lcd_state = DENIED_MSG;
}
}
// end of for loop
}
rfid.halt();
}
}
}
crd_buf[CARDS][5];
? So much simpler to deal with.