Hi, I'm doing a project access control, and I have to implement in real life.
The system works as follows:
If the reader detects a new card in the system verifies whether or not, if there opens the door, otherwise no.
The user has a menu, which can save a tag in the system.
Problem:
My only problem is when saving a new label, the reader does not detect if a new card and when he does detect a new card the reader does not leave the condition, while the Arduino itself.
And as a result to bring a different card (to access the site) stored in the EEPROM memory, (and not open the door).
#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];
byte rfid[3];
} usercredential;
// array with user credentials
USERCREDENTIALS credentials[15];
char Ingresar_ID[6];
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() {
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
//If the sensor rfid detects that there is a card verify if exists or not in the system
leer_usuario();
lcd.clear();
lcd.setCursor(0,0);lcd.print("Modo lectura"); //Imprime en la LCD
lcd.setCursor(3,3);lcd.print("Presione *"); //Imprime en la LCD
int Tecla = kpd.getKey();
if(Tecla == '*'){ //Pressing asterisk access main menu
lcd.clear();
lcd.setCursor(0,0);lcd.print("Menu"); //Imprime en la LCD
lcd.setCursor(0,1);lcd.print("1. Add user"); //Imprime en la LCD
lcd.setCursor(0,2);lcd.print("2. Menu 2"); //Imprime en la LCD
kp = Teclado ();
switch (kp){
case '1':
guardar_tag();
break;
case '2':
lcd.clear();
lcd.setCursor(0,2);lcd.print("2. Menu 2"); //Imprime en la LCD
break;
}
}
}
//THIS CONDITION WORKS VERY WELL BECAUSE IT CARRIES THE 'RETURN'
bool leer_usuario(){
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return false;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return false;
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[2]) && (rfid.uid.uidByte[3] == credentials[user].rfid[3])) {
lcd.clear ();
lcd.setCursor(0,0); lcd.print("Open door");
delay(1000);
lcd.clear ();
break;
}
else{
if(user == (sizeof(credentials) / sizeof(USERCREDENTIALS)-1)){
lcd.clear ();
lcd.setCursor(0,0); lcd.print("tag does not exist");
delay(1000);
lcd.clear ();
}
}
}
// Terminamos la lectura de la tarjeta actual
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
bool guardar_tag(){
//Enter the user id
lcd.clear(); //Limpia pantalla
lcd.setCursor(3,0); lcd.print("Enter user ID"); //Imprime en la lcd
//------------------------------------------------
for(i=0; i<sizeof(Ingresar_ID); i++){ //Sizeof obtiene el tamaño del arreglo Ingresar_ID, con el fin de obtener un valor exacto y facilite al programador si decea cambiar el tamaño del arreglo, no cambiarlo el tamaño en todos las funciones que se utilizaron con dicho arreglo.
kp = Teclado (); //Obtiene el valor de la tecla pulsada
Ingresar_ID[i]=kp; //Guardar la tecla pulsada en el arreglo
if(i<5){
lcd.setCursor(5+i,3); lcd.print(Ingresar_ID[i]); //Imprime en la lcd
}
if(Ingresar_ID[i] == 'A'){ //Si presiona A compara si el ID existe o no
Ingresar_ID[i] = '\0'; //Sustituir por el caracter nul
break; //Se sale del ciclo y ejecuta la funcion de comparacion del ID
}
}
if ((Ingresar_ID[1] == '\0') || (Ingresar_ID[2] == '\0') || (Ingresar_ID[3] == '\0') || (Ingresar_ID[4] == '\0') || (Ingresar_ID[5] == '\0')){
for(int user=0; user < sizeof(credentials) / sizeof(USERCREDENTIALS); user ++){
if(strcmp (credentials[user].id, Ingresar_ID)==0){ //If you found the id
lcd.clear();//Limpia Pantalla
lcd.setCursor(2,0);lcd.print("putting the tag"); //Imprime en la LCD
delay(5000);
//Check if there is a new card
if ( ! rfid.PICC_IsNewCardPresent()){
//Check if the ID has been read
if ( ! rfid.PICC_ReadCardSerial()){
for (byte j = 0; j < 4; j++) {
credenciales[user].rfid[j] = rfid.uid.uidByte[j]; //save
}
//saved in eeprom memory
for (i = 0; i < sizeof(credentials) / sizeof(USERCREDENTIALS); i++){
EEPROM.put(i * sizeof(USERCREDENTIALS)+ CREDENTIALSOFFSET, credentials[i]);
}
lcd.clear ();
lcd.setCursor(5,1); lcd.print("save");
delay(1000);
lcd.clear();
// Terminamos la lectura de la tarjeta actual
rfid.PICC_HaltA();
delay(1000);
// Detiene la encryptacion on PCD
rfid.PCD_StopCrypto1();
}
}
break;
}
}
}
}
I am having problems with the condition "guardar_tag ()", so the reader detects a new card should of carry "return"
I them remove because at the time of put you "return" not me allows enter to the condition
//Check if there is a new card
if ( ! rfid.PICC_IsNewCardPresent()){
//return;
//Check if the ID has been read
if ( ! rfid.PICC_ReadCardSerial()){
//return;
I am sure that much of the problem is that, but at the time of put "return" does not execute the following condition.
By please help
(translated by google)
