- مخطط الرسم البياني :
// Project : DOOR ACCESS CONTROL/ RFID DOOR LOCK
// By : DIY Channel
// My Youtube Channel : https://www.youtube.com/c/DIYChannel2019
#include <EEPROM.h>
#include <SPI.h>
#include <MFRC522.h>
#define COMMON_ANODE
#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif
#define redLed 7
#define greenLed 6
#define blueLed 5
#define relay 4
#define wipeB 3
boolean match = false;
boolean programMode = false;
int successRead;
byte storedCard[4];
byte readCard[4];
byte masterCard[4];
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(wipeB, INPUT_PULLUP);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
digitalWrite(redLed, LED_OFF);
digitalWrite(greenLed, LED_OFF);
digitalWrite(blueLed, LED_OFF);
//Protocol Configuration
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
Serial.println(F("BlueCore Tech Acces Control"));
ShowReaderDetails();
if (digitalRead(wipeB) == LOW) {
digitalWrite(redLed, LED_ON);
Serial.println(F("Wipe Button Pressed"));
Serial.println(F("You have 5 seconds to Cancel"));
Serial.println(F("All records will be removed. This cannot be undone!"));
delay(2000);
if (digitalRead(wipeB) == LOW) {
Serial.println(F("CLEARING DATABASE"));
for (int x = 0; x < EEPROM.length(); x = x + 1) {
if (EEPROM.read(x) == 0) {
}
else {
EEPROM.write(x, 0);
}
}
Serial.println(F("DATABASE Successfully Wiped"));
digitalWrite(redLed, LED_OFF);
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
}
else {
Serial.println(F("Wiping Cancelled"));
digitalWrite(redLed, LED_OFF);
}
}
if (EEPROM.read(1) != 143) {
Serial.println(F("No Master Card Set"));
Serial.println(F("Scan A RFID Card to Set as Master Card"));
do {
successRead = getID();
digitalWrite(blueLed, LED_ON);
delay(200);
digitalWrite(blueLed, LED_OFF);
delay(200);
}
while (!successRead);
for ( int j = 0; j < 4; j++ ) {
EEPROM.write( 2 + j, readCard[j] );
}
EEPROM.write(1, 143);
Serial.println(F("Master Card Set"));
}
Serial.println(F("82DD9089"));
Serial.println(F("Master Card's UID = "));
for ( int i = 0; i < 4; i++ ) {
masterCard[i] = EEPROM.read(2 + i);
Serial.print(masterCard[i], HEX);
}
Serial.println("");
Serial.println(F("F045A059"));
Serial.println(F("Everything Ready"));
Serial.println(F("Waiting for Keys or cards to be scanned"));
cycleLeds();
}
void loop () {
do {
successRead = getID();
if (programMode) {
cycleLeds();
}
else {
normalModeOn();
}
}
while (!successRead);
if (programMode) {
if ( isMaster(readCard) ) {
Serial.println(F("Master Card Scanned"));
Serial.println(F("Exiting Programming Mode"));
Serial.println(F("-----------------------------"));
programMode = false;
return;
}
else {
if ( findID(readCard) ) {
Serial.println(F("I know this key, removing..."));
deleteID(readCard);
Serial.println("-----------------------------");
}
else {
Serial.println(F("I do not know this key, adding..."));
writeID(readCard);
Serial.println(F("-----------------------------"));
}
}
}
else {
if ( isMaster(readCard) ) { // If scanned card's ID matches Master Card's ID enter program mode
programMode = true;
Serial.println(F("Hello Master - Entered Programming Mode"));
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
Serial.print(F("I have ")); // stores the number of ID's in EEPROM
Serial.print(count);
Serial.print(F(" record(s) in DATABASE"));
Serial.println("");
Serial.println(F("Scan a Card or key to ADD or REMOVE"));
Serial.println(F("-----------------------------"));
}
else {
if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
Serial.println(F("Welcome,DIY CHANNEL"));
granted(300); // Open the door lock for 300 ms
}
else { // If not, show that the ID was not valid
Serial.println(F("Acces Denied!"));
denied();
}
}
}
}
void granted (int setDelay) {
digitalWrite(blueLed, LED_OFF);
digitalWrite(redLed, LED_OFF);
digitalWrite(greenLed, LED_ON);
digitalWrite(relay, LOW);
delay(2000);
digitalWrite(relay, HIGH);
delay(1000);
}
void denied() {
digitalWrite(greenLed, LED_OFF);
digitalWrite(blueLed, LED_OFF);
digitalWrite(redLed, LED_ON);
delay(1000);
}
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
int getID() {
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
Serial.println(F("Scanned KEY's UID:"));
for (int i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x11)
Serial.print(F(" = BlueCore Tech. RFID Acces v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the RFID-MFRC522 properly connected?"));
while(true); // do not go further
}
}
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
void cycleLeds() {
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
}
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
void normalModeOn () {
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
digitalWrite(relay, HIGH); // Make sure Door is Locked
}
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
void readID( int number ) {
int start = (number * 4 ) + 2; // Figure out starting position
for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
}
}
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
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
int start = ( num * 4 ) + 6; // 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 < 4; j++ ) { // Loop 4 times
EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
}
successWrite();
Serial.println(F("Succesfully added ID record to DATABASE"));
}
else {
failedWrite();
Serial.println(F("Failed! There is something wrong with ID or bad DATABASE"));
}
}
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
void deleteID( byte a[] ) {
if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
failedWrite(); // If not
Serial.println(F("Failed! There is something wrong with ID or bad DATABASE"));
}
else {
int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
int slot; // Figure out the slot number of the card
int start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
int looping; // The number of times the loop repeats
int j;
int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
start = (slot * 4) + 2;
looping = ((num - slot) * 4);
num--; // Decrement the counter by one
EEPROM.write( 0, num ); // Write the new count to the counter
for ( j = 0; j < looping; j++ ) { // Loop the card shift times
EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
}
for ( int k = 0; k < 4; k++ ) { // Shifting loop
EEPROM.write( start + j + k, 0);
}
successDelete();
Serial.println(F("Succesfully removed ID record from DATABASE"));
}
}
///////////////////////////////////////// Check Bytes ///////////////////////////////////
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 < 4; k++ ) { // Loop 4 times
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
return true; // Return true
}
else {
return false; // Return false
}
}
///////////////////////////////////////// Find Slot ///////////////////////////////////
int findIDSLOT( byte find[] ) {
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
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[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
// is the same as the find[] ID card passed
return i; // The slot number of the card
break; // Stop looking we found it
}
}
}
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
boolean findID( byte find[] ) {
int count = EEPROM.read(0); // Read the first Byte of EEPROM that
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[4]
if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
return true;
break; // Stop looking we found it
}
else { // If not, return false
}
}
return false;
}
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
// Flashes the green LED 3 times to indicate a successful write to EEPROM
void successWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
delay(200);
}
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
// Flashes the red LED 3 times to indicate a failed write to EEPROM
void failedWrite() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
delay(200);
digitalWrite(redLed, LED_ON); // Make sure red LED is on
delay(200);
}
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
void successDelete() {
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
delay(200);
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
delay(200);
}
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
// Check to see if the ID passed is the master programing card
boolean isMaster( byte test[] ) {
if ( checkTwo( test, masterCard ) )
return true;
else
return false;
}
---------------------------------------------------------------------------
arduino,arduino rfid rc522,arduino project,arduino rfid door lock,arduino tutorial,rc522,arduino rfid reader,arduino uno,arduino rfid tutorial,rfid arduino,arduino rfid project,rfid door lock using arduino,door lock system using arduino,rfid rc522 arduino,arduino rfid buzzer,access control using rfid rc522,door security system using arduino and rfid rc522,control servo motor with rfid tag and arduino,arduino rfid,door lock using arduino,arduino rfid card,arduino rfid code,arduino projects
No comments:
Post a Comment