Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <SPI.h>//include the SPI bus library
  3. #include <MFRC522.h>//include the RFID reader library
  4.  
  5. #define SS_PIN 10 //slave select pin
  6. #define RST_PIN A0 //reset pin
  7. MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
  8. MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information
  9. const byte numRows= 4; //number of rows on the keypad
  10. const byte numCols= 4; //number of columns on the keypad
  11. char keymap[numRows][numCols]=
  12. {
  13. {'1', '2', '3', 'A'},
  14. {'4', '5', '6', 'B'},
  15. {'7', '8', '9', 'C'},
  16. {'*', '0', '#', 'D'}
  17. };
  18. byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
  19. byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
  20. Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
  21. void setup()
  22. {
  23. Serial.begin(9600); // Initialize serial communications with the PC
  24. SPI.begin(); // Init SPI bus
  25. mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
  26. Serial.println("Scan a MIFARE Classic card");
  27. for (byte i = 0; i < 6; i++) {
  28. key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  29. }
  30. }
  31. int block=1;//this is the block number we will write into and then read. Do not write into 'sector trailer' block, since this can make the block unusable.
  32. byte blockcontent[16] = {"The_Pink_Banker_"};//an array with 16 bytes to be written into one of the 64 card blocks is defined
  33. //byte blockcontent[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//all zeros. This can be used to delete a block.
  34. byte readbackblock[18]; //This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.
  35. void loop()
  36. {
  37. char keypressed = myKeypad.getKey();
  38. if (keypressed != NO_KEY)
  39. {
  40. Serial.println(keypressed);
  41. }
  42. if ( ! mfrc522.PICC_IsNewCardPresent()) {//if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
  43. return;//if it did not find a new card is returns a '0' and we return to the start of the loop
  44. }
  45.  
  46. // Select one of the cards
  47. if ( ! mfrc522.PICC_ReadCardSerial()) {//if PICC_ReadCardSerial returns 1, the "uid" struct (see MFRC522.h lines 238-45)) contains the ID of the read card.
  48. return;//if it returns a '0' something went wrong and we return to the start of the loop
  49. }
  50. Serial.println("card selected");
  51. writeBlock(block, blockcontent);//the blockcontent array is written into the card block
  52. readBlock(1, readbackblock);//read the block back
  53. Serial.print("read block: ");
  54. for (int j=0 ; j<16 ; j++)//print the block contents
  55. {
  56. Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
  57. }
  58. mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  59. Serial.println("");
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement