Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /*
  2. * Arduino Door Lock Access Control Project
  3. *
  4. * by Dejan Nedelkovski, www.HowToMechatronics.com
  5. *
  6. * Library: MFRC522, https://github.com/miguelbalboa/rfid
  7. */
  8. #include <SPI.h>
  9. #include <MFRC522.h>
  10. #include <LiquidCrystal_I2C.h>
  11. #include <Servo.h>
  12. #define RST_PIN 9
  13. #define SS_PIN 10
  14. byte readCard[4];
  15. char* myTags[100] = {};
  16. int tagsCount = 0;
  17. String tagID = "";
  18. boolean successRead = false;
  19. boolean correctTag = false;
  20. int proximitySensor;
  21. boolean doorOpened = false;
  22. // Create instances
  23. MFRC522 mfrc522(SS_PIN, RST_PIN);
  24. LiquidCrystal_I2C lcd(0x3F,16,2);
  25. Servo myServo; // Servo motor
  26. void setup() {
  27. // Initiating
  28. SPI.begin(); // SPI bus
  29. mfrc522.PCD_Init(); // MFRC522
  30. lcd.init(); // LCD screen
  31. lcd.backlight();
  32. myServo.attach(8); // Servo motor
  33. myServo.write(10); // Initial lock position of the servo motor
  34. // Prints the initial message
  35. lcd.print(" -zadny CIPy-");
  36. lcd.setCursor(0, 1);
  37. lcd.print(" naskenuj ");
  38. // Waits until a master card is scanned
  39. while (!successRead) {
  40. successRead = getID();
  41. if ( successRead == true) {
  42. myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
  43. lcd.clear();
  44. lcd.setCursor(0, 0);
  45. lcd.print("VIP CIP nastaven");
  46. tagsCount++;
  47. }
  48. }
  49. successRead = false;
  50. printNormalModeMessage();
  51. }
  52. void loop() {
  53. int proximitySensor = analogRead(A0);
  54. // If door is closed...
  55. if (proximitySensor > 200) {
  56. if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  57. return;
  58. }
  59. if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  60. return;
  61. }
  62. tagID = "";
  63. // The MIFARE PICCs that we use have 4 byte UID
  64. for ( uint8_t i = 0; i < 4; i++) { //
  65. readCard[i] = mfrc522.uid.uidByte[i];
  66. tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  67. }
  68. tagID.toUpperCase();
  69. mfrc522.PICC_HaltA(); // Stop reading
  70. correctTag = false;
  71. // Checks whether the scanned tag is the master tag
  72. if (tagID == myTags[0]) {
  73. lcd.clear();
  74. lcd.print("Nastaveni:");
  75. lcd.setCursor(0, 1);
  76. lcd.print("Pridat/odebrat ");
  77. while (!successRead) {
  78. successRead = getID();
  79. if ( successRead == true) {
  80. for (int i = 0; i < 100; i++) {
  81. if (tagID == myTags[i]) {
  82. myTags[i] = "";
  83. lcd.clear();
  84. lcd.setCursor(0, 0);
  85. lcd.print(" Odebrano ");
  86. printNormalModeMessage();
  87. return;
  88. }
  89. }
  90. myTags[tagsCount] = strdup(tagID.c_str());
  91. lcd.clear();
  92. lcd.setCursor(0, 0);
  93. lcd.print(" Pridano ");
  94. printNormalModeMessage();
  95. tagsCount++;
  96. return;
  97. }
  98. }
  99. }
  100. successRead = false;
  101. // Checks whether the scanned tag is authorized
  102. for (int i = 0; i < 100; i++) {
  103. if (tagID == myTags[i]) {
  104. lcd.clear();
  105. lcd.setCursor(0, 0);
  106. lcd.print(" Vstup POVOLEN!");
  107. myServo.write(170); // Unlocks the door
  108. printNormalModeMessage();
  109. correctTag = true;
  110. }
  111. }
  112. if (correctTag == false) {
  113. lcd.clear();
  114. lcd.setCursor(0, 0);
  115. lcd.print("Vstup ZAKAZAN");
  116. printNormalModeMessage();
  117. }
  118. }
  119. // If door is open...
  120. else {
  121. lcd.clear();
  122. lcd.setCursor(0, 0);
  123. lcd.print("DVERE OTEVRENY");
  124. while (!doorOpened) {
  125. proximitySensor = analogRead(A0);
  126. if (proximitySensor > 200) {
  127. doorOpened = true;
  128. }
  129. }
  130. doorOpened = false;
  131. delay(500);
  132. myServo.write(10); // Locks the door
  133. printNormalModeMessage();
  134. }
  135. }
  136. uint8_t getID() {
  137. // Getting ready for Reading PICCs
  138. if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  139. return 0;
  140. }
  141. if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  142. return 0;
  143. }
  144. tagID = "";
  145. for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
  146. readCard[i] = mfrc522.uid.uidByte[i];
  147. tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  148. }
  149. tagID.toUpperCase();
  150. mfrc522.PICC_HaltA(); // Stop reading
  151. return 1;
  152. }
  153. void printNormalModeMessage() {
  154. delay(1500);
  155. lcd.clear();
  156. lcd.print("-Rizeni VSTUPU-");
  157. lcd.setCursor(0, 1);
  158. lcd.print(" Naskenuj CIP");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement