roniloapin

Untitled

Mar 29th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <MFRC522.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <Servo.h>
  6. #include <Keypad.h>
  7. #include <Password.h>
  8. #include <SdFat.h>
  9.  
  10. Servo doorLock;
  11.  
  12. #define MFRC_RST_PIN 9
  13. #define MFRC_SS_PIN  10
  14. #define SD_SS_PIN    53
  15.  
  16. #define STATE_STARTUP       0
  17. #define STATE_STARTUP_ERROR 1
  18. #define STATE_STARTING      2
  19. #define STATE_WAITING       3
  20. #define STATE_SCAN_INVALID  4
  21. #define STATE_SCAN_VALID    5
  22. #define STATE_SCAN_MASTER   6
  23. #define STATE_ADDED_CARD    7
  24. #define STATE_REMOVED_CARD  8
  25.  
  26. #define REDPIN    6
  27. #define BLUEPIN   4
  28. #define BUZZER    3
  29. #define RELAY3    5
  30.  
  31. const int cardSize = 4;
  32. byte masterCard[cardSize] = {149,80,173,117};
  33. byte readCard[cardSize] = {0,0,0,0};
  34.  
  35. // Create MFRC522 instance
  36. MFRC522 mfrc522(MFRC_SS_PIN, MFRC_RST_PIN);
  37. // Set the LCD I2C address
  38. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  39.  
  40. SdFat sd;
  41.  
  42. Password password = Password( "00000" );
  43. const byte ROWS = 4; // Four rows
  44. const byte COLS = 3; // Three columns
  45. // Define the Keymap
  46. char keys[ROWS][COLS] = {
  47. {'1','2','3',},
  48. {'4','5','6',},
  49. {'7','8','9',},
  50. {'*','0',' ',}
  51. };
  52. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  53. byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
  54. byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
  55. const int buttonPin = 7;
  56. int buttonState = 0;
  57.  
  58. // Create the Keypad
  59. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  60.  
  61. byte currentState = STATE_STARTUP;
  62. unsigned long LastStateChangeTime;
  63. unsigned long StateWaitTime;
  64.  
  65. char cardFile[] = "cards.txt";
  66. char cardTempFile[] = "cardsTemp.txt";
  67.  
  68. //------------------------------------------------------------------------------------
  69. void PrintCard(byte printCard[cardSize])
  70. {
  71.   int index;
  72.  
  73.   Serial.print("Card - ");
  74.   for(index = 0; index < 4; index++)
  75.   {
  76.     if (index > 0)
  77.     {
  78.       Serial.print(",");
  79.     }
  80.     Serial.print(printCard[index]);
  81.   }
  82.   Serial.println(" ");
  83. }
  84.  
  85. //------------------------------------------------------------------------------------
  86. boolean findCard()
  87. {
  88.   byte currentCard[cardSize];
  89.   char text[10];
  90.   char c1;
  91.   int  index;
  92.   int  value;
  93.  
  94.   //Serial.print("find ");
  95.   //PrintCard(readCard);
  96.  
  97.   // open input file
  98.   ifstream readStr(cardFile);
  99.  
  100.   // check for open error
  101.   if (!readStr.is_open())
  102.   {
  103.     return false;
  104.   }
  105.  
  106.   index = 0;
  107.   // read until input fails
  108.   while (!readStr.eof())
  109.   {
  110.     readStr >> value >> c1;
  111.  
  112.     if (readStr.fail())
  113.     {
  114.       break;
  115.     }
  116.  
  117.     currentCard[index] = value;
  118.    
  119.     index++;
  120.     if (index > 3)
  121.     {
  122.       //Serial.print("file read ");
  123.       //PrintCard(currentCard);
  124.       if ((memcmp(currentCard, readCard, 4)) == 0)
  125.       {
  126.         return true;
  127.       }
  128.       index = 0;
  129.     }
  130.   }
  131.  
  132.   return false;
  133. }
  134.  
  135. //------------------------------------------------------------------------------------
  136. void addCard()
  137. {
  138.   int index;
  139.   SdFile writeFile;
  140.  
  141.   //Serial.print("add ");
  142.   //PrintCard(readCard);
  143.   if (writeFile.open(cardFile, O_RDWR | O_CREAT | O_AT_END))
  144.   {
  145.     for(index = 0; index < 4; index++)
  146.     {
  147.       writeFile.print(readCard[index]);
  148.       writeFile.print(",");
  149.     }
  150.     writeFile.close();
  151.   }
  152.   return;
  153. }
  154.  
  155. //------------------------------------------------------------------------------------
  156. void removeCard()
  157. {
  158.   byte currentCard[cardSize];
  159.   char text[10];
  160.   char c1;
  161.   int  readIndex, writeIndex;
  162.   int  value;
  163.   SdFile writeFile;
  164.  
  165.   //Serial.print("remove ");
  166.   //PrintCard(readCard);
  167.  
  168.   // open input file
  169.   ifstream readStr(cardFile);
  170.  
  171.   // check for open error
  172.   if (!readStr.is_open())
  173.   {
  174.     return;
  175.   }
  176.  
  177.   if (writeFile.open(cardTempFile, O_RDWR | O_CREAT | O_AT_END))
  178.   {
  179.     readIndex = 0;
  180.  
  181.     while (!readStr.eof())
  182.     {
  183.       readStr >> value >> c1;
  184.  
  185.       if (readStr.fail())
  186.       {
  187.         break;
  188.       }
  189.  
  190.       currentCard[readIndex] = value;
  191.    
  192.       readIndex++;
  193.       if (readIndex > 3)
  194.       {
  195.         //Serial.print("file write ");
  196.         //PrintCard(currentCard);
  197.         if (!((memcmp(currentCard, readCard, 4)) == 0))
  198.         {
  199.           for (writeIndex = 0; writeIndex < 4; writeIndex++)
  200.           {
  201.             writeFile.print(currentCard[writeIndex]);
  202.             writeFile.print(",");
  203.           }
  204.           writeFile.close();
  205.         }
  206.       }
  207.       readIndex = 0;
  208.     }
  209.   }
  210.  
  211.   sd.remove(cardFile);
  212.   sd.rename(cardTempFile, cardFile);
  213.  
  214.   return;
  215. }
  216.  
  217. //------------------------------------------------------------------------------------
  218. int readCardState()
  219. {
  220.   int index;
  221.  
  222.   for(index = 0; index < 4; index++)
  223.   {
  224.     readCard[index] = mfrc522.uid.uidByte[index];
  225.   }
  226.   //Serial.print("State ");
  227.   //PrintCard();
  228.  
  229.   //Check Master Card
  230.   if ((memcmp(readCard, masterCard, 4)) == 0)
  231.   {
  232.     return STATE_SCAN_MASTER;
  233.   }
  234.  
  235.   if (findCard() == true)
  236.   {
  237.     return STATE_SCAN_VALID;
  238.   }
  239.  
  240.  return STATE_SCAN_INVALID;
  241. }
  242.  
  243. //------------------------------------------------------------------------------------
  244. void DisplayInfo(char *Line1Str, char *Line2Str, int RedPinState, int BluePinState)
  245. {
  246.   lcd.clear();
  247.  
  248.   lcd.print(Line1Str);
  249.   lcd.setCursor(0,1);
  250.   lcd.print(Line2Str);
  251.   digitalWrite(REDPIN, RedPinState);
  252.   digitalWrite(BLUEPIN, BluePinState);
  253. }
  254.  
  255. //------------------------------------------------------------------------------------
  256. void updateState(byte aState)
  257. {
  258.   if (aState == currentState)
  259.   {
  260.     return;
  261.   }
  262.  
  263.   // do state change
  264.   switch (aState)
  265.   {
  266.     case STATE_STARTING:
  267.       DisplayInfo("RFID Scanner", "Starting up", HIGH, HIGH);
  268.       StateWaitTime = 1000;
  269.       break;
  270.      
  271.     case STATE_STARTUP_ERROR:
  272.       DisplayInfo("Error", "SD card", HIGH, HIGH);
  273.       StateWaitTime = 1000;
  274.       break;
  275.      
  276.     case STATE_WAITING:
  277.       DisplayInfo("Waiting for Card", "to be swiped", LOW, LOW);
  278.       StateWaitTime = 1000;
  279.       break;
  280.      
  281.     case STATE_SCAN_INVALID:
  282.       if (currentState == STATE_SCAN_MASTER)
  283.       {
  284.         addCard();
  285.         aState = STATE_ADDED_CARD;
  286.  
  287.         DisplayInfo("Card Scanned", "Card Added", LOW, HIGH);
  288.         StateWaitTime = 2000;
  289.       }
  290.       else if (currentState == STATE_REMOVED_CARD)
  291.       {
  292.         return;
  293.       }
  294.       else
  295.       {
  296.         DisplayInfo("Card Scanned", "Invalid Card", HIGH, LOW);
  297.         StateWaitTime = 2000;
  298.         digitalWrite(BUZZER, HIGH);
  299.         delay(200);
  300.         digitalWrite(BUZZER, LOW);
  301.         delay(200);
  302.         digitalWrite(BUZZER, HIGH);
  303.         delay(200);
  304.         digitalWrite(BUZZER, LOW);
  305.         delay(200);
  306.         digitalWrite(BUZZER, HIGH);
  307.         delay(200);
  308.         digitalWrite(BUZZER, LOW);
  309.       }
  310.       break;
  311.      
  312.     case STATE_SCAN_VALID:
  313.       if (currentState == STATE_SCAN_MASTER)
  314.       {
  315.         removeCard();
  316.         aState = STATE_REMOVED_CARD;
  317.  
  318.         DisplayInfo("Card Scanned", "Card Removed", LOW, HIGH);
  319.         StateWaitTime = 2000;
  320.        
  321.       }
  322.       else if (currentState == STATE_ADDED_CARD)
  323.       {
  324.         return;
  325.       }
  326.       else
  327.       {
  328.        
  329.         DisplayInfo("Card Scanned", "Valid Card", LOW, HIGH);
  330.         StateWaitTime = 2000;
  331.  
  332.         digitalWrite(RELAY3,LOW);
  333.         Serial.println("Light ON");
  334.         digitalWrite(BUZZER, HIGH);
  335.         doorLock.write(0);                // releases the door, you need to adjust this to positioning the servo according your door locker
  336.         delay(8000);                     // wait 5 senconds
  337.         doorLock.write(90);
  338.         digitalWrite(BUZZER, LOW);
  339.         digitalWrite(RELAY3,HIGH);
  340.         delay(10000);
  341.        
  342.  
  343.       }
  344.       break;
  345.      
  346.     case STATE_SCAN_MASTER:
  347.       DisplayInfo("Master Card", "", LOW, HIGH);
  348.       StateWaitTime = 5000;
  349.       digitalWrite(BUZZER, HIGH);
  350.         delay(200);
  351.         digitalWrite(BUZZER, LOW);
  352.       break;
  353.   }
  354.  
  355.   //Serial.print("Current State - ");
  356.   //Serial.print(currentState);
  357.   //Serial.print(", New State - ");
  358.   //Serial.println(aState);
  359.  
  360.   currentState = aState;
  361.   LastStateChangeTime = millis();
  362. }
  363.  
  364. //------------------------------------------------------------------------------------
  365. void setup()
  366. {
  367.   SPI.begin();         // Init SPI Bus
  368.   mfrc522.PCD_Init();  // Init MFRC522
  369.  
  370.   if (!sd.begin(SD_SS_PIN, SPI_HALF_SPEED))
  371.   {
  372.     updateState(STATE_STARTUP_ERROR);  
  373.   }
  374.  
  375.   lcd.begin(16,2);
  376.  
  377.   LastStateChangeTime = millis();
  378.   updateState(STATE_STARTING);
  379.   doorLock.attach(11);
  380.   pinMode(REDPIN, OUTPUT);  
  381.   pinMode(BLUEPIN, OUTPUT);
  382.   pinMode(BUZZER, OUTPUT);
  383.   pinMode(RELAY3, OUTPUT);
  384.   Serial.begin(9600);
  385.   pinMode(buttonPin, INPUT);
  386.   keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  387.   keypad.setDebounceTime(250);
  388. }
  389.  
  390. //------------------------------------------------------------------------------------
  391. void loop()
  392. {
  393.   keypad.getKey();
  394. buttonState = digitalRead(buttonPin);
  395. if (buttonState == HIGH) {
  396.  
  397. }
  398.   byte cardState;
  399.  
  400.   if ((currentState != STATE_WAITING) &&
  401.       (currentState != STATE_STARTUP_ERROR) &&
  402.       (StateWaitTime > 0) &&
  403.       (LastStateChangeTime + StateWaitTime < millis()))
  404.   {
  405.     updateState(STATE_WAITING);
  406.   }
  407.  
  408.   // Look for new cards
  409.   if ( ! mfrc522.PICC_IsNewCardPresent())
  410.   {
  411.     return;
  412.   }
  413.  
  414.   // Select one of the cards
  415.   if ( ! mfrc522.PICC_ReadCardSerial())
  416.   {
  417.     return;
  418.   }
  419.  
  420.   if (currentState != STATE_STARTUP_ERROR)
  421.   {
  422.     cardState = readCardState();
  423.     updateState(cardState);
  424.   }
  425.    
  426. }
  427.  
  428. void keypadEvent(KeypadEvent eKey){
  429. switch (keypad.getState()){
  430. case PRESSED:
  431. Serial.print(eKey);
  432. switch (eKey){
  433. case ' ': guessPassword(); break;
  434. default:
  435. password.append(eKey);
  436. }
  437. }}
  438.  
  439. void guessPassword(){
  440. if (password.evaluate()){
  441. lcd.setCursor(0,0);
  442. lcd.print(" VALID PASSWORD    "); //
  443. password.reset(); //resets password after correct entry
  444. delay(600);
  445. lcd.setCursor(0,1);
  446. lcd.print("     Welcome     ");
  447.         digitalWrite(BLUEPIN,HIGH);
  448.         digitalWrite(BUZZER, HIGH);
  449.         doorLock.write(0); // releases the door, you need to adjust this to positioning the servo according your door locker
  450.         delay(5000); // wait 5 senconds
  451.         doorLock.write(120);
  452.         digitalWrite(BUZZER, LOW);
  453.         digitalWrite(BLUEPIN,LOW);
  454. delay(2000);
  455. lcd.clear();
  456. }
  457.  
  458.  
  459. else{
  460. lcd.print("INVALID PASSWORD   ");
  461. password.reset(); //resets password after INCORRECT entry
  462. delay(600);
  463.         digitalWrite(REDPIN,HIGH);
  464.         digitalWrite(BUZZER, HIGH);
  465.         delay(200);
  466.         digitalWrite(BUZZER, LOW);
  467.         delay(200);
  468.         digitalWrite(BUZZER, HIGH);
  469.         delay(200);
  470.         digitalWrite(BUZZER, LOW);
  471.         delay(200);
  472.         digitalWrite(BUZZER, HIGH);
  473.         delay(200);
  474.         digitalWrite(BUZZER, LOW);
  475.         digitalWrite(REDPIN,LOW);
  476.        
  477. lcd.clear();
  478. }
  479. }
Add Comment
Please, Sign In to add comment