Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <SPI.h>//include the SPI bus library
- #include <MFRC522.h>//include the RFID reader library
- #define SS_PIN 10 //slave select pin
- #define RST_PIN A0 //reset pin
- MFRC522 mfrc522(SS_PIN, RST_PIN); // instatiate a MFRC522 reader object.
- MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information
- const byte numRows= 4; //number of rows on the keypad
- const byte numCols= 4; //number of columns on the keypad
- char keymap[numRows][numCols]=
- {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
- byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
- Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
- void setup()
- {
- Serial.begin(9600); // Initialize serial communications with the PC
- SPI.begin(); // Init SPI bus
- mfrc522.PCD_Init(); // Init MFRC522 card (in case you wonder what PCD means: proximity coupling device)
- Serial.println("Scan a MIFARE Classic card");
- for (byte i = 0; i < 6; i++) {
- key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
- }
- }
- 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.
- byte blockcontent[16] = {"The_Pink_Banker_"};//an array with 16 bytes to be written into one of the 64 card blocks is defined
- //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.
- 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.
- void loop()
- {
- char keypressed = myKeypad.getKey();
- if (keypressed != NO_KEY)
- {
- Serial.println(keypressed);
- }
- if ( ! mfrc522.PICC_IsNewCardPresent()) {//if PICC_IsNewCardPresent returns 1, a new card has been found and we continue
- return;//if it did not find a new card is returns a '0' and we return to the start of the loop
- }
- // Select one of the cards
- 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.
- return;//if it returns a '0' something went wrong and we return to the start of the loop
- }
- Serial.println("card selected");
- writeBlock(block, blockcontent);//the blockcontent array is written into the card block
- readBlock(1, readbackblock);//read the block back
- Serial.print("read block: ");
- for (int j=0 ; j<16 ; j++)//print the block contents
- {
- Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
- }
- mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
- Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement