Advertisement
gabbyshimoni

newPuzzleRFID

Mar 14th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. #define RST_PIN         9          // Configurable, see typical pin layout above
  5. #define SS_1_PIN        10         // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
  6. #define SS_2_PIN        8          // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
  7. #define SS_3_PIN        7
  8. #define SS_4_PIN        2
  9.  
  10. #define NR_OF_READERS   4
  11. String codeRead[4], lastCodes[4];
  12. String correctCodes[5] = {"abcd" , "efgh" , "ijkl" , "mnop" , "qrst"};
  13. byte ssPins[] = {SS_1_PIN, SS_2_PIN, SS_3_PIN, SS_4_PIN};
  14.  
  15. MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
  16. boolean wasChanged = false;
  17.  
  18.  
  19. /**
  20.    Initialize.
  21. */
  22. void setup() {
  23.  
  24.   Serial.begin(9600); // Initialize serial communications with the PC
  25.   while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  26.  
  27.   SPI.begin();        // Init SPI bus
  28.  
  29.   for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  30.     mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  31.     Serial.print(F("Reader "));
  32.     Serial.print(reader);
  33.     Serial.print(F(": "));
  34.     mfrc522[reader].PCD_DumpVersionToSerial();
  35.   }
  36. }
  37.  
  38. /**
  39.    Main loop.
  40. */
  41. void loop() {
  42.  
  43.   for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  44.     // Look for new cards
  45.  
  46.     if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  47.       Serial.print(F("Reader "));
  48.       Serial.print(reader);
  49.       // Show some details of the PICC (that is: the tag/card)
  50.       Serial.print(F(": Card UID:"));
  51.  
  52.       codeRead[reader] = "";
  53.       for (byte i = 0; i < mfrc522[reader].uid.size; i++) {
  54.         if (mfrc522[reader].uid.uidByte[i] < 10) codeRead[reader] += " 00" ;
  55.         else if (mfrc522[reader].uid.uidByte[i] < 100)  codeRead[reader] += " 0";
  56.         else codeRead[reader] += " ";
  57.  
  58.         codeRead[reader] += String(mfrc522[reader].uid.uidByte[i]);
  59.  
  60.       }
  61.  
  62.       Serial.println(reader);
  63.       Serial.println(codeRead[reader]);
  64.       if (lastCodes[reader] != codeRead[reader]) {
  65.         lastCodes[reader] = codeRead[reader];
  66.         wasChanged = true;
  67.       }
  68.  
  69.       // Halt PICC
  70.       mfrc522[reader].PICC_HaltA();
  71.       // Stop encryption on PCD
  72.       mfrc522[reader].PCD_StopCrypto1();
  73.     } //if (mfrc522[reader].PICC_IsNewC
  74.     delay(100);
  75.   } //for(uint8_t reader
  76.   int correct = 0;
  77.   if (wasChanged) {
  78.     for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  79.       if (lastCodes[reader] == correctCodes[reader]) correct++;
  80.     }
  81.     Serial.print("You have: ");
  82.     Serial.print(correct);
  83.     Serial.println( " correct tags");
  84.  
  85.     if (correct == 4) {
  86.       tone(6, 4000);
  87.       digitalWrite(13, HIGH);
  88.       delay(1000);
  89.       noTone(6);
  90.       scramble();
  91.     }
  92.   }
  93.   wasChanged = false;
  94.  
  95.  
  96. }
  97.  
  98. void scramble() {
  99.   String tmp;
  100.   for (int i = 0; i < 5; i++) {
  101.     tmp = correctCodes[i];
  102.     correctCodes[i] = correctCodes[i];
  103.     correctCodes[i + 1]  = tmp;
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement