2 (or more) PN532 modules. compare correcr cards on correct reader

I want to use 2 or more rfid readers to read two cards and check if the correct card is on the correct reader!

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// Number of Readers
int no_Readers = 2;

// Pin Definition
#define PN532_SCK (52)
#define PN532_MOSI (51)
//#define PN532_SS   (10)
#define PN532_MISO (50)


#define PN532_SS_1 10
#define PN532_SS_2 9
//#define PN532_SS_3 8
//#define PN532_SS_4 7

#define controlledOutput 5


// Arrays for saved UIDs
uint8_t savedUIDs[][7] = {
  { 0x43, 0xFC, 0x6B, 0xA9},
  { 0xA2, 0x1E, 0x3D, 0x51}
};

// Adafruit_PN532 Objects
Adafruit_PN532 nfc[] = {
  Adafruit_PN532(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_1),
  Adafruit_PN532(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_2),
  //Adafruit_PN532 (PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_3),
  //Adafruit_PN532 (PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_4)
};

void setup(void) {
  Serial.begin(115200);
  pinMode(controlledOutput, OUTPUT);
  while (!Serial) delay(10);

  for (int i = 0; i < no_Readers; ++i) {
    // Setup and Check Scanner
    nfc[i].begin();
    uint32_t versiondata = nfc[i].getFirmwareVersion();
    if (!versiondata) {
      Serial.print("Scanner ");
      Serial.print(i + 1);
      Serial.println(": PN53X board not found");
      while (1)
        ;
    }
    //nfc[i].SAMConfig();
    Serial.print("Scanner ");
    Serial.print(i + 1);
    Serial.println("found");
  }

  Serial.println("Waiting for Card ...");
}

// ... (other functions)

void loop(void) {
  for (int i = 0; i < no_Readers; ++i) {
    uint8_t uid[] = { 0, 0, 0, 0 };
    uint8_t uidLength;

    if (nfc[i].startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) {
      uint8_t success = nfc[i].readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
    if (success) {
  
    Serial.print("  UID Value ");
     Serial.print(i);
     Serial.print("  : ");
    nfc[i].PrintHex(uid, uidLength);
    
}
    }

    if (!nfc[i].startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) {
      //Serial.print("Scanner ");
      //Serial.print(i + 1);
      //Serial.println(": UID Not found");
    } else {
    
    }

    //savedUIDs[]
    if (compareArrays(uid[i], savedUIDs[i], 4)) {
      // 4 Successful keys Detected
      //Serial.println();
      Serial.println("Unlocked!");
      digitalWrite(controlledOutput, HIGH);
      //delay(1000);
    } else {
      //Serial.println();
      //Serial.println("Locked!");
      digitalWrite(controlledOutput, LOW);
      // delay(1000);
    }
  }
}
bool compareArrays(uint8_t arr1[], uint8_t arr2[], int length) {
  for (int i = 0; i < length; i++) {
    if (arr1[i] != arr2[i]) {
      return false;
    }
  }
  return true;
}

Do you have a question ?

it won't compare the cards!
what is wrong with the code?

Have you tried printing the values that are being compared ro see if they are what you expect them to be ?

bool compareArrays(uint8_t arr1[], uint8_t arr2[], int length) {
 for (int i = 0; i < length; i++) {
  Serial.print(arr1[i] );
 }
       Serial.print (" : "); 
 for (int i = 0; i < length; i++) {
  Serial.print(arr2[i] );
 }
        Serial.println ("  "); 
  for (int i = 0; i < length; i++) {
    if (arr1[i] != arr2[i]) {
     
      return false;

    }
  }
   Serial.println("true");
  return true;
}

i get :

1600734 : 162306181
1600494 : 67252107169

why?

Make things clearer by printing the array elements in HEX with a space between them

I get :

UID Value 0 : 0x43 0xFC 0x6B 0xA9
0 3 3 19 : 43 FC 6B A9
UID Value 1 : 0xA2 0x1E 0x3D 0x51
1E 0 0 0 : A2 1E 3D 51

Thanks i guess you helped me!!!!!
this now works!!!!

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// Number of Readers
const byte no_Readers = 2;

// Pin Definition
#define PN532_SCK (52)
#define PN532_MOSI (51)
//#define PN532_SS   (10)
#define PN532_MISO (50)


#define PN532_SS_1 10
#define PN532_SS_2 9
//#define PN532_SS_3 8
//#define PN532_SS_4 7

#define controlledOutput 5
String currentIDs[no_Readers];
const String correctIDs[] = { "2719890769", "1140616105" };

// Adafruit_PN532 Objects
Adafruit_PN532 nfc[] = {
  Adafruit_PN532(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_1),
  Adafruit_PN532(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_2),
  //Adafruit_PN532 (PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_3),
  //Adafruit_PN532 (PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS_4)
};

void setup(void) {
  Serial.begin(115200);
  pinMode(controlledOutput, OUTPUT);
  while (!Serial) delay(10);

  for (int i = 0; i < no_Readers; ++i) {
    // Setup and Check Scanner
    nfc[i].begin();
    uint32_t versiondata = nfc[i].getFirmwareVersion();
    if (!versiondata) {
      Serial.print("Scanner ");
      Serial.print(i + 1);
      Serial.println(": PN53X board not found");
      while (1)
        ;
    }
    //nfc[i].SAMConfig();
    Serial.print("Scanner ");
    Serial.print(i + 1);
    Serial.println("found");
  }

  Serial.println("Waiting for Card ...");
}

// ... (other functions)

void loop(void) {
   boolean puzzleSolved = false;
  for (int i = 0; i < no_Readers; ++i) {
    uint8_t uid[] = { 0, 0, 0, 0 };
    uint8_t uidLength;

    if (nfc[i].startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) {
      uint8_t success = nfc[i].readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
      if (success) {

        Serial.print("  UID Value ");
        Serial.print(i);
        Serial.print("  : ");
        nfc[i].PrintHex(uid, uidLength);


        if (uidLength == 4) {
          // We probably have a Mifare Classic card ...
          uint32_t cardid = uid[0];
          cardid <<= 8;
          cardid |= uid[1];
          cardid <<= 8;
          cardid |= uid[2];
          cardid <<= 8;
          cardid |= uid[3];
          Serial.print("Seems to be a Mifare Classic card #");
          Serial.println(cardid);
          currentIDs[i] = cardid;
          if (currentIDs[0] == correctIDs[0] && currentIDs[1] == correctIDs[1]) {
            // The puzzle has not been solved
              puzzleSolved = true;
          } else{puzzleSolved = false;
          Serial.println("false");
          }
        }
      }
    }
  

  if (!nfc[i].startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A)) {
  Serial.println("false");
  } else {
  }
  }

    if(puzzleSolved){
    Serial.println("true");
  }
}

Please explain what the probkem was and how you fixed it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.