Advertisement
a5768549

Untitled

Jun 1st, 2020
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. //RC522 1
  5. #define RST_PIN 22 // RST-PIN for RC522 - RFID - SPI - Module GPIO-0
  6. #define SS_PIN  21  // SDA-PIN for RC522 - RFID - SPI - Module GPIO-15
  7.  
  8. //RC522 2
  9. #define RST_PIN_2 17 // RST-PIN for RC522 - RFID - SPI - Module GPIO-0
  10. #define SS_PIN_2  16  // SDA-PIN for RC522 - RFID - SPI - Module GPIO-16
  11.  
  12. int tag[4];
  13.  
  14. MFRC522 rfid_1(SS_PIN, RST_PIN);   // Create MFRC522 instance
  15. MFRC522 rfid_2(SS_PIN_2, RST_PIN_2);   // Create MFRC522 instance
  16.  
  17.  
  18. void dump_byte_array(byte *buffer, byte bufferSize) {
  19.   for (byte i = 0; i < bufferSize; i++) {
  20.     Serial.print(buffer[i] < 0x10 ? "" : ".");
  21.     Serial.print(buffer[i], DEC);
  22.     tag[i] = buffer[i];
  23.   }
  24. }
  25.  
  26. void read_first_rfid()
  27. {
  28.   if ( ! rfid_1.PICC_ReadCardSerial()) {
  29.     delay(50);
  30.     return;
  31.   }
  32.   Serial.println("First RFID Tag Detected...");
  33.   Serial.print(F("Card UID:"));
  34.   dump_byte_array(rfid_1.uid.uidByte, rfid_1.uid.size);
  35.   Serial.println();
  36.  
  37.   delay(3000);
  38. }
  39.  
  40. void read_second_rfid()
  41. {
  42.   if ( ! rfid_2.PICC_ReadCardSerial()) {
  43.     delay(50);
  44.     return;
  45.   }
  46.   Serial.println("Second RFID Tag Detected...");
  47.   Serial.print(F("Card UID:"));
  48.   dump_byte_array(rfid_2.uid.uidByte, rfid_2.uid.size);
  49.   Serial.println();
  50.  
  51.   delay(3000);
  52. }
  53.  
  54. void detect_first_rfid()
  55. {
  56.   if ( ! rfid_1.PICC_IsNewCardPresent())
  57.   {
  58.     return;
  59.   }
  60.   read_first_rfid();
  61. }
  62.  
  63. void detect_second_rfid()
  64. {
  65.   if ( ! rfid_2.PICC_IsNewCardPresent())
  66.   {
  67.     return;
  68.   }
  69.   read_second_rfid();
  70. }
  71.  
  72.  
  73.  
  74. void setup()
  75. {
  76.   // Initialize serial communications
  77.   Serial.begin(9600);
  78.   delay(10);
  79.   SPI.begin();           // Init SPI bus
  80.   rfid_1.PCD_Init();    // Init MFRC522
  81.   rfid_2.PCD_Init();    // Init MFRC522
  82. }
  83.  
  84. void loop()
  85. {
  86.   detect_first_rfid();
  87.   detect_second_rfid();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement