Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.   --------------------------------------------------------------------------------------------------------------------
  3.   Example sketch/program showing how to read data from more than one PICC to serial.
  4.   --------------------------------------------------------------------------------------------------------------------
  5.   This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6.  
  7.   Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
  8.   MFRC522 based RFID Reader on the Arduino SPI interface.
  9.  
  10.   Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
  11.            and knowledge are required!
  12.  
  13.   @license Released into the public domain.
  14.  
  15.   Typical pin layout used:
  16.   -----------------------------------------------------------------------------------------
  17.               MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  18.               Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
  19.   Signal      Pin          Pin           Pin       Pin        Pin              Pin
  20.   -----------------------------------------------------------------------------------------
  21.   RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  22.   SPI SS 1    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required *
  23.   SPI SS 2    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required *
  24.   SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  25.   SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  26.   SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  27. */
  28.  
  29. #include <SPI.h>
  30. #include <MFRC522.h>
  31.  
  32. #define RST_1_PIN       8
  33.  
  34. #define SS_1_PIN        5
  35. #define SS_2_PIN        6
  36. #define SS_3_PIN        7
  37.  
  38.          
  39. #define NR_OF_READERS   3
  40.  
  41. byte ssPins[] = {SS_1_PIN, SS_2_PIN, SS_3_PIN};
  42.  
  43. MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
  44.  
  45.  
  46. const int buttonPin = 2;     // the number of the pushbutton pin
  47. const int ledPin =  3;      // the number of the LED pin
  48.  
  49. // variables will change:
  50. int buttonState = 0;         // variable for reading the pushbutton status
  51.  
  52.  
  53. void setup() {
  54.   Serial.begin(9600); // Initialize serial communications with the PC
  55.   while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  56.  
  57.   SPI.begin();        // Init SPI bus
  58.  
  59.   for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  60.     mfrc522[reader].PCD_Init(ssPins[reader], RST_1_PIN); // Init each MFRC522 card
  61.    
  62.     Serial.print(F("Reader "));
  63.     Serial.print(reader);
  64.     Serial.print(F(":"));
  65.     mfrc522[reader].PCD_DumpVersionToSerial();
  66.   }
  67.  
  68.     // initialize the LED pin as an output:
  69.   pinMode(ledPin, OUTPUT);
  70.   // initialize the pushbutton pin as an input:
  71.   pinMode(buttonPin, INPUT);
  72. }
  73.  
  74. void loop() {
  75.  
  76.   // delay(50);
  77.  
  78.   for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  79.     // Look for new cards
  80.    
  81.     if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  82.       Serial.print(F("Reader "));
  83.       Serial.print(reader);
  84.       // Show some details of the PICC (that is: the tag/card)
  85.       Serial.print(F(":"));
  86.       dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
  87.       Serial.println();
  88.       //Serial.print(F("PICC type: "));
  89.       //MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
  90.       //Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
  91.  
  92.       // Halt PICC
  93.       //mfrc522[reader].PICC_HaltA();
  94.       // Stop encryption on PCD
  95.       mfrc522[reader].PCD_StopCrypto1();
  96. }
  97.     else
  98.     {
  99.       Serial.print(F("Reader "));
  100.       Serial.print(reader);
  101.       Serial.print(F(":"));
  102.       Serial.print(F("X"));
  103.       Serial.println();
  104.     }
  105.  
  106.    // read the state of the pushbutton value:
  107.   buttonState = digitalRead(buttonPin);
  108.  
  109.   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  110.   if (buttonState == HIGH) {
  111.     // turn LED on:
  112.     digitalWrite(ledPin, HIGH);
  113.   } else {
  114.     // turn LED off:
  115.     digitalWrite(ledPin, LOW);
  116.   }
  117.    
  118.     } //if (mfrc522[reader].PICC_IsNewC
  119.   } //for(uint8_t reader
  120.  
  121.  
  122. /* Helper routine to dump a byte array as hex values to Serial. */
  123. void dump_byte_array(byte *buffer, byte bufferSize) {
  124.   for (byte i = 0; i < bufferSize; i++) {
  125.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  126.     Serial.print(buffer[i], HEX);
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement