Aca dejo el codigo
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <Keypad.h>
#define RST_PIN A2//Pin 9 para el reset del RC522
#define SS_PIN A1//Pin 10 para el SS (SDA) del RC522
MFRC522 mfrc522(SS_PIN, RST_PIN); ///Creamos el objeto para el RC522
byte ActualID[7]; //almacenará el código del Tag leído
byte Usuario1[7]= {0x45, 0xCC, 0xE3, 0x2A};
byte Usuario2[7]= {0x04, 0x29, 0x3A, 0x7A, 0x76, 0x61, 0x80} ; //código del usuario 2
byte Usuario3[7]= {0x04, 0x32, 0x52, 0xDA, 0x7B, 0x56, 0x80} ;
byte Usuario4[7]= {0x04, 0x8B, 0x55, 0x92, 0xF1, 0x5A, 0x80} ;
//variables y salidas
int pir_pin = 2;
int LEDV = 3;
int LEDA = 4;
int LEDR = 5;
int scl = A4;
int sda = A5;
int RELE = 1;
int intentos = 0;
int nivel = 1;
int contador = 0;
int pin_zumbador = 6;
/Datos LCD/
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#define I2C_ADDR 0x27
LiquidCrystal_PCF8574 lcd(I2C_ADDR);//creo LCD
//DATOS DEL TECLADO
#include <Keypad.h>
const byte FILAS = 4;
const byte COLUMNAS = 3;
char keys [FILAS] [COLUMNAS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pinesFilas[FILAS] = {13,12,11,10};
byte pinesColumnas[COLUMNAS] = {9,8,7};
Keypad teclado=Keypad(makeKeymap(keys), pinesFilas, pinesColumnas, FILAS, COLUMNAS);
char pin[5];
int posicion = 0;
void setup()
{
Serial.begin(9600); // Iniciamos comunicacion serial
SPI.begin(); //Iniciamos el Bus SPI
mfrc522.PCD_Init(); // Iniciamos el MFRC522
lcd.begin(16, 2);
//lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(0);
lcd.setCursor(0,0);
lcd.print("Bienvenido a Kueyk");
pinMode (A0,OUTPUT);
pinMode (A1,OUTPUT);
pinMode (LEDR, OUTPUT);
pinMode (LEDA, OUTPUT);
pinMode (LEDV, OUTPUT);
pinMode (pir_pin, INPUT);
pinMode(pin_zumbador, OUTPUT);
digitalWrite(LEDV, LOW);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDA, LOW);
}
void loop()
{
contador++;
delay (1000);
switch (contador)
{
case 1:
if (digitalRead(pir_pin==HIGH))
{
lcd.setBacklight(0);
lcd.setCursor(0,0);
lcd.print("Bienvenido mario");
lcd.setCursor(0,1);
lcd.print("Tarjeta o Codigo");
digitalWrite(LEDV, HIGH);
digitalWrite(LEDR, LOW);
digitalWrite(LEDA, LOW);
}
else (digitalRead(pir_pin==LOW));
break;
case 2:
void tarjeta();
{
if (nivel==1)
{
bool tarjeta=RFID();
if(tarjeta)//Llama la funcion previamente declarada para leer la tarjeta y continua si es correcta
{
lcd.setBacklight(128);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ID Correcto");
lcd.setCursor(0,1);
lcd.print("Espere por favor");
digitalWrite(RELE,HIGH);
delay(2000);
lcd.clear();
}
}
}
void codigo ();
int tiempo=(millis()/1000);
Serial.print(tiempo);
if (tiempo<=60)
{
pin[posicion] = teclado.getKey();//Inicializa el Teclado
if (pin[posicion]) //si empecé a teclear la clave
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Pulse # al final");
for(int i = 0; i <= posicion; i++)
{
lcd.setCursor(i,1);
lcd.print("*");
}
posicion++;
if(posicion == 5) posicion = 0;//Si la posicion del cusor de paso reinicia la contraseña
}
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("tiempo excedido");
Serial.print("TIEMPO EXEDIDO");
delay(300);
millis()==0;
digitalWrite(LEDV,LOW);
tiempo=0;
contador=0;
intentos=0;
}
if(pin[4] == '#')
{
pin[4] = 'a';
if(pin[0] == '1' && pin[1] == '2' && pin[2] == '3' && pin[3] == '4') //Verifica si el Pin colocado es correcto
{//si es correcta
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Clave correcta ");
lcd.setCursor(0,1);
lcd.print("Espere por favor");
lcd.clear();
intentos=0;
nivel=1;
}
else
{//si no es correcta
intentos++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CLAVE INCORRECTA");
digitalWrite(LEDR,HIGH);
delay(1700);
digitalWrite(LEDR,LOW);
if (intentos==5)
{
bool alarma();
}//Si nos equivocamos 3 veces la alarma se enciende
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Nuevamente");
lcd.setCursor(0,1);
lcd.print("Ingrese clave");
}
}
delay(10);
}
}
}
//------------------------------Declaraciones de funciones---------------------------------//
bool RFID(){
// Revisamos si hay nuevas tarjetas presentes
if ( mfrc522.PICC_IsNewCardPresent()){
//Seleccionamos una tarjeta
if ( mfrc522.PICC_ReadCardSerial()){
// Enviamos serialemente su ID
Serial.print(F("Tag ID:"));
for (byte i = 0; i < mfrc522.uid.size; i++){
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
ActualID[i]=mfrc522.uid.uidByte[i];
}
Serial.print(" ");
//comparamos los ID para determinar si es uno de nuestros usuarios permitidos.
if((compareArray(ActualID,Usuario1)) || (compareArray(ActualID,Usuario2))){
return(true);
}else{
return(false);
}
// Terminamos la lectura de la tarjeta tarjeta actual
mfrc522.PICC_HaltA();
}
}
}
/Función para comparar dos vectores/
boolean compareArray(byte array1[],byte array2[]){
if(array1[0] != array2[0])return(false);
if(array1[1] != array2[1])return(false);
if(array1[2] != array2[2])return(false);
if(array1[3] != array2[3])return(false);
return(true);
}
bool alarma(){
//musica del imperio//
tone(6, 440); delay(1300);
tone(6, 349); delay(350);
tone(6, 523); delay(150);
tone(6, 440); delay(500);
tone(6, 349); delay(350);
tone(6, 523); delay(150);
tone(6, 440); delay(1000);
tone(6, 659); delay(1300);
tone(6, 698); delay(350);
tone(6, 523); delay(150);
tone(6, 415); delay(500);
tone(6, 349); delay(350);
tone(6, 523); delay(150);
tone(6, 440); delay(1000);
tone(6, 880); delay(500);
tone(6, 440); delay(500);
tone(6, 880); delay(500);
tone(6, 830); delay(250);
tone(6, 784); delay(250);
tone(6, 740); delay(125);
tone(6, 698); delay(125);
tone(6, 740); delay(500);
tone(6, 455); delay(250);
tone(6, 622); delay(500);
tone(6, 587); delay(250);
tone(6, 554); delay(250);
tone(6, 523); delay(125);
tone(6, 466); delay(125);
tone(6, 523); delay(500);
tone(6, 349); delay(125);
tone(6, 415); delay(500);
tone(6, 349); delay(375);
tone(6, 440); delay(125);
tone(6, 523); delay(500);
tone(6, 440); delay(375);
tone(6, 523); delay(125);
tone(6, 659); delay(1000);
tone(6, 880); delay(500);
tone(6, 440); delay(500);
tone(6, 880); delay(500);
tone(6, 830); delay(250);
tone(6, 784); delay(250);
tone(6, 740); delay(125);
tone(6, 698); delay(125);
tone(6, 740); delay(500);
tone(6, 445); delay(250);
tone(6, 622); delay(500);
tone(6, 587); delay(250);
tone(6, 554); delay(250);
tone(6, 523); delay(125);
tone(6, 466); delay(125);
tone(6, 523); delay(500);
tone(6, 349); delay(250);
tone(6, 415); delay(500);
tone(6, 349); delay(375);
tone(6, 523); delay(125);
tone(6, 440); delay(500);
tone(6, 349); delay(375);
tone(6, 261); delay(125);
tone(6, 440); delay(1000);
noTone(pin_zumbador);
delay(3000);
}