Problems at the time of storing a tag in an array

Hi, I’m having problems when saving a RFID tag;

At the beginning I use a function to read a tag and know whether or not it exists, and works very well

But at the moment call a second function so that it reads the tag and store it in an array; It does not work, the program ignores it.

Attached the code, please do not focus much on the syntax of programming since this is an old test program.

Where is this having trouble in this part of code:

 lcd.clear();
 lcd.setCursor(0,0); lcd.print("Desea agregar"); //Imprime en la lcd
  lcd.setCursor(0,1); lcd.print("Tag"); //Imprime en la lcd
  lcd.setCursor(0,2); lcd.print("Si"); //Imprime en la lcd
  lcd.setCursor(0,3); lcd.print("Mas tarde"); //Imprime en la lcd
                               

   char kp = Teclado ();

     switch(kp){
     case '1':
              Leer_Tag(user); ////function add tag
     break;

     case '2': //menu
     lcd.clear(); 
      lcd.setCursor(0,3); lcd.print("Mas tarde"); //Imprime en la lcd
      delay(1000);
      lcd.clear();
                               
       break;
 }

In which ask you if you want to add a tag, if the user presses 1, the program ignores what contains the function

Read tag is a function that serves to keep the tag of a user

void Leer_Tag(int usuarios){
   lcd.clear ();
   lcd.setCursor(0,0); lcd.print("Acerque la tarjeta");
   
   while(1){

    // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

 for(int i=0; i < sizeof(credentials) / sizeof(USERCREDENTIALS); i ++){ //cambiar por credential, usarios credenciales
    
                if ((rfid.uid.uidByte[0] == credentials[i].rfid[0]) && (rfid.uid.uidByte[1] == credentials[i].rfid[1]) && (rfid.uid.uidByte[2] == credentials[i].rfid[3]) && (rfid.uid.uidByte[3] == credentials[i].rfid[3])) { //cambiar por credenciales
                    lcd.clear ();
                    lcd.setCursor(0,0); lcd.print("Tag existente");
                    delay(1000);
                    lcd.clear ();
                    break;
                }

                else{
                     if(i == (sizeof(credentials) / sizeof(USERCREDENTIALS)-1)){ //cambiar por credential, usarios credenciales
                       for (byte j = 0; j < 4; j++) {
                             credentials[usuarios].rfid[j] = rfid.uid.uidByte[j];
                       }
                      break;
                    }
                }
            }


            // Terminamos la lectura de la tarjeta  actual
           rfid.PICC_HaltA();
        
          // Stop encryption on PCD
          rfid.PCD_StopCrypto1();
   }
}

If I remove the part of rfid execute it well

But if I add the rfid code (as it was before), the program ignores the function

void Leer_Tag(int usuarios){

   lcd.clear ();
   lcd.setCursor(0,0); lcd.print("Acerque la tarjeta");
   delay(100);

}

Also made the attempt to replace the function “Leer_Tag ()” by the “leer_usuario ()” function (function that is at the beginning of the program and it works correctly, what it does is that if it detects a tag verifies the existence of this system) in the “case 1” switch to see if it worked and the program also was ignorant of the function.

 lcd.clear();
 lcd.setCursor(0,0); lcd.print("Desea agregar"); //Imprime en la lcd
  lcd.setCursor(0,1); lcd.print("Tag"); //Imprime en la lcd
  lcd.setCursor(0,2); lcd.print("Si"); //Imprime en la lcd
  lcd.setCursor(0,3); lcd.print("Mas tarde"); //Imprime en la lcd
                               

   char kp = Teclado ();

     switch(kp){
     case '1':
              leer_usuario() //replaced function
     break;

     case '2': //menu
     lcd.clear(); 
      lcd.setCursor(0,3); lcd.print("Mas tarde"); //Imprime en la lcd
      delay(1000);
      lcd.clear();
                               
       break;
 }

I hope your help;

Thanks a lot

(Translate by google)

rfid_prob.ino (15.5 KB)

Read tag is a function that serves to keep the tag of a user

Why does it contain an infinite loop? Get rid of the infinite loop!

Simplify the program to be more easy reading;

Is composed of two parts:

  1. a way reading, in which the user about the card and the system verifies if there is or not; in order to control access.
 leer_usuario()
  1. If user presses asterisk, enter the menu and followed saved the code of your card to the system database.

In this example pressing asterisks; the system will wait to read a card, if you read a card displays the “Hello” message on the lcd.

   if(Tecla == '*'){ //If you press * enter menu

   lcd.clear ();
   lcd.setCursor(0,0); lcd.print("Place the card near");

    // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

    //If it detects a card shows on the display "Hello"
    
     lcd.clear ();
     lcd.setCursor(0,0); lcd.print("Hello");
     delay(1000);

      // Terminamos la lectura de la tarjeta  actual
           rfid.PICC_HaltA();
        
          // Stop encryption on PCD
          rfid.PCD_StopCrypto1();
           
}

The problem is that the system comes out after pressing asterisk

