Skip to content

Instantly share code, notes, and snippets.

@ctung
Last active August 17, 2020 16:31
Show Gist options
  • Save ctung/5917300 to your computer and use it in GitHub Desktop.
Save ctung/5917300 to your computer and use it in GitHub Desktop.
Arduino RFID Electric Door Lock Control using PN532 Mifare Reader
//Library Imports
#include <SoftwareSerial.h>
#include <EEPROM.h>
#define doorPin 6
//START USER EDIT
const int SOFT_RX = 3;
const int SOFT_TX = 10;
const int CARD_SIZE = 4;
byte MASTER_ADD[4] = {0xAB,0xCD,0xEF,0x12};
byte MASTER_DEL[4] = {0xDE,0xAD,0xBE,0xEF};
//END USER EDIT
SoftwareSerial rfid(SOFT_TX, SOFT_RX); //pin10 Rx, pin3 Tx, mega doesn't support change interrupts on pin 2
byte readCard[CARD_SIZE];
byte storedCard[CARD_SIZE];
boolean match = false;
boolean programMode = false;
void setup()
{
pinMode(doorPin, OUTPUT);
Serial.begin(9600);
Serial.println("Welcome to Chuck's RFID access control:");
// set the data rate for the SoftwareSerial port
rfid.begin(9600);
delay(10);
startAutoScan();
}
void loop() // run over and over
{
if(rfid.available() > 0) {
getID();
printID();
if (programMode)
{
if ( !checkTwo(readCard,MASTER_ADD) )
{
writeID(readCard);
Serial.println("New key added");
programMode = false;
}
}
else if ( checkTwo(readCard,MASTER_ADD) )
{
programMode = true;
Serial.println("Detected MASTER ADD key");
Serial.println("Swipe key you wish to add...");
}
else if ( checkTwo(readCard,MASTER_DEL) )
{
if (EEPROM.read(0) > 0) { EEPROM.write(0,0); }
Serial.println("Detected MASTER DEL key");
Serial.println("All guest keys disabled");
}
else if ( findID(readCard) )
{
Serial.println("Detected VALID key");
Serial.println("Open Door");
openDoor(2);
}
else
{
Serial.println("ID not valid");
}
}
}
void startAutoScan()
{
rfid.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail.
}
void getID()
{
for(int i=0; i<CARD_SIZE; i++){
delay(10);
readCard[i] = rfid.read();
}
}
void printID()
{
for(int i=0; i<CARD_SIZE; i++) {
Serial.print(readCard[i],HEX);
Serial.print(" ");
}
Serial.println();
}
// Check two arrays of bytes to see if they are exact matches
boolean checkTwo ( byte a[], byte b[] )
{
if ( a[0] != NULL ) // Make sure there is something in the array first
match = true; // Assume they match at first
for ( int k = 0; k < CARD_SIZE; k++ ) // Loop 5 times
{
/*
Serial.print("[");
Serial.print(k);
Serial.print("] ReadCard [");
Serial.print(a[k], HEX);
Serial.print("] StoredCard [");
Serial.print(b[k], HEX);
Serial.print("] \n");
*/
if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
match = false;
}
if ( match ) // Check to see if if match is still true
{
//Serial.print("Strings Match! \n");
return true; // Return true
}
else {
//Serial.print("Strings do not match \n");
return false; // Return false
}
}
// Read an ID from EEPROM and save it to the storedCard[6] array
void readID( int number ) // Number = position in EEPROM to get the 5 Bytes from
{
int start = (number * CARD_SIZE ) - CARD_SIZE + 1; // Figure out starting position
//Serial.print("Start: ");
//Serial.print(start);
//Serial.print("\n\n");
for ( int i = 0; i < CARD_SIZE; i++ ) // Loop 5 times to get the 5 Bytes
{
storedCard[i] = EEPROM.read(start+i); // Assign values read from EEPROM to array
/*
Serial.print("Read [");
Serial.print(start+i);
Serial.print("] [");
Serial.print(storedCard[i], HEX);
Serial.print("] \n");
*/
}
}
// Looks in the EEPROM to try to match any of the EEPROM ID's with the passed ID
boolean findID( byte find[] )
{
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
//Serial.print("Count: "); // stores the number of ID's in EEPROM
//Serial.print(count);
//Serial.print("\n");
for ( int i = 1; i <= count; i++ ) // Loop once for each EEPROM entry
{
readID(i); // Read an ID from EEPROM, it is stored in storedCard[6]
if( checkTwo( find, storedCard ) ) // Check to see if the storedCard read from EEPROM
{ // is the same as the find[] ID card passed
//Serial.print("We have a matched card!!! \n");
return true;
break; // Stop looking we found it
}
else // If not, return false
{
//Serial.print("No Match here.... \n");
}
}
return false;
}
// Write an array to the EEPROM in the next available slot
void writeID( byte a[] )
{
if ( !findID( a ) ) // Before we write to the EEPROM, check to see if we have seen this card before!
{
int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
/*
Serial.print("Num: ");
Serial.print(num);
Serial.print(" \n");
*/
int start = ( num * CARD_SIZE ) + 1; // Figure out where the next slot starts
num++; // Increment the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( int j = 0; j < CARD_SIZE; j++ ) // Loop 5 times
{
EEPROM.write( start+j, a[j] ); // Write the array values to EEPROM in the right position
/*
Serial.print("W[");
Serial.print(start+j);
Serial.print("] Value [");
Serial.print(a[j], HEX);
Serial.print("] \n");
*/
}
//successWrite();
}
else
{
//failedWrite();
}
}
void openDoor(int setDelay)
{
setDelay *=1000; // Sets delay in seconds
digitalWrite(doorPin, HIGH); // Unlock door
delay(setDelay);
digitalWrite(doorPin,LOW); // Relock door
}
@wendy2112
Copy link

Pse correct ide to get it working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment