Advertisement
computermuseo

Rfid carico credito Arcade by Computermuseo

Dec 30th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.42 KB | None | 0 0
  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. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7. #define COMMON_ANODE
  8. #ifdef COMMON_ANODE
  9. #define LED_ON LOW
  10. #define LED_OFF HIGH
  11. #else
  12. #define LED_ON HIGH
  13. #define LED_OFF LOW
  14. #endif
  15. #define redLed 7 // Set Led Pins
  16. #define greenLed 6
  17. #define blueLed 5
  18. #define relay 4 // Set Relay Pin
  19. #define wipeB 3 // Button pin for WipeMode
  20. boolean match = false; // initialize card match to false
  21. boolean programMode = false; // initialize programming mode to false
  22. boolean replaceMaster = false;
  23. int successRead; // Variable integer to keep if we have Successful Read from Reader
  24. byte storedCard[4]; // Stores an ID read from EEPROM
  25. byte readCard[4]; // Stores scanned ID read from RFID Module
  26. byte masterCard[4]; // Stores master card's ID read from EEPROM
  27.  
  28.  
  29. #define SS_PIN 10
  30. #define RST_PIN 9
  31. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  32.  
  33. ///////////////////////////////////////// Setup ///////////////////////////////////
  34. void setup() {
  35. {
  36. lcd.begin();
  37. lcd.backlight();
  38. }
  39.  
  40. pinMode(redLed, OUTPUT);
  41. pinMode(greenLed, OUTPUT);
  42. pinMode(blueLed, OUTPUT);
  43. pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
  44. pinMode(relay, OUTPUT);
  45. //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
  46. digitalWrite(relay, HIGH); // Make sure door is locked
  47. digitalWrite(redLed, LED_OFF); // Make sure led is off
  48. digitalWrite(greenLed, LED_OFF); // Make sure led is off
  49. digitalWrite(blueLed, LED_OFF); // Make sure led is off
  50.  
  51. //Protocol Configuration
  52. Serial.begin(9600); // Initialize serial communications with PC
  53. SPI.begin(); // MFRC522 Hardware uses SPI protocol
  54. mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
  55.  
  56. //If you set Antenna Gain to Max it will increase reading distance
  57. //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
  58.  
  59. lcd.print(F("NFC attivo"));
  60. lcd.clear();// For debugging purposes
  61. ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
  62.  
  63. //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  64. if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
  65. digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
  66. lcd.print(F("reset premuto"));
  67. lcd.setCursor(0,1);
  68. lcd.print(F("15 secondi"));
  69. delay(15000);
  70. lcd.clear();
  71. if (digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
  72. lcd.print(F("Pulizia EEPROM"));
  73. for (int x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
  74. if (EEPROM.read(x) == 0) { //If EEPROM address 0
  75. // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
  76. }
  77. else {
  78. EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
  79. }
  80. }
  81. lcd.print(F("EEPROM CANCELLATA!"));
  82. digitalWrite(redLed, LED_OFF); // visualize successful wipe
  83. delay(200);
  84. digitalWrite(redLed, LED_ON);
  85. delay(200);
  86. digitalWrite(redLed, LED_OFF);
  87. delay(200);
  88. digitalWrite(redLed, LED_ON);
  89. delay(200);
  90. digitalWrite(redLed, LED_OFF);
  91. lcd.clear();
  92. }
  93. else {
  94. lcd.print(F("WIPE CANCELLATO"));
  95. digitalWrite(redLed, LED_OFF);
  96. lcd.clear();
  97. }
  98. }
  99. // Check if master card defined, if not let user choose a master card
  100. // This also useful to just redefine Master Card
  101. // You can keep other EEPROM records just write other than 143 to EEPROM address 1
  102. // EEPROM address 1 should hold magical number which is '143'
  103. if (EEPROM.read(1) != 143) {
  104. lcd.print(F("CONFIGURAZIONE "));
  105. lcd.setCursor(1,1);
  106. lcd.print(F("ADMIN CARD"));
  107. do {
  108. successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
  109. digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
  110. delay(200);
  111. digitalWrite(blueLed, LED_OFF);
  112. delay(200);
  113. lcd.clear();
  114. }
  115. while (!successRead); // Program will not go further while you not get a successful read
  116. for ( int j = 0; j < 4; j++ ) { // Loop 4 times
  117. EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
  118. }
  119. EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
  120. lcd.print(F("CARD ADMIN"));
  121. lcd.setCursor(0,1);
  122. lcd.print(F("CONFERMATA"));
  123. lcd.clear();
  124. }
  125. lcd.print(F("-------------------"));
  126. lcd.setCursor(1,0);
  127. lcd.print(F("UID CARD ADMIN"));
  128. for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
  129. masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
  130. lcd.print(masterCard[i], HEX);
  131. lcd.clear();
  132. }
  133. lcd.print(F("AVVICINARE"));
  134. lcd.setCursor(0,1);
  135. lcd.print(F("LA CARD"));
  136. cycleLeds(); // Everything ready lets give user some feedback by cycling leds
  137. }
  138.  
  139.  
  140. ///////////////////////////////////////// Main Loop ///////////////////////////////////
  141. void loop () {
  142. do {
  143. successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
  144. if (digitalRead(wipeB) == LOW) {
  145. digitalWrite(redLed, LED_ON); // Make sure led is off
  146. digitalWrite(greenLed, LED_OFF); // Make sure led is off
  147. digitalWrite(blueLed, LED_OFF); // Make sure led is off
  148. lcd.print(F("avviata procedura"));
  149. lcd.setCursor(0,1);
  150. lcd.print(F("reset!"));
  151. delay(10000);
  152. if (digitalRead(wipeB) == LOW) {
  153. EEPROM.write(1, 0); // Reset Magic Number.
  154. lcd.print(F("CARD CANCELLATA"));
  155. lcd.clear();
  156. while (1);
  157. }
  158. }
  159. if (programMode) {
  160. cycleLeds(); // Program Mode cycles through RGB waiting to read a new card
  161. }
  162. else {
  163. normalModeOn(); // Normal mode, blue Power LED is on, all others are off
  164. }
  165. }
  166. while (!successRead); //the program will not go further while you not get a successful read
  167. if (programMode) {
  168. if ( isMaster(readCard) ) { //If master card scanned again exit program mode
  169. lcd.print(F("ADMIN CARD"));
  170. lcd.setCursor(0,1);
  171. lcd.print(F("uscita programmazione..."));
  172. delay(2000);
  173. lcd.clear();
  174. programMode = false;
  175. return;
  176. }
  177. else {
  178. if ( findID(readCard) ) { // If scanned card is known delete it
  179. lcd.print(F("RIMOSSO L'ID"));
  180. deleteID(readCard);
  181. lcd.print(F("ins/rim della card"));
  182. }
  183. else { // If scanned card is not known add it
  184. lcd.print(F("ERR SCAN ID"));
  185. writeID(readCard);
  186. lcd.print(F("RIPROVA"));
  187. delay(2000);
  188. lcd.clear();
  189. }
  190. }
  191. }
  192. else {
  193. if ( isMaster(readCard)) { // If scanned card's ID matches Master Card's ID enter program mode
  194. programMode = true;
  195. lcd.println(F("PROGRAMMAZIONE"));
  196. lcd.setCursor(0,1);
  197. lcd.println(F("ATTIVATA!"));
  198. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  199. lcd.print(count);
  200. lcd.print(F("ID REGISTRATO"));
  201. delay(2000);
  202. lcd.clear();
  203. lcd.println(F("SCAN ID NEW"));
  204. }
  205. else {
  206. if ( findID(readCard) ) { // If not, see if the card is in the EEPROM
  207. lcd.print(F("CREDITO INSERITO"));
  208. lcd.setCursor(0,1);
  209. lcd.print(F("NELL' ARCADE"));
  210. granted(300); // Open the door lock for 300 ms
  211. delay(3000);
  212. lcd.clear();
  213. }
  214. else { // If not, show that the ID was not valid
  215. Serial.println(F("CARD NON VALIDA"));
  216. denied();
  217. }
  218. }
  219. }
  220. }
  221.  
  222. ///////////////////////////////////////// Access Granted ///////////////////////////////////
  223. void granted (int setDelay) {
  224. digitalWrite(blueLed, LED_OFF); // Turn off blue LED
  225. digitalWrite(redLed, LED_OFF); // Turn off red LED
  226. digitalWrite(greenLed, LED_ON); // Turn on green LED
  227. digitalWrite(relay, LOW); // Unlock door!
  228. delay(setDelay); // Hold door lock open for given seconds
  229. digitalWrite(relay, HIGH); // Relock door
  230. delay(1000); // Hold green LED on for a second
  231. }
  232.  
  233. ///////////////////////////////////////// Access Denied ///////////////////////////////////
  234. void denied() {
  235. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  236. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  237. digitalWrite(redLed, LED_ON); // Turn on red LED
  238. delay(1000);
  239. }
  240.  
  241.  
  242. ///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
  243. int getID() {
  244. // Getting ready for Reading PICCs
  245. if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  246. return 0;
  247. }
  248. if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  249. return 0;
  250. }
  251. // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  252. // I think we should assume every PICC as they have 4 byte UID
  253. // Until we support 7 byte PICCs
  254. Serial.println(F("Scanned PICC's UID:"));
  255. for (int i = 0; i < 4; i++) { //
  256. readCard[i] = mfrc522.uid.uidByte[i];
  257. Serial.print(readCard[i], HEX);
  258. }
  259. Serial.println("");
  260. mfrc522.PICC_HaltA(); // Stop reading
  261. return 1;
  262. }
  263.  
  264. void ShowReaderDetails() {
  265. // Get the MFRC522 software version
  266. byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  267. Serial.print(F("MFRC522 Software Version: 0x"));
  268. Serial.print(v, HEX);
  269. if (v == 0x91)
  270. Serial.print(F(" = v1.0"));
  271. else if (v == 0x92)
  272. Serial.print(F(" = v2.0"));
  273. else
  274. Serial.print(F(" (unknown),probably a chinese clone?"));
  275. Serial.println("");
  276. // When 0x00 or 0xFF is returned, communication probably failed
  277. if ((v == 0x00) || (v == 0xFF)) {
  278. Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
  279. Serial.println(F("SYSTEM HALTED: Check connections."));
  280. while (true); // do not go further
  281. }
  282. }
  283.  
  284. ///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
  285. void cycleLeds() {
  286. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  287. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  288. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  289. delay(200);
  290. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  291. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  292. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  293. delay(200);
  294. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  295. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  296. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  297. delay(200);
  298. }
  299.  
  300. //////////////////////////////////////// Normal Mode Led ///////////////////////////////////
  301. void normalModeOn () {
  302. digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
  303. digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
  304. digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
  305. digitalWrite(relay, HIGH); // Make sure Door is Locked
  306. }
  307.  
  308. //////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
  309. void readID( int number ) {
  310. int start = (number * 4 ) + 2; // Figure out starting position
  311. for ( int i = 0; i < 4; i++ ) { // Loop 4 times to get the 4 Bytes
  312. storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
  313. }
  314. }
  315.  
  316. ///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
  317. void writeID( byte a[] ) {
  318. if ( !findID( a ) ) { // Before we write to the EEPROM, check to see if we have seen this card before!
  319. int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
  320. int start = ( num * 4 ) + 6; // Figure out where the next slot starts
  321. num++; // Increment the counter by one
  322. EEPROM.write( 0, num ); // Write the new count to the counter
  323. for ( int j = 0; j < 4; j++ ) { // Loop 4 times
  324. EEPROM.write( start + j, a[j] ); // Write the array values to EEPROM in the right position
  325. }
  326. successWrite();
  327. Serial.println(F("INSERITO CON SUCCESSO NELLA EEPROM"));
  328. }
  329. else {
  330. failedWrite();
  331. Serial.println(F("ATTENZIONE! ID NON LETTO CORRETTAMENTE"));
  332. }
  333. }
  334.  
  335. ///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
  336. void deleteID( byte a[] ) {
  337. if ( !findID( a ) ) { // Before we delete from the EEPROM, check to see if we have this card!
  338. failedWrite(); // If not
  339. Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
  340. }
  341. else {
  342. int num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
  343. int slot; // Figure out the slot number of the card
  344. int start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
  345. int looping; // The number of times the loop repeats
  346. int j;
  347. int count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
  348. slot = findIDSLOT( a ); // Figure out the slot number of the card to delete
  349. start = (slot * 4) + 2;
  350. looping = ((num - slot) * 4);
  351. num--; // Decrement the counter by one
  352. EEPROM.write( 0, num ); // Write the new count to the counter
  353. for ( j = 0; j < looping; j++ ) { // Loop the card shift times
  354. EEPROM.write( start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
  355. }
  356. for ( int k = 0; k < 4; k++ ) { // Shifting loop
  357. EEPROM.write( start + j + k, 0);
  358. }
  359. successDelete();
  360. Serial.println(F("RIMOSSO CON SUCCESSO DALLA EEPROM"));
  361. }
  362. }
  363.  
  364. ///////////////////////////////////////// Check Bytes ///////////////////////////////////
  365. boolean checkTwo ( byte a[], byte b[] ) {
  366. if ( a[0] != NULL ) // Make sure there is something in the array first
  367. match = true; // Assume they match at first
  368. for ( int k = 0; k < 4; k++ ) { // Loop 4 times
  369. if ( a[k] != b[k] ) // IF a != b then set match = false, one fails, all fail
  370. match = false;
  371. }
  372. if ( match ) { // Check to see if if match is still true
  373. return true; // Return true
  374. }
  375. else {
  376. return false; // Return false
  377. }
  378. }
  379.  
  380. ///////////////////////////////////////// Find Slot ///////////////////////////////////
  381. int findIDSLOT( byte find[] ) {
  382. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  383. for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
  384. readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
  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. return i; // The slot number of the card
  388. break; // Stop looking we found it
  389. }
  390. }
  391. }
  392.  
  393. ///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
  394. boolean findID( byte find[] ) {
  395. int count = EEPROM.read(0); // Read the first Byte of EEPROM that
  396. for ( int i = 1; i <= count; i++ ) { // Loop once for each EEPROM entry
  397. readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
  398. if ( checkTwo( find, storedCard ) ) { // Check to see if the storedCard read from EEPROM
  399. return true;
  400. break; // Stop looking we found it
  401. }
  402. else { // If not, return false
  403. }
  404. }
  405. return false;
  406. }
  407.  
  408. ///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
  409. // Flashes the green LED 3 times to indicate a successful write to EEPROM
  410. void successWrite() {
  411. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  412. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  413. digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
  414. delay(200);
  415. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  416. delay(200);
  417. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  418. delay(200);
  419. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  420. delay(200);
  421. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  422. delay(200);
  423. digitalWrite(greenLed, LED_ON); // Make sure green LED is on
  424. delay(200);
  425. }
  426.  
  427. ///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
  428. // Flashes the red LED 3 times to indicate a failed write to EEPROM
  429. void failedWrite() {
  430. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  431. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  432. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  433. delay(200);
  434. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  435. delay(200);
  436. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  437. delay(200);
  438. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  439. delay(200);
  440. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  441. delay(200);
  442. digitalWrite(redLed, LED_ON); // Make sure red LED is on
  443. delay(200);
  444. }
  445.  
  446. ///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
  447. // Flashes the blue LED 3 times to indicate a success delete to EEPROM
  448. void successDelete() {
  449. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  450. digitalWrite(redLed, LED_OFF); // Make sure red LED is off
  451. digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
  452. delay(200);
  453. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  454. delay(200);
  455. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  456. delay(200);
  457. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  458. delay(200);
  459. digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
  460. delay(200);
  461. digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
  462. delay(200);
  463. }
  464.  
  465. ////////////////////// Check readCard IF is masterCard ///////////////////////////////////
  466. // Check to see if the ID passed is the master programing card
  467. boolean isMaster( byte test[] ) {
  468. if ( checkTwo( test, masterCard ) )
  469. return true;
  470. else
  471. return false;
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement