Advertisement
Kyngston

Arduino Door Strike

May 14th, 2014
1,352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. //Library Imports
  2. #include <SoftwareSerial.h>
  3. #include <EEPROM.h>
  4. #define doorPin 6
  5.  
  6. //START USER EDIT
  7. const int SOFT_RX = 3;
  8. const int SOFT_TX = 10;
  9. const int CARD_SIZE = 4;
  10. byte MASTER_ADD[4] = {0x12,0x34,0x56,0x78}; //change these key values to match your tag
  11. byte MASTER_DEL[4] = {0xAB,0xCD,0xEF,0x12};
  12. //END USER EDIT
  13.  
  14. SoftwareSerial rfid(SOFT_TX, SOFT_RX); //pin10 Rx, pin3 Tx, mega doesn't support change interrupts on pin 2
  15. byte readCard[CARD_SIZE];
  16. byte storedCard[CARD_SIZE];
  17. boolean match = false;
  18. boolean programMode = false;
  19.  
  20. void setup()
  21. {
  22.   pinMode(doorPin, OUTPUT);
  23.   Serial.begin(9600);
  24.   Serial.println("Welcome to Chuck's RFID access control:");
  25.   // set the data rate for the SoftwareSerial port
  26.   rfid.begin(9600);
  27.   delay(10);
  28.   startAutoScan();
  29. }
  30.  
  31. void loop() // run over and over
  32. {
  33.   if(rfid.available() > 0) {
  34.     getID();
  35.     //printID();
  36.     if (programMode)
  37.     {
  38.       if ( !checkTwo(readCard,MASTER_ADD) )
  39.       {
  40.         writeID(readCard);
  41.         Serial.println("New key added");
  42.         programMode = false;
  43.       }
  44.     }
  45.     else if ( checkTwo(readCard,MASTER_ADD) )
  46.     {
  47.       programMode = true;
  48.       Serial.println("Detected MASTER ADD key");
  49.       Serial.println("Swipe key you wish to add...");
  50.     }
  51.     else if ( checkTwo(readCard,MASTER_DEL) )
  52.     {
  53.       if (EEPROM.read(0) > 0) { EEPROM.write(0,0); }
  54.       Serial.println("Detected MASTER DEL key");
  55.       Serial.println("All guest keys disabled");
  56.     }
  57.     else if ( findID(readCard) )
  58.     {
  59.       Serial.println("Detected VALID key");
  60.       Serial.println("Open Door");
  61.       openDoor(2);
  62.     }
  63.     else
  64.     {
  65.       Serial.println("ID not valid");
  66.     }
  67.   }
  68. }
  69.  
  70.  
  71. void startAutoScan()
  72. {
  73.   rfid.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail.
  74. }
  75.  
  76.  
  77. void getID()
  78. {
  79.   for(int i=0; i<CARD_SIZE; i++){
  80.     delay(10);
  81.     readCard[i] = rfid.read();
  82.   }
  83. }
  84.  
  85. void printID()
  86. {
  87.   for(int i=0; i<CARD_SIZE; i++) {
  88.     Serial.print(readCard[i],HEX);
  89.     Serial.print(" ");
  90.   }
  91.   Serial.println();
  92. }
  93.  
  94. // Check two arrays of bytes to see if they are exact matches
  95. boolean checkTwo ( byte a[], byte b[] )
  96. {
  97.   if ( a[0] != NULL )             // Make sure there is something in the array first
  98.     match = true;                 // Assume they match at first
  99.    
  100.   for ( int k = 0;  k < CARD_SIZE; k++ )  // Loop 5 times
  101.   {
  102.     /*
  103.     Serial.print("[");
  104.     Serial.print(k);
  105.     Serial.print("] ReadCard [");
  106.     Serial.print(a[k], HEX);
  107.     Serial.print("] StoredCard [");
  108.     Serial.print(b[k], HEX);
  109.     Serial.print("] \n");
  110.     */
  111.     if ( a[k] != b[k] )           // IF a != b then set match = false, one fails, all fail
  112.      match = false;
  113.   }
  114.   if ( match )                    // Check to see if if match is still true
  115.   {
  116.     //Serial.print("Strings Match! \n");  
  117.     return true;                  // Return true
  118.   }
  119.   else {
  120.     //Serial.print("Strings do not match \n");
  121.     return false;                 // Return false
  122.   }
  123. }
  124.  
  125. // Read an ID from EEPROM and save it to the storedCard[6] array
  126. void readID( int number )  // Number = position in EEPROM to get the 5 Bytes from
  127. {
  128.    int start = (number * CARD_SIZE ) - CARD_SIZE + 1;  // Figure out starting position
  129.    //Serial.print("Start: ");
  130.    //Serial.print(start);
  131.    //Serial.print("\n\n");
  132.    
  133.    for ( int i = 0; i < CARD_SIZE; i++ )  // Loop 5 times to get the 5 Bytes
  134.    {
  135.      storedCard[i] = EEPROM.read(start+i);  // Assign values read from EEPROM to array
  136.      /*
  137.      Serial.print("Read [");
  138.      Serial.print(start+i);
  139.      Serial.print("] [");
  140.      Serial.print(storedCard[i], HEX);
  141.      Serial.print("] \n");
  142.      */
  143.    }
  144. }
  145.  
  146. // Looks in the EEPROM to try to match any of the EEPROM ID's with the passed ID
  147. boolean findID( byte find[] )
  148. {
  149.   int count = EEPROM.read(0);             // Read the first Byte of EEPROM that
  150.   //Serial.print("Count: ");                // stores the number of ID's in EEPROM
  151.   //Serial.print(count);
  152.   //Serial.print("\n");
  153.   for ( int i = 1; i <= count; i++ )      // Loop once for each EEPROM entry
  154.   {
  155.     readID(i);                            // Read an ID from EEPROM, it is stored in storedCard[6]
  156.     if( checkTwo( find, storedCard ) )    // Check to see if the storedCard read from EEPROM
  157.     {                                     // is the same as the find[] ID card passed
  158.       //Serial.print("We have a matched card!!! \n");
  159.       return true;
  160.       break;                              // Stop looking we found it
  161.     }
  162.     else                                  // If not, return false
  163.     {
  164.       //Serial.print("No Match here.... \n");
  165.     }
  166.    
  167.   }
  168.   return false;
  169. }
  170.  
  171. // Write an array to the EEPROM in the next available slot
  172. void writeID( byte a[] )
  173. {
  174.   if ( !findID( a ) )          // Before we write to the EEPROM, check to see if we have seen this card before!
  175.   {
  176.     int num = EEPROM.read(0);  // Get the numer of used spaces, position 0 stores the number of ID cards
  177.     /*
  178.     Serial.print("Num: ");
  179.     Serial.print(num);
  180.     Serial.print(" \n");
  181.     */
  182.     int start = ( num * CARD_SIZE ) + 1;   // Figure out where the next slot starts
  183.  
  184.     num++;                         // Increment the counter by one
  185.     EEPROM.write( 0, num );        // Write the new count to the counter
  186.    
  187.     for ( int j = 0; j < CARD_SIZE; j++ )  // Loop 5 times
  188.     {
  189.       EEPROM.write( start+j, a[j] );  // Write the array values to EEPROM in the right position
  190.       /*
  191.       Serial.print("W[");
  192.       Serial.print(start+j);
  193.       Serial.print("] Value [");
  194.       Serial.print(a[j], HEX);
  195.       Serial.print("] \n");
  196.       */
  197.     }
  198.     //successWrite();
  199.   }
  200.   else
  201.   {
  202.     //failedWrite();
  203.   }
  204. }
  205.  
  206. void openDoor(int setDelay)
  207. {
  208.   setDelay *=1000; // Sets delay in seconds
  209.   digitalWrite(doorPin, HIGH); // Unlock door
  210.   delay(setDelay);
  211.   digitalWrite(doorPin,LOW); // Relock door
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement