I have a adafruit nfc shield: http://www.adafruit.com/products/789 which is reading properly. I’m trying to add a Serial Event or a Serial.read() to receive data from processing. After I implement the Serial.read(), the data reads buffer properly but only after the RFID tag is read. It can only read the last string that was sent if the rfid tag wasn’t read in between the serial communication. The Serial Event and while(Serial.available) in the draw loop results in the same bug.
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
/////////////RFID Variables/////////////////
#define IRQ (2)
#define RESET (3) // Not connected by default on the NFC Shield
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
uint8_t puid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
////////////LEDS///////////////////////////
String inputString = ""; // a string to hold incoming data
char buf[32];
const int r = 9;
const int g = 10;
const int b = 11;
int color[3] = {0,0,0};
boolean recieved = false;
int motor = 10;
void setup(void) {
Serial.begin(115200);
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);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
//setup outputs for rgb led strips
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
}
void loop(void) {
boolean 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[0], &uidLength);
if (success) {
//Serial.println("Found a card!");
///Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
//sends constant stream
Serial.print("UID Value:");
for (uint8_t i=0; i < uidLength; i++)
{
//Serial.print(" 0x");
//Serial.print(uid[i], HEX);
Serial.print(uid[i], DEC);
}
Serial.print("\n");
// Wait 1 second before continuing
delay(500);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar != '\n') {
if (inChar == 'M' || inChar == 'm')
{
motor = Serial.parseInt();
//Serial.println(motor);
}
else
{
inputString += inChar;
}
}
else
{
parseString(inputString);
inputString = "";
recieved = true;
}
}
if(recieved == true)
{
Serial.print("recieved: ");
Serial.print(color[0]);
Serial.print(" ");
Serial.print(color[1]);
Serial.print(" ");
Serial.println(color[2]);
analogWrite(r, color[0]);
analogWrite(g, color[1]);
analogWrite(b, color[2]);
recieved = false;
}
}
void parseString(String message)
{
//Serial.println(message);
int commaPosition;
int count = 0;
do
{
commaPosition = message.indexOf(',');
if(commaPosition != -1)
{
//Serial.println( message.substring(0,commaPosition));
message = message.substring(commaPosition+1, message.length());
}
else
{ // here after the last comma is found
if(message.length() > 0)
{
//Serial.println(message); // if there is text after the last comma,
} // print it
}
color[count] = message.toInt();
count++;
}
while(commaPosition >=0);
/* for(int x =0; x<3; x++)
{
Serial.print("id: ");
Serial.print(x);
Serial.print(" ");
Serial.print(color[x]);
Serial.println();
} */
}