adafruit / Adafruit-PN532

Arduino library for SPI and I2C access to the PN532 RFID/Near Field Communication chip
Other
422 stars 266 forks source link

PN532 Shield - can you start/stop the read function? #99

Closed davessi closed 1 year ago

davessi commented 2 years ago

I am working on a wheel of fortune game and thought of using RFID cards in the segments of the wheel to identify and play certain videos when the wheel has stopped. If the wheel stops in segment A it would read the card in that segment and then play a video on a laptop. I can get this to work with an Arduino Leonardo and the Keyboard.h library. If I test the PN532 with the appropriate card it will play the correct video. My issue is that the PN532 is waiting to read cards all the time on set-up and as the wheel turns I just get one card id after the other and that's a problem. I built a tachometer with a hall sensor to tell me when the wheel is moving and when it has stopped. I can trigger a simple LED to turn on when moving and off when the wheel has stopped. However, I cannot figure out how to get that tachometer to stop the PN532 when the wheel is turning and then read a card. my code is below and I am a novice programmer. Is there a pause or stop-read command that I am missing? code below:

#include <Keyboard.h>
#include <Wire.h>
#include <Adafruit_PN532.h>
#include <BlockNot.h>

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (6)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

BlockNot cardTimer0(5, SECONDS);
BlockNot cardTimer1(5, SECONDS); 
BlockNot cardTimer2(5, SECONDS);
BlockNot cardTimer3(5, SECONDS);
BlockNot cardTimer4(5, SECONDS);

float revolutions=0;
int rpm=0; // max value 32,767 16 bit
long  startTime=0;
long  elapsedTime;
int ledPin =12;

void setup(void) {

  Serial.begin(115200);
  while (!Serial) delay(10); // for Leonardo/Micro/Zero

  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // configure board to read RFID tags
  nfc.SAMConfig();

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

void loop(void) {

revolutions=0; rpm=0;
startTime=millis();         
attachInterrupt(digitalPinToInterrupt(7),interruptFunction,RISING);
delay(1000);
detachInterrupt(7);                

//How many counts we've had from the hall effect sensor and calc the RPM
elapsedTime=millis()-startTime;     //finds the time, should be very close to 1 sec

if(revolutions>0)
{
  rpm=(max(1, revolutions) * 60000) / elapsedTime;        //calculates rpm
}

String outMsg = String("RPM :") + rpm;
Serial.println(outMsg);

if ((rpm) > 0)
Serial.print ("Wheel Spinning"); 

if ((rpm) == 0)
  Keyboard.begin();

  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {

    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.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);

     if ((cardid == 1839898475 || 641164975 )&& cardTimer0.TRIGGERED){
      Keyboard.write('A');

     } 
     if ((cardid == 3335073343 || 3813925170)&& cardTimer1.TRIGGERED){
      Keyboard.write('B');

     }  
    if ((cardid == 2260681791 || 2796534086)&& cardTimer2.TRIGGERED){
      Keyboard.write('C'); 

    }  
    if ((cardid == 3329626431 || 1716831046)&& cardTimer3.TRIGGERED){  
      Keyboard.write('D');

    }   
    if ((cardid == 1186344511 || 3813206066)&& cardTimer4.TRIGGERED){
      Keyboard.write('E');

    }          
    Keyboard.end();      
     Serial.println("");
  }
}
void interruptFunction() //interrupt service routine
{  
  revolutions++;
}
caternuson commented 1 year ago

Closing since not a library issue. Can try posting in the forums for help with project code: https://forums.adafruit.com/