Advertisement
Double_G

ID12 Innovation + Arduino + 512kbit eeprom

Feb 15th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.56 KB | None | 0 0
  1. #include <EEPROM.h> // Needed to write to EEPROM storage
  2. #include <SPI.h>
  3.  
  4. #define powerPin    7
  5. #define failPin     6
  6. #define passPin     5
  7. #define doorPin     4
  8. #define eepromreset 9
  9.  
  10. byte olvas, cim=0;
  11. char val;
  12. boolean programMode = false;
  13. boolean match = false;
  14. byte fnb = 0;             // Felső 8 bit
  15. byte anb = 1;             // Alsó 8 bit
  16. unsigned int address;
  17.  
  18. byte storedCard[6];  // Stores an ID read from EEPROM
  19. byte readCard[6];    // Sotres an ID read from the RFID reader
  20. byte checksum = 0;   // Stores the checksum to verify the ID
  21.  
  22. void setup()
  23. {
  24.  
  25.   pinMode(10,OUTPUT);
  26.   digitalWrite(10, HIGH);
  27.   SPI.begin();  
  28.   SPI.setDataMode(SPI_MODE3);     //setup SPI protocol
  29.   SPI.setBitOrder(MSBFIRST);
  30.   pinMode(powerPin, OUTPUT);      // Connected to Blue on tri-color LED to indicate reader is ready
  31.   pinMode(passPin, OUTPUT);       // Connected to Green on tri-color LED to indicate user is valid
  32.   pinMode(failPin, OUTPUT);       // Connected to Green on tri-color LED to indicate user is NOT valid or read failed
  33.   pinMode(doorPin, OUTPUT);       // Connected to relay to activate the door lock
  34.   pinMode(eepromreset, INPUT);
  35.   digitalWrite(eepromreset, HIGH);
  36.  
  37.   Serial.begin(9600);             // Connect to the serial port
  38.  
  39.   //eeprom_erase();                   //szedd ki a kommenetet ha törölni akarod az egész tárat.
  40. }
  41.  
  42. void loop ()
  43. {  
  44.   if (digitalRead(eepromreset) == LOW) {eeprom_erase(); Serial.print("reset");}
  45.   byte val = 0;       // Temp variable to hold the current byte
  46.  
  47.     normalModeOn();     // Normal mode, blue Power LED is on, all others are off
  48.  
  49.  
  50.   if ( programMode)   // Program mode to add a new ID card
  51.   {
  52.     programModeOn();  // Program Mode cycles through RGB waiting to read a new card
  53.    
  54.     if(Serial.available() > 0)  // Waits for something to come on the serial line
  55.     {
  56.       if((val = Serial.read()) == 2)  // First Byte should be 2, STX byte
  57.       {  
  58.         getID();                      // Get the ID, sets readCard = to the read ID
  59.         if ( !isMaster(readCard) )    // Check to see if it is the master programing card
  60.         {
  61.           writeID(readCard);          // If not, write the card to the EEPROM sotrage
  62.           programMode = false;        // Turn off programing mode
  63.           checksum = 0;               // Make sure the checksum is empty
  64.         }
  65.       }
  66.     }
  67.   }
  68.  
  69.   // Normal Operation------------------------------------------------------------------
  70.   else
  71.   {
  72.     if(Serial.available() > 0)          // If the serial port is available and sending data...
  73.     {
  74.       if((val = Serial.read()) == 2)    // First Byte should be 2, STX byte
  75.       {                  
  76.         getID();                        // Get the ID, sets readCard = to the read ID
  77.         byte bytesread = 0;
  78.        
  79.         for ( int i = 0; i < 5; i++ )         // Loop 5 times
  80.         {
  81.           if  ( readCard[i] < 16 )              // Print out 0 if < 16 to prepend output
  82.             delay(0);
  83.             //Serial.print("");
  84.            
  85.           //Serial.print(readCard[i], HEX);     // Print out the hex value read in
  86.           //Serial.print(" ");
  87.         }
  88.         //Serial.println();
  89.         //Serial.print("Checksum: ");
  90.         //Serial.println(readCard[5], HEX);       // Checksum read from the card
  91.        
  92.         if ( readCard[5] == checksum )        // See if the 5th BYTE (the checksum) read in from the reader
  93.         {                                     // matches the checksum caculated
  94.           checksum = 0;                       // If so, we can empty the variable storing the calculated checksum
  95.           //Serial.println(" passed");  
  96.           //Serial.println();
  97.           if ( isMaster( readCard ) )         // Check to see if the card is the master programing card
  98.           {
  99.             programMode = true;    
  100.             //Serial.println("program mode");   // If so, enable programing mode
  101.           }
  102.           else
  103.           {
  104.             if ( findID(readCard) )           // If not, see if the card is in the EEPROM
  105.             {
  106.               openDoor(5);
  107.               ////Serial.println("nyitás");                   // If it is, open the door lock
  108.             }
  109.             else
  110.             {
  111.               failed();                       // If not, show that the ID was not valid
  112.             }
  113.           }
  114.         }
  115.         else                                  // If the checksum failed
  116.         {                                     // Print out the checksum
  117.        
  118.           //Serial.println(" error");
  119.           //Serial.println();
  120.           //Serial.print("[");
  121.           //Serial.print(readCard[5], HEX);
  122.           //Serial.print("] != [");
  123.           //Serial.print(checksum, HEX);
  124.           //Serial.print("] ");
  125.          
  126.         }
  127.       }
  128.     }
  129.   }
  130. }
  131.  
  132. // If the serial port is ready and we received the STX BYTE (2) then this function is called
  133. // to get the 4 BYTE ID + 1 BYTE checksum. The ID+checksum is stored in readCard[6]
  134. // Bytes 0-4 are the 5 ID bytes, byte 5 is the checksum
  135. // getID --------------------------------------------------------------------------------------------------
  136.  
  137. void getID()
  138. {
  139.   byte bytesread = 0;
  140.   byte i = 0;
  141.   byte val = 0;
  142.   byte tempbyte = 0;
  143.  
  144.   // 5 HEX Byte code is actually 10 ASCII Bytes.
  145.   while ( bytesread < 12 ) // Read 10 digit code + 2 digit checksum
  146.   {                        
  147.     if( Serial.available() > 0)   // Check to make sure data is coming on the serial line
  148.     {
  149.       val = Serial.read();        // Store the current ASCII byte in val
  150.                                  
  151.       if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02))
  152.       {                           // If header or stop bytes before the 10 digit reading
  153.         break;                    // Stop reading                                
  154.       }
  155.      
  156.       if ( (val >= '0' ) && ( val <= '9' ) )    // Do Ascii/Hex conversion
  157.       {
  158.         val = val - '0';
  159.       }
  160.       else if ( ( val >= 'A' ) && ( val <= 'F' ) )
  161.       {
  162.         val = 10 + val - 'A';
  163.       }
  164.  
  165.       if ( bytesread & 1 == 1 )      // Every two ASCII charactors = 1 BYTE in HEX format
  166.       {
  167.                                      // Make some space for this hex-digit by
  168.                                      // shifting the previous hex-digit with 4 bits to the left:
  169.         readCard[bytesread >> 1] = (val | (tempbyte << 4));
  170.    
  171.         if ( bytesread >> 1 != 5 )                // If we're at the checksum byte,
  172.         {
  173.           checksum ^= readCard[bytesread >> 1];   // Calculate the checksum using XOR
  174.         };
  175.       }
  176.       else                                        // If it is the first HEX charactor
  177.       {
  178.         tempbyte = val;                           // Store the HEX in a temp variable
  179.       };
  180.       bytesread++;                                // Increment the counter to keep track
  181.     }
  182.   }
  183.   bytesread = 0;
  184. }
  185.  
  186.  
  187. // Read an ID from EEPROM and save it to the storedCard[6] array
  188. //-------------------------------------------------------------------------------------------------------------
  189. void readID( int number )  // Number = position in EEPROM to get the 5 Bytes from
  190. {
  191.    unsigned int start = (number * 5 ) - 3;  // Figure out starting position
  192.    //Serial.print("Start: ");
  193.    //Serial.print(start);
  194.    //Serial.print("\n\n");
  195.    
  196.    for ( int i = 0; i < 5; i++ )  // Loop 5 times to get the 5 Bytes
  197.    {
  198.      //storedCard[i] = EEPROM.read(start+i);  // Assign values read from EEPROM to array
  199.      
  200.      address = start + i;
  201.      //Serial.print(address);
  202.      digitalWrite(10, LOW);
  203.      SPI.transfer(3); // read 3,0
  204.      SPI.transfer(address >>8);
  205.      SPI.transfer(address & 0xff); // registry
  206.      storedCard[i] = SPI.transfer(0x00);
  207.      digitalWrite(10, HIGH);
  208.      
  209.      
  210.      //Serial.print("Read [");
  211.      //Serial.print(start);
  212.      //Serial.print("] [");
  213.      //Serial.print(storedCard[i], HEX);
  214.      //Serial.print("] \n");
  215.      
  216.    }
  217. }
  218.  
  219. // Write an array to the EEPROM in the next available slot
  220. //--------------------------------------------------------------------------------------------------------
  221. void writeID( byte a[] )
  222. {
  223.   if ( !findID( a ) )             // Before we write to the EEPROM, check to see if we have seen this card before!
  224.   {
  225.      //int num = EEPROM.read(0);  // Get the numer of used spaces, position 0 stores the number of ID cards
  226.      digitalWrite(10, LOW);
  227.      SPI.transfer(3);             // read 3,0
  228.      SPI.transfer(0);
  229.      SPI.transfer(0);             // registry
  230.      int fnb = SPI.transfer(0x00);
  231.      digitalWrite(10, HIGH);
  232.  
  233.      digitalWrite(10, LOW);
  234.      SPI.transfer(3);             // read 3,0
  235.      SPI.transfer(0);
  236.      SPI.transfer(1);             // registry
  237.      int anb = SPI.transfer(0x00);
  238.      digitalWrite(10, HIGH);
  239.  
  240.      unsigned num = ((fnb * 256) + anb);
  241.          
  242.      //Serial.print("Num: ");
  243.      //Serial.print(num);
  244.      //Serial.print(" \n");
  245.    
  246.     unsigned int start = ( num * 5 ) + 2;   // Figure out where the next slot starts
  247.     //Serial.println(start);        
  248.     num++;                         // Increment the counter by one
  249.     //EEPROM.write( 0, num );        // Write the new count to the counter
  250.     //Serial.println(num);
  251.    
  252.     digitalWrite(10, LOW);
  253.     SPI.transfer(6); // write enabled 6
  254.     digitalWrite(10, HIGH);
  255.      
  256.     digitalWrite(10, LOW);
  257.     SPI.transfer(2);              // write 2,0
  258.     SPI.transfer(0);              //a cím felső 8 bit-je
  259.     SPI.transfer(0);              //a cím alsó 8 bit-je
  260.     fnb = num >>8;
  261.     SPI.transfer(fnb);            // data to record
  262.     digitalWrite(10, HIGH);
  263.     delay(5);
  264.     //Serial.print("fnb:");
  265.     //Serial.println(fnb);
  266.  
  267.     digitalWrite(10, LOW);
  268.     SPI.transfer(6); // write enabled 6
  269.     digitalWrite(10, HIGH);
  270.    
  271.  
  272.     digitalWrite(10, LOW);
  273.     SPI.transfer(2);              // write 2,0
  274.     SPI.transfer(0);              //a cím felső 8 bit-je
  275.     SPI.transfer(1);              //a cím alsó 8 bit-je
  276.     anb = num & 0x00FF;
  277.     SPI.transfer(anb);            // data to record
  278.     digitalWrite(10, HIGH);
  279.     delay(5);
  280.     //Serial.print("anb:");
  281.     //Serial.println(anb);
  282.    
  283.    
  284.    
  285.     for ( int j = 0; j < 5; j++ )  // Loop 5 times
  286.     {
  287.       //EEPROM.write( start+j, a[j] );  // Write the array values to EEPROM in the right position
  288.      
  289.       digitalWrite(10, LOW);
  290.       SPI.transfer(6); // write enabled 6
  291.       digitalWrite(10, HIGH);
  292.  
  293.       address = start + j;
  294.       digitalWrite(10, LOW);
  295.       SPI.transfer(2); // write 2,0
  296.       SPI.transfer(address >>8);           //a cím felső 8 bit-je
  297.       SPI.transfer(address & 0x00FF);      //a cím alsó 8 bit-je
  298.       SPI.transfer(a[j]); // data to record
  299.       digitalWrite(10, HIGH);
  300.       delay(4);
  301.  
  302.      
  303.       //Serial.print("W[");
  304.       //Serial.print(start);
  305.       //Serial.print(address);
  306.       //Serial.print("] Value [");
  307.       //Serial.print(a[j], HEX);
  308.       //Serial.print("] \n");
  309.      
  310.     }
  311.     successWrite();
  312.   }
  313.   else
  314.   {
  315.     failedWrite();
  316.   }
  317. }
  318.  
  319. // Check two arrays of bytes to see if they are exact matches
  320. //---------------------------------------------------------------------------------------------------------
  321.  
  322. boolean checkTwo ( byte a[], byte b[] )
  323. {
  324.   if ( a[0] != NULL )             // Make sure there is something in the array first
  325.     match = true;                 // Assume they match at first
  326.    
  327.   for ( int k = 0;  k < 5; k++ )  // Loop 5 times
  328.   {
  329.    
  330.     //Serial.print("[");
  331.     //Serial.print(k);
  332.     //Serial.print("] ReadCard [");
  333.     //Serial.print(a[k], HEX);
  334.     //Serial.print("] StoredCard [");
  335.     //Serial.print(b[k], HEX);
  336.     //Serial.print("] \n");
  337.  
  338.     if ( a[k] != b[k] )           // IF a != b then set match = false, one fails, all fail
  339.      match = false;
  340.   }
  341.   if ( match )                    // Check to see if if match is still true
  342.   {
  343.     ////Serial.print("Strings Match! \n");  
  344.     return true; //Serial.print("talaltam egyezest");                  // Return true
  345.   }
  346.   else {
  347.     ////Serial.print("Strings do not match \n");
  348.     return false; //Serial.print("nem talaltam egyezest");                 // Return false
  349.   }
  350. }
  351.  
  352. // Looks in the EEPROM to try to match any of the EEPROM ID's with the passed ID
  353. //------------------------------------------------------------------------------------------------------------
  354.  
  355. boolean findID( byte find[] )
  356. {
  357.      //int count = EEPROM.read(0);             // Read the first Byte of EEPROM that
  358.      digitalWrite(10, LOW);
  359.      SPI.transfer(3); // read 3,0
  360.      SPI.transfer(0);
  361.      SPI.transfer(0); // registry
  362.      int fnb = SPI.transfer(0x00);
  363.      digitalWrite(10, HIGH);
  364.  
  365.      digitalWrite(10, LOW);
  366.      SPI.transfer(3); // read 3,0
  367.      SPI.transfer(0);
  368.      SPI.transfer(1); // registry
  369.      int anb = SPI.transfer(0x00);
  370.      digitalWrite(10, HIGH);
  371.      
  372.      //Serial.print("fnb:");
  373.      //Serial.println(fnb);
  374.      //Serial.print("anb:");
  375.      //Serial.println(anb);
  376.      unsigned count = ((fnb * 256) + anb);
  377.      //Serial.println(count);
  378.      
  379.     //Serial.print("Count: ");                // stores the number of ID's in EEPROM
  380.     //Serial.print(count);
  381.     //Serial.print("\n");
  382.   for ( int i = 1; i <= count; i++ )      // Loop once for each EEPROM entry
  383.   {
  384.     readID(i);                            // Read an ID from EEPROM, it is stored in storedCard[6]
  385.     if( checkTwo( find, storedCard ) )    // Check to see if the storedCard read from EEPROM
  386.     {                                     // is the same as the find[] ID card passed
  387.       //Serial.print("We have a matched card!!! \n");
  388.       //Serial.print("Ajtó nyitás\n");
  389.       return true;
  390.       break;                              // Stop looking we found it
  391.     }
  392.     else                                  // If not, return false
  393.     {
  394.       //Serial.print("No Match here.... \n");
  395.     }
  396.    
  397.   }
  398.   return false;
  399. }
  400.  
  401. // Opens door and turns on the green LED for setDelay seconds
  402. //-------------------------------------------------------------------------------------------------------------
  403.  
  404. void openDoor( int setDelay )
  405. {
  406.   setDelay *= 1000; // Sets delay in seconds
  407.  
  408.   digitalWrite(powerPin, LOW);  // Turn off blue LED
  409.   digitalWrite(failPin, LOW);   // Turn off red LED
  410.   digitalWrite(passPin, HIGH);  // Turn on green LED
  411.   digitalWrite(doorPin, LOW);  // Unlock door!
  412.  
  413.   delay(setDelay); // Hold door lock open for 5 seconds
  414.  
  415.   digitalWrite(doorPin, HIGH); // Relock door
  416.   digitalWrite(passPin, LOW); // Turn off green LED
  417. }
  418.  
  419. // Flashes Red LED if failed login
  420. //-------------------------------------------------------------------------------------------------------------
  421. void failed()
  422. {
  423.   digitalWrite(passPin, LOW);   // Make sure green LED is off
  424.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  425.      
  426.   // Blink red fail LED 3 times to indicate failed key
  427.   digitalWrite(failPin, HIGH);  // Turn on red LED
  428.   delay(500);
  429.   digitalWrite(failPin, LOW);   // Turn off red LED
  430.      
  431.   digitalWrite(failPin, HIGH);  // Turn on red LED
  432.   delay(500);
  433.   digitalWrite(failPin, LOW);   // Turn off red LED
  434.      
  435.   digitalWrite(failPin, HIGH);  // Turn on red LED
  436.   delay(500);
  437.   digitalWrite(failPin, LOW);   // Turn off red LED
  438. }
  439.  
  440. // Check to see if the ID passed is the master programing card
  441. //----------------------------------------------------------------------------------------------------------
  442. boolean isMaster( byte test[] )
  443. {
  444.   byte bytesread = 0;
  445.   byte i = 0;                // Example card, replace with one of yours you want to be the master
  446.   byte val[10] = {'0','F','0','0','5','3','F','0','3','0' };  
  447.   byte master[6];
  448.   byte checksum = 0;
  449.   byte tempbyte = 0;
  450.   bytesread = 0;
  451.  
  452.   for ( i = 0; i < 10; i++ )  // First we need to convert the array above into a 5 HEX BYTE array
  453.   {
  454.     if ( (val[i] >= '0' ) && ( val[i] <= '9' ) )   // Convert one char to HEX
  455.     {
  456.       val[i] = val[i] - '0';
  457.     }
  458.     else if ( (val[i] >= 'A' ) && ( val[i] <= 'F' ) )
  459.     {
  460.       val[i] = 10 + val[i] - 'A';
  461.     }
  462.      
  463.     if (bytesread & 1 == 1) // Every two hex-digits, add byte to code:
  464.     {
  465.         // make some space for this hex-digit by
  466.         // shifting the previous hex-digit with 4 bits to the left:
  467.       master[bytesread >> 1] = (val[i] | (tempbyte << 4));
  468.    
  469.       if (bytesread >> 1 != 5)                 // If we're at the checksum byte,
  470.       {
  471.         checksum ^= master[bytesread >> 1];      // Calculate the checksum... (XOR)
  472.       };
  473.     }
  474.     else
  475.     {
  476.       tempbyte = val[i];                       // Store the first hex digit first...
  477.     };
  478.     bytesread++;        
  479.   }
  480.  
  481.   if ( checkTwo( test, master ) )                // Check to see if the master = the test ID
  482.     return true;
  483.   else
  484.     return false;
  485. }
  486.  
  487. // Controls LED's for Normal mode, Blue on, all others off
  488. //-------------------------------------------------------------------------------------------------------------
  489. void normalModeOn()
  490. {
  491.   digitalWrite(powerPin, HIGH);    // Power pin ON and ready to read card
  492.   digitalWrite(passPin, LOW);      // Make sure Green LED is off
  493.   digitalWrite(failPin, LOW);      // Make sure Red LED is off
  494.   digitalWrite(doorPin, HIGH);      // Make sure Door is Locked
  495. }
  496.    
  497. // Controls LED's for program mode, cycles through RGB
  498. //-------------------------------------------------------------------------------------------------------------
  499. void programModeOn()
  500. {
  501.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  502.   digitalWrite(failPin, LOW);   // Make sure blue LED is off
  503.   digitalWrite(passPin, HIGH);  // Make sure green LED is on
  504.   delay(200);
  505.   digitalWrite(powerPin, LOW);  // Make sure blue LED is off
  506.   digitalWrite(failPin, HIGH);  // Make sure blue LED is on
  507.   digitalWrite(passPin, LOW);   // Make sure green LED is off
  508.   delay(200);
  509.   digitalWrite(powerPin, HIGH); // Make sure blue LED is on
  510.   digitalWrite(failPin, LOW);   // Make sure blue LED is off
  511.   digitalWrite(passPin, LOW);   // Make sure green LED is off
  512.   delay(200);
  513. }
  514.  
  515. //Flashes the green LED 3 times to indicate a successful write to EEPROM
  516. //-------------------------------------------------------------------------------------------------------------
  517. void successWrite()
  518. {
  519.   digitalWrite(powerPin, LOW); // Make sure blue LED is off
  520.   digitalWrite(failPin, LOW);  // Make sure blue LED is off
  521.   digitalWrite(passPin, HIGH); // Make sure green LED is on
  522.   delay(200);
  523.   digitalWrite(passPin, LOW);  // Make sure green LED is off
  524.   delay(200);
  525.   digitalWrite(passPin, HIGH); // Make sure green LED is on
  526.   delay(200);
  527.   digitalWrite(passPin, LOW);  // Make sure green LED is off
  528.   delay(200);
  529.   digitalWrite(passPin, HIGH); // Make sure green LED is on
  530.   delay(200);
  531. }
  532.  
  533. // Flashes the red LED 3 times to indicate a failed write to EEPROM
  534. //------------------------------------------------------------------------------------------------------------
  535. void failedWrite()
  536. {
  537.   digitalWrite(powerPin, LOW); // Make sure blue LED is off
  538.   digitalWrite(failPin, HIGH); // Make sure red LED is on
  539.   digitalWrite(passPin, LOW);  // Make sure green LED is off
  540.   delay(200);
  541.   digitalWrite(failPin, LOW);  // Make sure red LED is off
  542.   delay(200);
  543.   digitalWrite(failPin, HIGH); // Make sure red LED is on
  544.   delay(200);
  545.   digitalWrite(failPin, LOW);  // Make sure red LED is off
  546.   delay(200);
  547.   digitalWrite(failPin, HIGH); // Make sure red LED is on
  548.   delay(200);
  549. }
  550. //Eeprom törlése
  551. //------------------------------------------------------------------------------------------------------------
  552. void eeprom_erase()
  553. {
  554.   Serial.println("eeprom torlese");
  555.   digitalWrite(10, LOW);
  556.   SPI.transfer(6);  
  557.   digitalWrite(10, HIGH);
  558.  
  559.   digitalWrite(10, LOW);
  560.   SPI.transfer(1);  
  561.   SPI.transfer(0);
  562.   digitalWrite(10, HIGH);
  563.  
  564.   for(unsigned int j=0; j< 2; )
  565.  {
  566.      digitalWrite(failPin, LOW);
  567.      digitalWrite(10, LOW);
  568.      SPI.transfer(6); // write enabled 6
  569.      digitalWrite(10, HIGH);
  570.      
  571.      digitalWrite(10, LOW);
  572.      SPI.transfer(2); // write 2,0
  573.      SPI.transfer(j >>8);           //a cím felső 8 bit-je
  574.      SPI.transfer(j & 0x00FF);      //a cím alsó 8 bit-je
  575.      SPI.transfer(0x00); // data to record
  576.      digitalWrite(10, HIGH);
  577.      delay(4);
  578.      
  579.      //digitalWrite(10, LOW);
  580.      //SPI.transfer(6); // write enabled 6
  581.      //digitalWrite(10, HIGH);
  582.      j++;
  583.      digitalWrite(failPin, HIGH);
  584.    
  585.   }
  586.    digitalWrite(10, LOW);
  587.    SPI.transfer(4); // write disabled 4
  588.    digitalWrite(10, HIGH);
  589.    Serial.println("eeprom_torlese_kesz");
  590.    delay(1000);
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement