document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM
  2. #include <SPI.h> // RC522 Module uses SPI protocol
  3. #include <MFRC522.h> // Library for Mifare RC522 Devices
  4.  
  5. /* Instead of a Relay maybe you want to use a servo
  6. * Servos can lock and unlock door locks too
  7. * There are examples out there.
  8. */
  9.  
  10. // #include <Servo.h>
  11.  
  12. /* For visualizing whats going on hardware
  13. * we need some leds and
  14. * to control door lock a relay and a wipe button
  15. * (or some other hardware)
  16. * Used common anode led,digitalWriting HIGH turns OFF led
  17. * Mind that if you are going to use common cathode led or
  18. * just seperate leds, simply comment out #define COMMON_ANODE,
  19. */
  20.  
  21. #define COMMON_ANODE
  22.  
  23. #ifdef COMMON_ANODE
  24. #define LED_ON LOW
  25. #define LED_OFF HIGH
  26. #else
  27. #define LED_ON HIGH
  28. #define LED_OFF LOW
  29. #endif
  30.  
  31. #define redLed 7
  32. #define greenLed 6
  33. #define blueLed 5
  34. #define relay 4
  35. #define wipeB 3 // Button pin for WipeMode
  36.  
  37. boolean match = false; // initialize card match to false
  38. boolean programMode = false; // initialize programming mode to false
  39.  
  40. int successRead; // Variable integer to keep if we have Successful Read from Reader
  41.  
  42. byte storedCard[4]; // Stores an ID read from EEPROM
  43. byte readCard[4]; // Stores scanned ID read from RFID Module
  44. byte masterCard[4]; // Stores master card's ID read from EEPROM
  45.  
  46. /* We need to define MFRC522's pins and create instance
  47. * Pin layout should be as follows (on Arduino Uno):
  48. * MOSI: Pin 11 / ICSP-4
  49. * MISO: Pin 12 / ICSP-1
  50. * SCK : Pin 13 / ICSP-3
  51. * SS : Pin 10 (Configurable)
  52. * RST : Pin 9 (Configurable)
  53. * look MFRC522 Library for
  54. * pin configuration for other Arduinos.
  55. */
  56.  
  57. #define SS_PIN 10
  58. #define RST_PIN 9
  59. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  60.  
  61. ///////////////////////////////////////// Setup ///////////////////////////////////
  62. void setup() {
  63. //Arduino Pin Configuration
  64. pinMode(redLed, OUTPUT);
  65. pinMode(greenLed, OUTPUT);
  66. pinMode(blueLed, OUTPUT);
  67. pinMode(relay, OUTPUT);
  68. digitalWrite(relay, HIGH); // Make sure door is locked
  69. digitalWrite(redLed, LED_OFF); // Make sure led is off
  70. digitalWrite(greenLed, LED_OFF); // Make sure led is off
  71. digitalWrite(blueLed, LED_OFF); // Make sure led is off
  72.  
  73. //Protocol Confciguration
  74. Serial.begin(9600); // Initialize serial communications with PC
  75. SPI.begin(); // MFRC522 Hardware uses SPI protocol
  76. mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
  77. mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); //Set Antenna Gain to Max- this will increase reading distance
  78.  
  79. //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  80. pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
  81. if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
  82. digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
  83. Serial.println("BOTTONE FORMATTAZIONE PREMUTO");
  84. Serial.println("HAI 5 SECONDI PER CANCELLARE");
  85. Serial.println("QUESTO RIMUOVERA' TUTTI I DATI, NON SI PUO' TORNARE INDIETRO");
  86. delay(5000); // Give user enough time to cancel operation
  87. if (digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
  88. Serial.println("AVVIATA PULIZIA EEPROM");
  89. for (int x=0; x<1024; x=x+1){ //Loop end of EEPROM address
  90. if (EEPROM.read(x) == 0){ //If EEPROM address 0
  91. // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
  92. }
  93. else{
  94. EEPROM.write(x, 0); // if not write 0, it takes 3.3mS
  95. }
  96. }
  97. Serial.println("CANCELLATO");
  98. digitalWrite(redLed, LED_OFF); // visualize successful wipe
  99. delay(200);
  100. digitalWrite(redLed, LED_ON);
  101. delay(200);
  102. digitalWrite(redLed, LED_OFF);
  103. delay(200);
  104. digitalWrite(redLed, LED_ON);
  105. delay(200);
  106. digitalWrite(redLed, LED_OFF);
  107. }
  108. else {
  109. Serial.println("!!! CANCELLAZIONE ANNULLATA !!!");
  110. digitalWrite(redLed, LED_OFF);
  111. }
  112. }
  113. //Check if master card defined, if not let user choose a master card
  114. //This also useful to just redefine Master Card
  115. //You can keep other EEPROM records just write other than 1 to EEPROM address 1
  116. if (EEPROM.read(1) != 143) { // Look EEPROM if Master Card defined, EEPROM address 1 holds if defined
  117. // 143 our magical number
  118. Serial.println("NESSUNA CARTA MASTER CONFIGURATA!");
  119. Serial.println("PASSA LA TESSERA PER DEFINIRE QUELLA MASTER");
  120. do {
  121. successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
  122. digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
  123. delay(200);
  124. digitalWrite(blueLed, LED_OFF);
  125. delay(200);
  126. }
  127. while (!successRead); //the program will not go further while you not get a successful read
  128. for ( int j = 0; j < 4; j++ ) { // Loop 4 times
  129. EEPROM.write( 2 +j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
  130. }
  131. EEPROM.write(1,143); //Write to EEPROM we defined Master Card.
  132. Serial.println("CARTA MASTER DEFINITA");
  133. }
  134. Serial.println("##### CONTROLLO INGRESSI PORTA V1.0 #####"); //For purposes
  135. Serial.println("CARTA MASTER UID");
  136. for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
  137. masterCard[i] = EEPROM.read(2+i); // Write it to masterCard
  138. Serial.print(masterCard[i], HEX);
  139. }
  140. Serial.println("");
  141. Serial.println("IN ATTESA DI UNA CARTA...");
  142. cycleLeds(); // Everything ready lets give user some feedback by cycling leds
  143. }
  144.  
  145.  
  146. ///////////////////////////////////////// Main Loop ///////////////////////////////////
  147. void loop () {
  148. do {
  149. successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
  150. if (programMode) {
  151. cycleLeds(); // Program Mode cycles through RGB waiting to read a new card
  152. }
  153. else {
  154. normalModeOn(); // Normal mode, blue Power LED is on, all others are off
  155. }
  156. }
  157. while (!successRead); //the program will not go further while you not get a successful read
  158. if (programMode) {
  159. if ( isMaster(readCard) ) { //If master card scanned again exit program mode
  160. Serial.println("Questa e la carta master!");
  161. Serial.println("Uscita dalla modalità programmazione...");
  162. Serial.println("-----------------------------");
  163. programMode = false;
  164. return;
  165. }
  166. else {
  167. if ( findID(readCard) ) { //If scanned card is known delete it
  168. Serial.println("La carta è stata rimossa dall' archivio...");
  169. deleteID(readCard);
  170. Serial.println("-----------------------------");
  171. }
  172. else { // If scanned card is not known add it
  173. Serial.println("La carta è stata aggiunta all' archivio...");
  174. writeID(readCard);
  175. Serial.println("-----------------------------");
  176. }
  177. }
  178. }
  179. else {
  180. if ( isMaster(readCard) ) { // If scanned card's ID matches Master Card's ID enter program mode
  181. programMode = true;
  182. Serial.println("Benvenuto Master - Entrato in modalità programmazione");
  183. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  184. Serial.print("I ho "); // stores the number of ID's in EEPROM
  185. Serial.print(count);
  186. Serial.print(" registrato su EEPROM");
  187. Serial.println("");
  188. Serial.println("scannerizzare carta per inserire o eliminare");
  189. Serial.println("-----------------------------");
  190. }
  191. else {
  192. if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
  193. Serial.println("Carta Riconosciuta, ACCESSO CONSENTITO...");
  194. openDoor(300); // Open the door lock for 300 ms
  195. }
  196. else { // If not, show that the ID was not valid
  197. Serial.println("Carta Non Riconosciuta, ACCESSO NEGATO...");
  198. failed();
  199. }
  200. }
  201. }
  202. }
  203.  
  204. ///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
  205. int getID() {
  206. // Getting ready for Reading PICCs
  207. if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  208. return 0;
  209. }
  210. if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  211. return 0;
  212. }
  213. // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  214. // I think we should assume every PICC as they have 4 byte UID
  215. // Until we support 7 byte PICCs
  216. Serial.println("SCANNERIZZA CARTA UID:");
  217. for (int i = 0; i < 4; i++) { //
  218. readCard[i] = mfrc522.uid.uidByte[i];
  219. Serial.print(readCard[i], HEX);
  220. }
  221. Serial.println("");
  222. mfrc522.PICC_HaltA(); // Stop reading
  223. return 1;
  224. }
  225.  
  226. ///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
  227. void cycleLeds() {
  228. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  229. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  230. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  231. delay(200);
  232. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  233. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  234. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  235. delay(200);
  236. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  237. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  238. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  239. delay(200);
  240. }
  241.  
  242. //////////////////////////////////////// Normal Mode Led ///////////////////////////////////
  243. void normalModeOn () {
  244. digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
  245. digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
  246. digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
  247. digitalWrite(relay, HIGH); // Make sure Door is Locked
  248. }
  249.  
  250. //////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
  251. void readID( int number ) {
  252. int start = (number * 4 ) + 2; // Figure out starting position
  253. for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
  254. storedCard[i] = EEPROM.read(start+i); // Assign values read from EEPROM to array
  255. }
  256. }
  257.  
  258. ///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
  259. void writeID( byte a[] ) {
  260. if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
  261. int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
  262. int start = ( num * 4 ) + 6; // Figure out where the next slot starts
  263. num++; // Increment the counter by one
  264. EEPROM.write( 0, num ); // Write the new count to the counter
  265. for ( int j = 0; j < 4; j++ ) { // Loop 4 times
  266. EEPROM.write( start+j, a[j] ); // Write the array values to EEPROM in the right position
  267. }
  268. successWrite();
  269. }
  270. else {
  271. failedWrite();
  272. }
  273. }
  274.  
  275. ///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
  276. void deleteID( byte a[] ) {
  277. if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
  278. failedWrite(); // If not
  279. }
  280. else {
  281. int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
  282. int slot; // Figure out the slot number of the card
  283. int start;// = ( num * 4 ) + 6; // Figure out where the next slot starts
  284. int looping; // The number of times the loop repeats
  285. int j;
  286. int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
  287. slot = findIDSLOT( a ); //Figure out the slot number of the card to delete
  288. start = (slot * 4) + 2;
  289. looping = ((num - slot) * 4);
  290. num--; // Decrement the counter by one
  291. EEPROM.write( 0, num ); // Write the new count to the counter
  292. for ( j = 0; j < looping; j++ ) { // Loop the card shift times
  293. EEPROM.write( start+j, EEPROM.read(start+4+j)); // Shift the array values to 4 places earlier in the EEPROM
  294. }
  295. for ( int k = 0; k < 4; k++ ) { //Shifting loop
  296. EEPROM.write( start+j+k, 0);
  297. }
  298. successDelete();
  299. }
  300. }
  301.  
  302. ///////////////////////////////////////// Check Bytes ///////////////////////////////////
  303. boolean checkTwo ( byte a[], byte b[] ) {
  304. if ( a[0] != NULL ) // Make sure there is something in the array first
  305. match = true; // Assume they match at first
  306. for ( int k = 0; k < 4; k++ ) { // Loop 4 times
  307. if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
  308. match = false;
  309. }
  310. if ( match ) { // Check to see if if match is still true
  311. return true; // Return true
  312. }
  313. else {
  314. return false; // Return false
  315. }
  316. }
  317.  
  318. ///////////////////////////////////////// Find Slot ///////////////////////////////////
  319. int findIDSLOT( byte find[] ) {
  320. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  321. for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
  322. readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
  323. if( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
  324. // is the same as the find[] ID card passed
  325. return i; // The slot number of the card
  326. break; // Stop looking we found it
  327. }
  328. }
  329. }
  330.  
  331. ///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
  332. boolean findID( byte find[] ) {
  333. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  334. for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
  335. readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
  336. if( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
  337. return true;
  338. break; // Stop looking we found it
  339. }
  340. else { // If not, return false
  341. }
  342. }
  343. return false;
  344. }
  345.  
  346. ///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
  347. // Flashes the green LED 3 times to indicate a successful write to EEPROM
  348. void successWrite() {
  349. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  350. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  351. digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
  352. delay(200);
  353. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  354. delay(200);
  355. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  356. delay(200);
  357. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  358. delay(200);
  359. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  360. delay(200);
  361. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  362. delay(200);
  363. Serial.println("SUCCESSO!!! CARTA INSERITA CORRETTAMENTE, NELL' ARCHIVIO!");
  364. }
  365.  
  366. ///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
  367. // Flashes the red LED 3 times to indicate a failed write to EEPROM
  368. void failedWrite() {
  369. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  370. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  371. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  372. delay(200);
  373. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  374. delay(200);
  375. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  376. delay(200);
  377. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  378. delay(200);
  379. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  380. delay(200);
  381. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  382. delay(200);
  383. Serial.println("Fallimento!!! Carta non inserita correttamente, prego riprovare!");
  384. }
  385.  
  386. ///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
  387. // Flashes the blue LED 3 times to indicate a success delete to EEPROM
  388. void successDelete() {
  389. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  390. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  391. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  392. delay(200);
  393. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  394. delay(200);
  395. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  396. delay(200);
  397. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  398. delay(200);
  399. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  400. delay(200);
  401. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  402. delay(200);
  403. Serial.println("Successo! carta aggiunta con successo!");
  404. }
  405.  
  406. ////////////////////// Check readCard IF is masterCard ///////////////////////////////////
  407. // Check to see if the ID passed is the master programing card
  408. boolean isMaster( byte test[] ) {
  409. if ( checkTwo( test, masterCard ) )
  410. return true;
  411. else
  412. return false;
  413. }
  414.  
  415. ///////////////////////////////////////// Unlock Door ///////////////////////////////////
  416. void openDoor( int setDelay ) {
  417. digitalWrite(blueLed, LED_OFF); // Turn off blue LED
  418. digitalWrite(redLed, LED_OFF); // Turn off red LED
  419. digitalWrite(greenLed, LED_ON); // Turn on green LED
  420. digitalWrite(relay, LOW); // Unlock door!
  421. delay(setDelay); // Hold door lock open for given seconds
  422. digitalWrite(relay, HIGH); // Relock door
  423. delay(2000); // Hold green LED on for 2 more seconds
  424. }
  425.  
  426. ///////////////////////////////////////// Failed Access ///////////////////////////////////
  427. void failed() {
  428. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  429. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  430. digitalWrite(redLed, LED_ON); // Turn on red LED
  431. delay(1200);
  432. }
');