#include <SPI.h>
#include <MFRC522.h>
#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal.h>




#define SS_PIN 53
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN);   // Configura los pines RST y SS del mrfc522

MFRC522::MIFARE_Key key; 





///////////////////////LCD///////////////////////////////////////

//Incializa la libreria con los numeros de pines a interactuar el LCD
LiquidCrystal lcd(A8, A9, A10, A11, A12, A13);

///////////////////////////////////////////////////////////////////


//////////////Teclado/////////////////////////////////////////////

//Definición de filas y columnas
const byte Filas = 4; //Cuatro filas
const byte Cols = 4; //Cuatro columnas

//Definición de los pines
byte Pins_Filas[] = {37, 35, 33, 31}; //Pines Arduino para las filas
byte Pins_Cols[] =  {29, 27, 25, 23}; // Pines Arduino para las columnas


//Definición de las teclas
char Teclas [ Filas ][ Cols ] =
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

//Se crea una instancia llamada Teclado1 y el cual asignara las teclas que tenemos en el arreglo "Teclas" y le decimos
//que se han conectados los pines de las filas y columnas


Keypad kpd = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);


char Teclado ()
{
  do{
    
   char Tecla = kpd.getKey();
   
    if (Tecla != NO_KEY)
    {
      return Tecla;
    } 
  } while (1);
}

/////////////////////////////////////////////////////////////////////////////////////////

#define CREDENTIALSOFFSET  11


struct USERCREDENTIALS
{
  // space for 9 character id and terminating nul character
  char id[6];
  // space for 15 character password and terminating nul character
  char pwd[10];
  byte rfid[3];
} usercredential;

// array with user credentials
USERCREDENTIALS credentials[15];



char  contrasena_ingresada[6];
char  contrasena_tecleada[10];
char  contrasena_tecleada1[10];



void setup() {
  // put your setup code here, to run once:
  lcd.begin(20, 4); //Configura el LCD con el numero de columas y filas. Para un LCD 16x4: 16 columnas y 4 filas.
  lcd.display(); //Enciende el display

  SPI.begin();               // Incializa la comunicacion SPI 
  rfid.PCD_Init(); // Init MFRC522
  
}

void loop() {
  
int estado;
int user;

//para llenar las primeras posiciones de datos
for(int i=0; i<11; i++){
  EEPROM.write(i, 1);
}


    for (byte i = 0; i < 6; i++) {
      key.keyByte[i] = 0xFF;
    }

    
   lcd.setCursor(0,0);lcd.print("read mode");   //Imprime en la LCD
   lcd.setCursor(3,3);lcd.print("Press *");   //Imprime en la LCD
   int Tecla = kpd.getKey();

    leer_usuario(); //Read a tag, and checks if it exists or not

        
    if(Tecla == '*'){ //If you press * enter menu

    //===============The system comes out, and does not execute the following code=====
   lcd.clear ();
   lcd.setCursor(0,0); lcd.print("Place the card near");

    // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;

    //If it detects a card shows on the display "Hello"
    
     lcd.clear ();
     lcd.setCursor(0,0); lcd.print("Hello");
     delay(1000);

      // Terminamos la lectura de la tarjeta  actual
           rfid.PICC_HaltA();
        
          // Stop encryption on PCD
          rfid.PCD_StopCrypto1();
    =========================================================       
}

}


//This function executes very well to the home of the void loop()

void leer_usuario(){


   // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent())
    return;

  // Verify if the NUID has been readed
  if ( ! rfid.PICC_ReadCardSerial())
    return;


   for(int user=0; user < sizeof(credentials) / sizeof(USERCREDENTIALS); user ++){ //cambiar por credential, usarios credenciales
    
                if ((rfid.uid.uidByte[0] == credentials[user].rfid[0]) && (rfid.uid.uidByte[1] == credentials[user].rfid[1]) && (rfid.uid.uidByte[2] == credentials[user].rfid[3]) && (rfid.uid.uidByte[3] == credentials[user].rfid[3])) { //cambiar por credenciales
                    lcd.clear ();
                    lcd.setCursor(0,0); lcd.print("Abrir puerta");
                    delay(1000);
                    lcd.clear ();
                }

                else{
                     if(user == (sizeof(credentials) / sizeof(USERCREDENTIALS)-1)){ //cambiar por credential, usarios credenciales
                       lcd.clear ();
                       lcd.setCursor(0,0); lcd.print("Tag inexistente");
                       delay(1000);
                       lcd.clear ();
                    }
                }
            }

      // Terminamos la lectura de la tarjeta  actual
           rfid.PICC_HaltA();
        
          // Stop encryption on PCD
          rfid.PCD_StopCrypto1();
            
}

Please help, because I almost finish my project.

Many thanks to everyone for their support.

In this program I discovered something; When I pressed asterisks and have at the same time the card near the reader displays the "hello" message on the LCD.

But if I pressed asterisks and don't have the card close to the reader, the system is out.

As I do to the system not is exit, and wait to the user approaches the card?

Sorry for the English, I am using google translator.

Thanks a lot