Ok, so I tested a few things and built a small wheel in my shop to verify the concepts. I went with a hall sensor because lots of ambient light was interfering with the IR sensor. The Hall works fine and on it's own with a simple tach code and I can get RPM on the serial port. That all works.
I also have the card reader working fine and it will read the cards as they should. Unfortunately they continue to read as the wheel spins. I was hoping my IF statement would keep the reader from reading until the wheel has stopped. The tach works and card reader works, I just cannot get them to work together. I just need to know when the wheel stops and take a reading.
#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
}
///lcd.setCursor(0,0);
String outMsg = String("RPM :") + rpm;
//fillMessage2DisplayWidth(outMsg);
//lcd.print(outMsg);
Serial.println(outMsg);
if ((rpm) > 0)
Serial.print ("Wheel Spinning");
else 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++;
}