Good evening,
I have this Shield NFC( http://wiki.seeedstudio.com/NFC_Shield_V2.0/) that has been running for almost a year on an arduino UNO.
I recently acquired a MEGA Arduino to expand the possibilities of my project.
The NFC shield has benefited from the modifications indicated in the wiki to be
able to use the shield with an IRQ (I also use the pin 9 instead of the 10)
and it works very well with the UNO.
Unfortunately it’s been a few days since I stumble to make it work the same
way on the MEGA. So I took the code of example # 3 with the adaptations that go well:
[code]#include <SPI.h>
#include “PN532_SPI.h”
#include “PN532.h”
#include “NfcAdapter.h”
// FLAG_NONE used to signal nothing needs to be done
#define FLAG_NONE 0
// FLAG_IRQ_TRIGGERED used to signal an interrupt trigger
#define FLAG_IRQ_TRIGGERED 1
// FLAG_RESET_IRQ used to signal that the interrupt needs to be reset
#define FLAG_RESET_IRQ 2
// flags variable used to store the present flag
volatile int flags = FLAG_NONE;
String const myUID = “1B B3 C6 EF”; // replace this UID with your NFC tag’s UID
// LED pins
int const greenLedPin = 3; // green led used for correct key notification
int const redLedPin = 4; // red led used for incorrect key notification
// the interrupt we’ll be using (interrupt 0) is located at digital pin 2
int const irqPin = 2; // interrupt pin
PN532_SPI interface(SPI, 9); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10
NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object
String scannedUID = “”; // this is where we’ll store the scanned tag’s UID
void setup(void) {
// make LED pins outputs
pinMode(greenLedPin,OUTPUT);
pinMode(redLedPin,OUTPUT);
Serial.begin(9600); // start serial comm
Serial.println("NDEF Reader");
nfc.begin(); // begin NFC comm
// turn off the LEDs
digitalWrite(greenLedPin,LOW);
digitalWrite(redLedPin,LOW);
// attach the function “irq” to interrupt 0 on the falling edges
attachInterrupt(digitalPinToInterrupt(2),irq,FALLING);// digital pin 2 is interrupt 0, we’ll call the irq function (below) on the falling edge of this pin
}
void loop(void) {
int flag = getFlag(); // get the present flag
switch(flag) // check which flag/signal we are on
{
case FLAG_NONE:
// nothing needs to be done
break;
case FLAG_IRQ_TRIGGERED: // the interrupt pin has been triggered
Serial.println("Interrupt Triggered");
if (nfc.tagPresent())
{
// an NFC tag is present
NfcTag tag = nfc.read(); // read the NFC tag
scannedUID = tag.getUidString(); // get the NFC tag's UID
if(myUID.compareTo(scannedUID) == 0) // compare the NFC tag's UID with the correct tag's UID (a match exists when compareTo returns 0)
{
// the scanned NFC tag matches the saved myUID value
Serial.println("Correct tag/key");
blinkLed(greenLedPin,200,4); // blink the green led
// put your here to trigger the unlocking mechanism (e.g. motor, transducer)
}else{
// the scanned NFC tag's UDI does not match the myUID value
Serial.println("Incorrect tag/key");
blinkLed(redLedPin,200,4); // blink the red led
// DO NOT UNLOCK! an incorrect NFC tag was used.
// put your code here to trigger an alarm (e.g. buzzard, speaker) or do something else
}
// return to the original state
setFlag(FLAG_NONE);
reset_PN532_IRQ_pin();
}else{
// a tag was not present (the IRQ was triggered by some other action)
setFlag(FLAG_NONE);
}
break;
default:
// do any other stuff for flags not handled above
break;
}
}
/*
- Name: setFlat
- Description: used to set actions/flags to be executed in the loop(void) function
- Parameters:
-
int flag - the action/flag to store
- Returns: void
*/
void setFlag(int flag)
{
flags = flag;
}
/*
- Name: getFlag
- Description: used to get the present flag/action
- Parameters: void
- Returns: int - the flags variable. The action/flag set by setFlag
*/
int getFlag()
{
return flags;
}
/*
- Name: irq
- Description: Interrupt service routine (ISR). This function will be executed whenever there is a falling edge on digital pin 2 (the interrupt 0 pin)
- Parameters: void
- Returns: void
/
void irq()
{
if(getFlag()==FLAG_NONE){
setFlag(FLAG_IRQ_TRIGGERED);
}
}
/ - Name: reset_PN532_IRQ_pin
- Description: used to reset the PN532 interrupt request (IRQ) pin
- Parameters: void
- Returns: void
*/
void reset_PN532_IRQ_pin()
{
nfc.tagPresent();
}
/*
- Name: blinkLed
- Description: used to toggle a pin to blink an LED attached to the pin
- Parameters:
-
ledPin - the pin where the led is connected to
-
delayTime - the time in milliseconds between HIGH and LOW
-
times - the number of times to toggle the pin
- Returns: void
*/
void blinkLed(int ledPin,int delayTime,int times)
{
for(int i=0;i<times;i++){
digitalWrite(ledPin,HIGH);
delay(delayTime);
digitalWrite(ledPin,LOW);
delay(delayTime);
}
}[/code]
It turns out that this code works very well with the UNO, as expected,
the tags are read correctly. On the other hand, on the MEGA,
the chip is detected but no reading of the tags is carried out.
It seems to me that this code should work on both cards without modification?
Any help will be appreciate.
Forgive my english and thanks to gTrad :roll: