Hey! I am trying to setup RFID + PIN using arduino !
My Code works perfect with just RFID! But when i am trying to add second factor authentication i.e. the pin my code doesn't work any more!
#include <SPI.h>
#include <MFRC522.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {2, 3, 4, 5}; //Rows 0 to 3
byte colPins[numCols]= {6, 7, 8, 9}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(24, 25, 26, 27, 28, 29);
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
// Init array that will store new NUID
byte nuidPICC[4];
byte knownTac0[4] = {43,149,119,172};
byte knownTac1[4] = {150,014,65,59};
byte knownTac2[4] = {102,27,07,73};
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bool success = false;
int id;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int redpin = 41;
int greenpin = 40;
void setup() {
Serial.begin(9600);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
// Print a message to the LCD and Serial.
lcd.clear();
lcd.print("Waiting for Card");
Serial.println("Waiting for Card");
digitalWrite(redpin, HIGH);
}
void loop() {
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been read
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.print("Card Detected");
Serial.println(F("Card Detected"));
delay(1000);
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
// comparing IDs
if (knownTac0[0] == nuidPICC[0] &&
knownTac0[1] == nuidPICC[1] &&
knownTac0[2] == nuidPICC[2] &&
knownTac0[3] == nuidPICC[3]){
lcd.clear();
lcd.print("Sumbal");
Serial.println(F("Sumbal"));
delay(1000);
success = keypad_loop("1111");
}
else if(knownTac1[0] == nuidPICC[0] &&
knownTac1[1] == nuidPICC[1] &&
knownTac1[2] == nuidPICC[2] &&
knownTac1[3] == nuidPICC[3]){
lcd.clear();
lcd.print("Sumbal");
Serial.println(F("Sumbal"));
delay(1000);
}
else if(knownTac2[0] == nuidPICC[0] &&
knownTac2[1] == nuidPICC[1] &&
knownTac2[2] == nuidPICC[2] &&
knownTac2[3] == nuidPICC[3]){
lcd.clear();
lcd.print("Sumbal");
Serial.println(F("Sumbal"));
delay(1000);
}
else {
lcd.clear();
lcd.print("Not Found");
Serial.println("Not found");
delay(1000);
}
if(success){
lcd.clear();
lcd.print("Door Unlocked!");
Serial.println("Door Unlocked!");
digitalWrite(redpin, LOW);
digitalWrite(greenpin, HIGH);
}
else{
lcd.clear();
lcd.print("Try again!");
Serial.println("Try again!");
}
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
// Print a message to the LCD and Serial.
delay(2000);
lcd.clear();
lcd.print("Waiting for Card");
Serial.println("Waiting for Card");
digitalWrite(greenpin, LOW);
digitalWrite(redpin, HIGH);
}
bool keypad_loop(String PIN){ // PIN section
lcd.clear();
lcd.print("Enter PIN...");
Serial.println("Enter PIN...");
String input;
int i = 0;
bool control = true;
bool success = false;
while(control == true){
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
if(i<4){ // 4-digit input
if(i==0)lcd.clear();
i++;
input.concat( String(keypressed) );
lcd.print("*");
}
if(i==4){
control = false;
if(input.equals(PIN) ){
success = true;
}
}
} // NO_KEY if ends
} // while ends
return success;
}
Here's the code i used!
The code works if i don't call "keypad_loop" using "success = keypad_loop("####")"
What might be wrong?
Using arduino UNO btw , and ignore the pin numbers for LCD and LED's!