Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM
- #include <SPI.h> // RC522 Module uses SPI protocol
- #include <MFRC522.h> // Library for Mifare RC522 Devices
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- #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 // Set Led Pins
- #define greenLed 6
- #define blueLed 5
- #define relay 4 // Set Relay Pin
- #define wipeB 3 // Button pin for WipeMode
- boolean match = false; // initialize card match to false
- boolean programMode = false; // initialize programming mode to false
- boolean replaceMaster = false;
- int successRead; // Variable integer to keep if we have Successful Read from Reader
- byte storedCard[4]; // Stores an ID read from EEPROM
- byte readCard[4]; // Stores scanned ID read from RFID Module
- byte masterCard[4]; // Stores master card's ID read from EEPROM
- #define SS_PIN 10
- #define RST_PIN 9
- MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
- ///////////////////////////////////////// Setup ///////////////////////////////////
- void setup() {
- {
- lcd.begin();
- lcd.backlight();
- }
- pinMode(redLed, OUTPUT);
- pinMode(greenLed, OUTPUT);
- pinMode(blueLed, OUTPUT);
- pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
- pinMode(relay, OUTPUT);
- //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
- digitalWrite(relay, HIGH); // Make sure door is locked
- digitalWrite(redLed, LED_OFF); // Make sure led is off
- digitalWrite(greenLed, LED_OFF); // Make sure led is off
- digitalWrite(blueLed, LED_OFF); // Make sure led is off
- //Protocol Configuration
- Serial.begin(9600); // Initialize serial communications with PC
- SPI.begin(); // MFRC522 Hardware uses SPI protocol
- mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
- //If you set Antenna Gain to Max it will increase reading distance
- //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
- lcd.print(F("NFC attivo"));
- lcd.clear();// For debugging purposes
- ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
- //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
- if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
- digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
- lcd.print(F("reset premuto"));
- lcd.setCursor(0,1);
- lcd.print(F("15 secondi"));
- delay(15000);
- lcd.clear();
- if (digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
- lcd.print(F("Pulizia EEPROM"));
- for (int x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
- if (EEPROM.read(x) == 0) { //If EEPROM address 0
- // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
- }
- else {
- EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
- }
- }
- lcd.print(F("EEPROM CANCELLATA!"));
- digitalWrite(redLed, LED_OFF); // visualize successful wipe
- delay(200);
- digitalWrite(redLed, LED_ON);
- delay(200);
- digitalWrite(redLed, LED_OFF);
- delay(200);
- digitalWrite(redLed, LED_ON);
- delay(200);
- digitalWrite(redLed, LED_OFF);
- lcd.clear();
- }
- else {
- lcd.print(F("WIPE CANCELLATO"));
- digitalWrite(redLed, LED_OFF);
- lcd.clear();
- }
- }
- // Check if master card defined, if not let user choose a master card
- // This also useful to just redefine Master Card
- // You can keep other EEPROM records just write other than 143 to EEPROM address 1
- // EEPROM address 1 should hold magical number which is '143'
- if (EEPROM.read(1) != 143) {
- lcd.print(F("CONFIGURAZIONE "));
- lcd.setCursor(1,1);
- lcd.print(F("ADMIN CARD"));
- do {
- successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
- digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
- delay(200);
- digitalWrite(blueLed, LED_OFF);
- delay(200);
- lcd.clear();
- }
- while (!successRead); // Program will not go further while you not get a successful read
- for ( int j = 0; j < 4; j++ ) { // Loop 4 times
- EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
- }
- EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
- lcd.print(F("CARD ADMIN"));
- lcd.setCursor(0,1);
- lcd.print(F("CONFERMATA"));
- lcd.clear();
- }
- lcd.print(F("-------------------"));
- lcd.setCursor(1,0);
- lcd.print(F("UID CARD ADMIN"));
- for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
- masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
- lcd.print(masterCard[i], HEX);
- lcd.clear();
- }
- lcd.print(F("AVVICINARE"));
- lcd.setCursor(0,1);
- lcd.print(F("LA CARD"));
- cycleLeds(); // Everything ready lets give user some feedback by cycling leds
- }
- ///////////////////////////////////////// Main Loop ///////////////////////////////////
- void loop () {
- do {
- successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
- if (digitalRead(wipeB) == LOW) {
- digitalWrite(redLed, LED_ON); // Make sure led is off
- digitalWrite(greenLed, LED_OFF); // Make sure led is off
- digitalWrite(blueLed, LED_OFF); // Make sure led is off
- lcd.print(F("avviata procedura"));
- lcd.setCursor(0,1);
- lcd.print(F("reset!"));
- delay(10000);
- if (digitalRead(wipeB) == LOW) {
- EEPROM.write(1, 0); // Reset Magic Number.
- lcd.print(F("CARD CANCELLATA"));
- lcd.clear();
- while (1);
- }
- }
- if (programMode) {
- cycleLeds(); // Program Mode cycles through RGB waiting to read a new card
- }
- else {
- normalModeOn(); // Normal mode, blue Power LED is on, all others are off
- }
- }
- while (!successRead); //the program will not go further while you not get a successful read
- if (programMode) {
- if ( isMaster(readCard) ) { //If master card scanned again exit program mode
- lcd.print(F("ADMIN CARD"));
- lcd.setCursor(0,1);
- lcd.print(F("uscita programmazione..."));
- delay(2000);
- lcd.clear();
- programMode = false;
- return;
- }
- else {
- if ( findID(readCard) ) { // If scanned card is known delete it
- lcd.print(F("RIMOSSO L'ID"));
- deleteID(readCard);
- lcd.print(F("ins/rim della card"));
- }
- else { // If scanned card is not known add it
- lcd.print(F("ERR SCAN ID"));
- writeID(readCard);
- lcd.print(F("RIPROVA"));
- delay(2000);
- lcd.clear();
- }
- }
- }
- else {
- if ( isMaster(readCard)) { // If scanned card's ID matches Master Card's ID enter program mode
- programMode = true;
- lcd.println(F("PROGRAMMAZIONE"));
- lcd.setCursor(0,1);
- lcd.println(F("ATTIVATA!"));
- int count = EEPROM.read(0); // Read the first Byte of EEPROM that
- lcd.print(count);
- lcd.print(F("ID REGISTRATO"));
- delay(2000);
- lcd.clear();
- lcd.println(F("SCAN ID NEW"));
- }
- else {
- if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
- lcd.print(F("CREDITO INSERITO"));
- lcd.setCursor(0,1);
- lcd.print(F("NELL' ARCADE"));
- granted(300); // Open the door lock for 300 ms
- delay(3000);
- lcd.clear();
- }
- else { // If not, show that the ID was not valid
- Serial.println(F("CARD NON VALIDA"));
- denied();
- }
- }
- }
- }
- ///////////////////////////////////////// Access Granted ///////////////////////////////////
- void granted (int setDelay) {
- digitalWrite(blueLed, LED_OFF); // Turn off blue LED
- digitalWrite(redLed, LED_OFF); // Turn off red LED
- digitalWrite(greenLed, LED_ON); // Turn on green LED
- digitalWrite(relay, LOW); // Unlock door!
- delay(setDelay); // Hold door lock open for given seconds
- digitalWrite(relay, HIGH); // Relock door
- delay(1000); // Hold green LED on for a second
- }
- ///////////////////////////////////////// Access Denied ///////////////////////////////////
- void denied() {
- digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
- digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
- digitalWrite(redLed, LED_ON); // Turn on red LED
- 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 PICC'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 Software Version: 0x"));
- Serial.print(v, HEX);
- if (v == 0x91)
- Serial.print(F(" = v1.0"));
- else if (v == 0x92)
- Serial.print(F(" = v2.0"));
- else
- Serial.print(F(" (unknown),probably a chinese clone?"));
- Serial.println("");
- // When 0x00 or 0xFF is returned, communication probably failed
- if ((v == 0x00) || (v == 0xFF)) {
- Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
- Serial.println(F("SYSTEM HALTED: Check connections."));
- 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("INSERITO CON SUCCESSO NELLA EEPROM"));
- }
- else {
- failedWrite();
- Serial.println(F("ATTENZIONE! ID NON LETTO CORRETTAMENTE"));
- }
- }
- ///////////////////////////////////////// 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 EEPROM"));
- }
- 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("RIMOSSO CON SUCCESSO DALLA EEPROM"));
- }
- }
- ///////////////////////////////////////// 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement