PN532 i2C nfc.begin(); stops the code

Hello Guys,

i have maybe a very simple question, but didnt find the answer on the internet.
My project is to turning off and on module PN 532 from adafruit to save nergy from battery.

I tested on arduino nano & mega 2560
module PN532 for NFC read on I2C bus (i2C is here a need)
library: GitHub - picospuch/PN532: NFC library for Arduino using PN532
Problem i encountered: line nfc.begin(); does stuck/stop the code from running, when module is not present (this is wanted).
If the module is present no problem at all.

Question: how to let the code run through/over/past nfc.begin(); when pn532 module is not present???
THX

#if 0
#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_SPI pn532spi(SPI, 10);
NfcAdapter nfc = NfcAdapter(pn532spi);

#else
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
#endif

void setup() {
Serial.begin(9600);
Serial.println(“NDEF Writer”);
nfc.begin();
Serial.println(“Check if alive”);
}

void loop() {
Serial.println("\nPlace a formatted Mifare Classic NFC tag on the reader.");
if (nfc.tagPresent()) {
NdefMessage message = NdefMessage();
message.addUriRecord(“http://arduino.cc”);

bool success = nfc.write(message);
if (success) {
Serial.println(“Success. Try reading this tag with your phone.”);
} else {
Serial.println(“Write failed.”);
}
}
delay(5000);
}

Problem i encountered: line nfc.begin(); does stuck/stop the code from running, when module is not present (this is wanted).

It's interesting that this is a problem if you wanted it explicitly.

Anyway, this behavior is explicitly programmed in the library (using while (1);), so this library can only be used unmodified if the hardware is always available at startup.

Question: how to let the code run through/over/past nfc.begin(); when pn532 module is not present???

Modify the begin() method to return a boolean status instead of the endless loop. It's not possible without modifying the library.

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