#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];
boolean A = LOW;
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.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();
leer_usuario(); //If the reader reads a tag, check the system if it exists or not.
if(Tecla == '*'){ //If the push asterisk user, keep the tag in the system
guardar_tag();
}
}
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[2]) && (rfid.uid.uidByte[3] == credentials[user].rfid[3])) { //cambiar por credenciales
if ((rfid.uid.uidByte[0] == credentials[0].rfid[0]) && (rfid.uid.uidByte[1] == credentials[0].rfid[1]) && (rfid.uid.uidByte[2] == credentials[0].rfid[2]) && (rfid.uid.uidByte[3] == credentials[0].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();
}
void guardar_tag(){
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
for (byte j = 0; j < 4; j++) {
credentials[0].rfid[j] = rfid.uid.uidByte[j];
}
lcd.clear ();
lcd.setCursor(0,0); lcd.print("save");
delay(1000);
lcd.clear();
// Terminamos la lectura de la tarjeta actual
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}
I have a problem, when user push asterisk (to keep the tag in the system) program exits quickly condition since it does not read any card
if(Tecla == '*'){ //If the push asterisk user, keep the tag in the system
guardar_tag();
}
Even if I have the card near the rfid reader, the system exits fast since everything goes too fast and the reader does not give you time to read the card.
Only once managed to keep the code card in the system.
How do I get the system to wait some time to read a tag, without getting out of the condition?
By please help
(translate.google)