miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.75k stars 1.43k forks source link

[HELP!!!] Counting RFID TAG with Multiple RFID Reader [MFRC522] #511

Closed fatahillahardhi closed 4 years ago

fatahillahardhi commented 4 years ago

Hii, im new in here

i have a question bout MFRC522, how can i counting RFID TAG in each Readers?

For example i have two rfid readers(reader 0 and reader 1) and i have two variables (count0 and count1), count0 for counting tag in readers 0 and count1 for counting tag in readers 1. Then i scan one tag in readers 0 so at count0 it will be 1, and in count1 it will be 0 (because i only scan in readers 0), it does in readers 1 too.

I also tried so hard(bcs im newbie in arduino) but i also failed:(

Pleaseee helpppp its for my final project in my college:(

 /**
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read data from more than one 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 read data from more than one PICC (that is: a RFID Tag or Card) using a
 * MFRC522 based RFID Reader on the Arduino SPI interface.
 *
 * Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
 *          and knowledge are required!
 *
 * @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 1    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
 * SPI SS 2    SDA(SS)      ** custom, take a unused pin, only HIGH/LOW required **
 * 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 <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h> //library penampil LCD

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_1_PIN        10         // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
#define SS_2_PIN        8          // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1

#define NR_OF_READERS   2

LiquidCrystal_I2C lcd(0x27,20,4); //0x3f dapat dicari dengan i2c scanner
SoftwareSerial s(5,6);

byte ssPins[] = {SS_1_PIN, SS_2_PIN};

MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.
int i = 1;
byte code[10];
int counter[NR_OF_READERS] = {0,0};
//int counter_1 = 0;
String uidString;

//char* reader0[10][13];
//char* reader1[10][13];
//int count0 = 1;
//int count1 = 1;

/**
 * Initialize.
 */
void setup() {
  s.begin(115200);
  Serial.begin(115200); // Initialize serial communications with the PC
  while (!Serial);
  SPI.begin();        // Init SPI bus

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
    Serial.print(F("Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
  }

  lcd.init();
  lcd.init();
  lcd.backlight(); //menghidupkan lampu latar LCD

 // mfrc522.PCD_Init(); // Init MFRC522

  lcd.setCursor (3,0);
  lcd.print("Final Project");
  delay (2000);
  lcd.clear();
}

/**
 * Main loop.
 */
void loop() {
  lcd.setCursor (0,0);
  lcd.print("SCAN RFID");

  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    // Look for new cards
//  uint8_t reader = 0;
//  while(NR_OF_READERS == 0){
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
      Serial.println();
      Serial.print(F("Reader "));
      Serial.print(reader);
      // Show some details of the PICC (that is: the tag/card)
      Serial.print(F(": Card UID:"));
      dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
      Serial.println();
      Serial.print(F("PICC type: "));
      MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
      Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
      counter[reader] = i++;
      lcd.setCursor (0,0);

//      lcd.setCursor(0,1);
//      lcd.print(counter);

      Serial.print("Counter ");
      Serial.print(reader);
      Serial.print(" : ");
      Serial.print(counter[reader]);
//      s.write(counter); 
      // Halt PICC
    }
      mfrc522[reader].PICC_HaltA();
      // Stop encryption on PCD
      mfrc522[reader].PCD_StopCrypto1();
//      counter = counter-counter;
    }    

    //if (mfrc522[reader].PICC_IsNewC
   //for(uint8_t reader
}

/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
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);
  }
}

By the way sorry for my bad english :D