/* * -------------------------------------------------------------------------------------------------------------------- * Example sketch/program showing how to read new NUID from a PICC to serial. * -------------------------------------------------------------------------------------------------------------------- * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid * * Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID * Reader on the Arduino SPI interface. * * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages * when removing the PICC from reading distance too early. * * @license Released into the public domain. * * Typical pin layout used: * ----------------------------------------------------------------------------------------- * MFRC522 Arduino Arduino Arduino Arduino Arduino * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro * Signal Pin Pin Pin Pin Pin Pin * ----------------------------------------------------------------------------------------- * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST * SPI SS SDA(SS) 10 53 D10 10 10 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 */ #include #include #include "TM1637.h" #define CLK 3 #define DIO 2 constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class TM1637 tm1637(CLK, DIO); MFRC522::MIFARE_Key key; MFRC522::StatusCode status; const byte dev = 3; const byte sector = 1; const byte blockAddr = 4; const byte trailerBlock = 7; byte _prevDev = 0; byte _curStep = 0; unsigned long cards[165][2] = { { 1236741907,1}, { 3916372499,1}, { 208736790, 1}, { 3970631190,1}, { 4234174756,1}, { 2088125196,1}, { 3425763350,1}, { 1495542547,1}, { 2579902995,1}, { 2457449185,1}, { 214745356, 1}, { 4231970060,1}, { 2895567116,1}, { 2995753444,1}, { 2093565215,1}, { 2362979084,1}, { 2184899809,1}, { 3525043681,1}, { 3163683363,1}, { 3917879059,1}, { 3703056396,1}, { 2617834275,1}, { 2033137683,1}, { 3706937380,1}, { 1645875685,1}, { 3110942739,1}, { 700205364, 1}, { 2899655460,1}, { 3693873421,1}, { 2361931532,1}, { 1085975638,1 }, //test { 2307138579,2}, { 4180094995,2}, { 1283588886,2}, { 844844746, 2}, { 2081313316,2}, { 1278088204,2}, { 3436117525,2}, { 741019158, 2}, { 965286419, 2}, { 2351443748,2}, { 1500065555,2}, { 3702184204,2}, { 2723781860,2}, { 3531866853,2}, { 469838092, 2}, { 1280638731,2}, { 577756130, 2}, { 2083792420,2}, { 2623954463,2}, { 3375637779,2}, { 1022239244,2}, { 3702983460,2}, { 967255059, 2}, { 1821387807,2}, { 1823814436,2}, { 159134483, 2}, { 4240220708,2}, { 3805198307,2}, { 4073903844,2}, { 3700048396,2}, { 1347071062,2}, //test { 2307860755,3}, { 3118340883,3}, { 741803286, 3}, { 3806285770,3}, { 1817775628,3}, { 754529567, 3}, { 1552092438,3}, { 3423144214,3}, { 2032083219,3}, { 739910180, 3}, { 3970429452,3}, { 4243660557,3}, { 48966117, 3}, { 3803484388,3}, { 2363165196,3}, { 2632271884,3}, { 1818316575,3}, { 2451042532,3}, { 40964323, 3}, { 965418771, 3}, { 1112864227,3}, { 2354471204,3}, { 1500190483,3}, { 3525591971,3}, { 3966893835,3}, { 3914083091,3}, { 3434207011,3}, { 3428177955,3}, { 2091731748,3}, { 4232603172,3}, { 4039358294,3}, //test { 1498428435,4}, { 2311006739,4}, { 473525264, 4}, { 1011285781,4}, { 2622301452,4}, { 2361794828,4}, { 3965454358,4}, { 1276931600,4}, { 964958995, 4}, { 3430925836,4}, { 1823194124,4}, { 4243701516,4}, { 2454160612,4}, { 1655063525,4}, { 2890285324,4}, { 2364745740,4}, { 3167363340,4}, { 1558757644,4}, { 2193480674,4}, { 1773218579,4}, { 2630225676,4}, { 3703740196,4}, { 2846110483,4}, { 845306948, 4}, { 1546019364,4}, { 1774394387,4}, { 1276798756,4}, { 2620988964,4}, { 3424090916,4}, { 1547183652,4}, { 9935454, 4}, //test { 480474929, 5}, { 2352379953,5}, { 2890561585,5}, { 1290958897,5}, { 1558083633,5}, { 1015314737,5}, { 478705969, 5}, { 479885617, 5}, { 3694098993,5}, { 205748785, 5}, { 479164209, 5}, { 3423696689,5}, { 469792561, 5}, { 3966793265,5}, { 208500273, 5}, { 744388145, 5}, { 3167450417,5}, { 2092266801,5}, { 4240930097,5}, { 1280865841,5}, { 1012692529,5}, { 745502257, 5}, { 738359089, 5}, { 3692197681,5}, { 747730737, 5}, { 749041457, 5}, { 2084075569,5}, { 3427432497,5}, { 2622388273,5}, { 1826519089,5}, { 3433397297,5}, { 3970268209,5}, { 2887089201,5}, { 3959913521,5}, { 1006795825,5}, { 1551137585,5}, { 3160832817,5}, { 485194289, 5}, { 1290238513,5}, { 2631301681,5}, { 2094543628,5} }; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } Serial.println(F("This code scan the MIFARE Classsic NUID.")); Serial.print(F("Using the following key:")); printHex(key.keyByte, MFRC522::MF_KEY_SIZE); Serial.println(); tm1637.init(); tm1637.set(BRIGHT_TYPICAL); } void loop() { // Look for new cards if ( ! rfid.PICC_IsNewCardPresent()) return; // Verify if the NUID has been readed if ( ! rfid.PICC_ReadCardSerial()) return; MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } unsigned long UID = ((unsigned long)rfid.uid.uidByte[0] << 24) +((unsigned long)rfid.uid.uidByte[1] << 16)+((unsigned long)rfid.uid.uidByte[2] << 8)+((unsigned long)rfid.uid.uidByte[3]); Serial.print(F("uid=")); Serial.println(UID); if (auth()) if(read()) processStateMachine(UID); //write(0,0); // Halt PICC rfid.PICC_HaltA(); // Stop encryption on PCD rfid.PCD_StopCrypto1(); } void correct(int8_t lb, bool repeat) { if (!repeat){ if (!write(dev, _curStep+1)){ return; } } Serial.print(F("displaying:")); Serial.println(lb); int8_t DispMSG[] = {18, 11, 00, 00}; if (lb>=0){ tm1637.point(true); if (lb<10) { DispMSG[2]=0; DispMSG[3]=lb; }else{ DispMSG[2]=lb/10; DispMSG[3]=lb%10; } }else { tm1637.point(false); DispMSG[0]=14; DispMSG[1]=21; DispMSG[2]=13; DispMSG[3]=17; } tm1637.display(DispMSG); delay(1400); tm1637.point(false); tm1637.clearDisplay(); } void error() { if (write(0, 0)){ Serial.print(F("displaying eror")); int8_t DispMSG[] = {14, 20, 0, 20}; tm1637.point(false); tm1637.display(DispMSG); delay(1400); tm1637.clearDisplay(); } } void bonus(byte dev) { int8_t DispMSG[4]; if (dev == 1){ DispMSG[0]=11; DispMSG[1]=14; DispMSG[2]=5; DispMSG[3]=19; } else if (dev == 2){ DispMSG[0]=15; DispMSG[1]=20; DispMSG[2]=1; DispMSG[3]=17; } else if (dev == 3){ DispMSG[0]=14; DispMSG[1]=21; DispMSG[2]=13; DispMSG[3]=17; } tm1637.point(false); tm1637.display(DispMSG); delay(1400); tm1637.clearDisplay(); } byte getCardType (unsigned long uid) { for (byte i=0; i<165;++i){ if (uid == cards[i][0]) return cards[i][1]; } return -1; } void processStateMachine(unsigned long uid) { byte card = getCardType(uid); Serial.print(F("card type:")); Serial.println(card); if (dev == 1) { bool repeat = false; if (_prevDev == 1) { _curStep--; repeat = true;} if (card == 1) { if (_curStep == 0) correct(2,repeat); else if (_curStep == 3) correct(3,repeat); else if (_curStep == 6) correct(22,repeat); else if (_curStep == 8) correct(29,repeat); else if (_curStep == 11) correct(17,repeat); else if (_curStep == 14) correct(-1,repeat); else error(); } else if (card == 2) { if (_curStep == 2) correct(17,repeat); else if (_curStep == 4) correct(27,repeat); else if (_curStep == 6) correct(36,repeat); else if (_curStep == 8) correct(7,repeat); else if (_curStep == 11) correct(19,repeat); else error(); } else if (card == 3) { if (_curStep == 5) correct(18,repeat); else if (_curStep == 7) correct(33,repeat); else if (_curStep == 9) correct(5,repeat); else if (_curStep == 11) correct(16,repeat); else if (_curStep == 13) correct(37,repeat); else error(); } else if (card == 4) { if (_curStep == 1) correct(10,repeat); else if (_curStep == 6) correct(5,repeat); else if (_curStep == 8) correct(21,repeat); else if (_curStep == 10) correct(39,repeat); else if (_curStep == 13) correct(-1,repeat); else error(); } else if (card == 5) { bonus(1); } } else if (dev == 2) { bool repeat = false; if (_prevDev == 2) { _curStep--; repeat = true;} if (card == 1) { if (_curStep == 1) correct(34,repeat); else if (_curStep == 5) correct(14,repeat); else if (_curStep == 10) correct(31,repeat); else if (_curStep == 12) correct(24,repeat); else error(); } else if (card == 2) { if (_curStep == 1) correct(1,repeat); else if (_curStep == 3) correct(9,repeat); else if (_curStep == 7) correct(12,repeat); else if (_curStep == 9) correct(40,repeat); else if (_curStep == 12) correct(-1,repeat); else error(); } else if (card == 3) { if (_curStep == 1) correct(39,repeat); else if (_curStep == 3) correct(22,repeat); else if (_curStep == 10) correct(26,repeat); else if (_curStep == 14) correct(3,repeat); else error(); } else if (card == 4) { if (_curStep == 0) correct(13,repeat); else if (_curStep == 2) correct(24,repeat); else if (_curStep == 4) correct(28,repeat); else if (_curStep == 7) correct(31,repeat); else if (_curStep == 9) correct(9,repeat); else if (_curStep == 12) correct(32,repeat); else error(); } else if (card == 5) { bonus(2); } } else if (dev == 3) { bool repeat = false; if (_prevDev == 3) { _curStep--; repeat = true;} if (card == 1) { if (_curStep == 2) correct(35,repeat); else if (_curStep == 4) correct(8,repeat); else if (_curStep == 7) correct(11,repeat); else if (_curStep == 9) correct(6,repeat); else if (_curStep == 13) correct(30,repeat); else error(); } else if (card == 2) { if (_curStep == 0) correct(23,repeat); else if (_curStep == 5) correct(14,repeat); else if (_curStep == 10) correct(11,repeat); else error(); } else if (card == 3) { if (_curStep == 0) correct(4,repeat); else if (_curStep == 2) correct(21,repeat); else if (_curStep == 4) correct(25,repeat); else if (_curStep == 6) correct(20,repeat); else if (_curStep == 8) correct(32,repeat); else if (_curStep == 12) correct(35,repeat); else if (_curStep == 15) correct(-1,repeat); else error(); } else if (card == 4) { if (_curStep == 3) correct(2,repeat); else if (_curStep == 5) correct(38,repeat); else if (_curStep == 11) correct(15,repeat); else error(); } else if (card == 5) { bonus(3); } } } byte auth() { // Authenticate using key A Serial.println(F("Authenticating using key A...")); status = (MFRC522::StatusCode) rfid.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(rfid.uid)); if (status != MFRC522::STATUS_OK) { Serial.print(F("PCD_Authenticate() failed: ")); Serial.println(rfid.GetStatusCodeName(status)); return 0; }else{ Serial.print(F("PCD_Authenticate() Success: ")); Serial.println(rfid.GetStatusCodeName(status)); return 1; } } byte read() { byte buffer[18]; byte size = sizeof(buffer); // Read data from the block Serial.print(F("Reading data from block ")); Serial.print(blockAddr); Serial.println(F(" ...")); status = (MFRC522::StatusCode) rfid.MIFARE_Read(blockAddr, buffer, &size); if (status != MFRC522::STATUS_OK) { Serial.print(F("MIFARE_Read() failed: ")); Serial.println(rfid.GetStatusCodeName(status)); return 0; } Serial.print(F("Data in block ")); Serial.print(blockAddr); Serial.println(F(":")); dump_byte_array(buffer, 16); Serial.println(); Serial.println(); _prevDev = buffer[0]; _curStep = buffer[1]; return 1; } byte write(byte ddev, byte step) { byte dataBlock[] = {ddev, step,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; Serial.print(F("Writing data into block ")); Serial.print(blockAddr); Serial.println(F(" ...")); dump_byte_array(dataBlock, 16); Serial.println(); status = (MFRC522::StatusCode) rfid.MIFARE_Write(blockAddr, dataBlock, 16); if (status != MFRC522::STATUS_OK) { Serial.print(F("MIFARE_Write() failed: ")); Serial.println(rfid.GetStatusCodeName(status)); return 0; } Serial.println(); return 1; } void printHex(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } void dump_byte_array(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } }