Interrupt when card removed PN512

Hello,

I'm working on a project involving four PN512s. I manage to get an interrupt when a card is detected but I don't understand how to get one when the card is removed.

Can you help me with that ?

Here my code:

#include <SPI.h>
#include <PN512.h>
#include "FastLED.h"

/*------------------RFID-------------------*/
#define NFC_RST 16

#define NFC_SS_1 17
#define NFC_SS_2 21
#define NFC_SS_3 22
#define NFC_SS_4 25

#define NFC_MOSI 23
#define NFC_MISO 19
#define NFC_SCK 18

#define IRQ_1 36
#define IRQ_2 39
#define IRQ_3 34
#define IRQ_4 35

#define NB_OF_READERS 4

PN512 readers[NB_OF_READERS];

/*tableau des pins de selection d'esclave*/
byte ssPins[] = { NFC_SS_1, NFC_SS_2, NFC_SS_3, NFC_SS_4 };

/*tableau des pins IRQ*/
byte irqPins[] = { IRQ_1, IRQ_2, IRQ_3, IRQ_4 };

/*état des evenements sur les lecteurs*/
volatile bool newCards[] = { false, false, false, false };

/*------------------!RFID-------------------*/

/*------------------RUBAN------------------*/

/*nombre de LEDs sur un ruban*/
#define NB_LED_STRIPS 25

/*pins de controle des rubans*/
#define STRIP_1_PIN 26
#define STRIP_2_PIN 27
#define STRIP_3_PIN 32
#define STRIP_4_PIN 33

/*nombre de rubans*/
#define NB_OF_STRIPS 4

/*tableau des pins de controle des rubans*/
byte stripsPins[] = { STRIP_1_PIN, STRIP_2_PIN, STRIP_3_PIN, STRIP_4_PIN };

/*tableau des ruban*/
CRGB strips[NB_OF_STRIPS][NB_LED_STRIPS];

/*position d'animation de chaque ruban*/
byte animatePositions[] = { 0, 0, 0, 0 };

/*index de la LED témoin de fonctionnement*/
#define STANDBY_LED 13

/*couleur d'animation du ruban*/
CRGB animationColorP1[] = { CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green };
CRGB animationColorP2[] = { CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green };

/*es qu'il faut animer un chenillard bi directionnel, classement par ruban*/
boolean mustAnimateBiDirectional[] = { false, false, false, false };

/*------------------!RUBAN------------------*/

volatile bool bNewInt = false;
void setup() {
  Serial.begin(9600);
  delay(1000);
  /*------------------RFID-------------------*/
  //remettre à 0 les lecteurs
  pinMode(NFC_RST, OUTPUT);
  nfcReset();

  //démarrer la communication avec les lecteurs
  SPI.begin(NFC_SCK, NFC_MISO, NFC_MOSI);

  for (byte i = 0; i < NB_OF_READERS; i++) {
    //interdire à la bibliotheque l'utilisation du reset
    readers[i] = PN512(ssPins[i], UINT8_MAX);

    //initialiser le lecteur
    readers[i].PCD_Init();

    //définir le gain de l'antenne
    readers[i].PCD_SetAntennaGain(PN512::RxGain_33dB);

    //activer l'événement sur la présence d'une nouvelle carte
    pinMode(irqPins[i], INPUT_PULLUP);
    readers[i].PCD_WriteRegister(readers[i].ComIEnReg, 0b10100000);
    newCards[i] = false;
  }

  attachInterrupt(irqPins[0], irq0, FALLING);
  attachInterrupt(irqPins[1], irq1, FALLING);
  attachInterrupt(irqPins[2], irq2, FALLING);
  attachInterrupt(irqPins[3], irq3, FALLING);

  /*------------------!RFID-------------------*/

  /*------------------RUBAN-------------------*/
  FastLED.addLeds<NEOPIXEL, STRIP_1_PIN>(strips[0], NB_LED_STRIPS);
  FastLED.addLeds<NEOPIXEL, STRIP_2_PIN>(strips[1], NB_LED_STRIPS);
  FastLED.addLeds<NEOPIXEL, STRIP_3_PIN>(strips[2], NB_LED_STRIPS);
  FastLED.addLeds<NEOPIXEL, STRIP_4_PIN>(strips[3], NB_LED_STRIPS);
  /*------------------!RUBAN-------------------*/
}

void irq0() {
  newCards[0] = true;
}
void irq1() {
  newCards[1] = true;
}
void irq2() {
  newCards[2] = true;
}
void irq3() {
  newCards[3] = true;
}

void nfcReset() {
  digitalWrite(NFC_RST, LOW);
  delay(100);
  digitalWrite(NFC_RST, HIGH);
  delay(100);
}

char dataOut[256];
char dataLine[50];
void loop() {
  delay(40);

  for (byte i = 0; i < NB_OF_READERS; i++) {
    if (newCards[i]) {
      PN512 reader = readers[i];
      reader.PICC_ReadCardSerial();
      Serial.print(i);
      printHex(reader.uid.uidByte, reader.uid.size);
      clearInt(reader);
      reader.PICC_HaltA();
      newCards[i] = false;
    }
    activateRec(readers[i]);
  }
  delay(100);
}

/*
 * The function sending to the MFRC522 the needed commands to activate the reception
 */
void activateRec(PN512 reader) {
  reader.PCD_WriteRegister(reader.FIFODataReg, reader.PICC_CMD_REQA);
  reader.PCD_WriteRegister(reader.CommandReg, reader.PCD_Transceive);
  reader.PCD_WriteRegister(reader.BitFramingReg, 0x87);
}

/*
 * The function to clear the pending interrupt bits after interrupt serving routine
 */
void clearInt(PN512 reader) {
  reader.PCD_WriteRegister(reader.ComIrqReg, 0x7F);
}

void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
  Serial.println("");
}

Why do you need an interrupt? You're scanning through the 'newCards[]' array on every pass through the loop() function. Why don't you instead use that for() loop to do a digitalRead() of each of the pins?

I am only at the beginning of my code, in the future I will have other actions in parallel.
Isn't it cleaner to make an interrupt rather than to interrogate the PN512 each time?

Not really. You're not able to do anything about it until you check the 'newCards[]' array in loop() anyway.

If you write proper non-blocking code that won't be an issue. And if you don't, the interrupt won't help because your checking of 'newCards[]' will be blocked.

Now I understand.
I will do like that !

Thank you.

Hello, can you share me a tutorial using module PN512 with any card NFC? example simple such as read and write data on Card. thanks you so much.

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