Back to Parent

[code]
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
String incoming="";
String incomings="";
int payloadLength=0;
int t=0;
int X=1;

boolean newData = false;

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

void setup() {
  Serial.begin(9600);
  nfc.begin();
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
}

void loop() {
  Serial.println("\nScan NFC tag on the NFC Shield\n");  // Command so that you an others will know what to do 
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.write("Scan NFC tag on");
  lcd.setCursor(0,1);
  lcd.write("the NFC Shield");

  if (nfc.tagPresent())
  {
    NfcTag tag = nfc.read();
    //Serial.println(tag.getTagType());
    Serial.print("UID: ");Serial.println(tag.getUidString()); // Retrieves the Unique Identification from your tag
    if (tag.hasNdefMessage()) // If your tag has a message
    {

      NdefMessage message = tag.getNdefMessage();
      Serial.print("\nThis Message in this Tag is ");
      Serial.print(message.getRecordCount());
      Serial.print(" NFC Tag Record");
      if (message.getRecordCount() != 1) { 
        Serial.print("s");
      }
      Serial.println(".");
      //lcd.write(".");
      // If you have more than 1 Message then it wil cycle through them
      int recordCount = message.getRecordCount();
      for (int i = 0; i < recordCount; i++)
      {
        Serial.print("\nNDEF Record ");Serial.println(i+1);
        NdefRecord record = message.getRecord(i);

        payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);

        if(payloadLength>0 && X==1){
        X=0;
        }


        String payloadAsString = ""; // Processes the message as a string vs as a HEX value
        for (int c = 0; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print("  Information (as String): ");
        Serial.println(payloadAsString);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.write("Information:");
        lcd.setCursor(0,1);
        delay(500);
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.print(payloadAsString);
        while(t<25){
        lcd.scrollDisplayLeft();
        t++;
        delay(250);
        }
        t=0;
        String uid = record.getId();
        //if (uid != "") {
          //Serial.print("  ID: ");
          //Serial.println(uid); // Prints the Unique Identification of the NFC Tag
          //lcd.write("  ID: ");
         // lcd.write(uid); 
       // }
      }
        if(X==0){
        Serial.println("\nDo you wish to erase this tag?(Y/N)\n");
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.write("Do you wish to erase this tag?(Y/N)");
        while(t<25){
        lcd.scrollDisplayLeft();
        t++;
        delay(250);
        }
        t=0;
        while(Serial.available()==0){
        }
        incoming=Serial.readString();
        if(incoming =="Y\n"){
                Serial.println("\nPlace tag on NFC shield.\n");
                lcd.clear();
                lcd.write("Place tag on NFC shield");
                while(t<15){
                lcd.scrollDisplayLeft();
                t++;
                delay(250);
                }
                t=0;
                while(nfc.tagPresent()==0){
                 }
                eraseNFC();
                Serial.println("\nDo you wish to write to this tag?(Y/N)\n");
                lcd.clear();
                lcd.setCursor(2,0);
                lcd.write("Do you wish to write to this tag?(Y/N)");
                while(t<25){
                lcd.scrollDisplayLeft();
                t++;
                delay(250);
                }
                t=0;
                while(Serial.available()==0){
                }
                incoming=Serial.readString();
                if(incoming == "Y\n"){
                       writeNFC();
                 }
                 else{
                     Serial.println("\nRemove tag.\n");
                     lcd.clear(); 
                     lcd.write("Remove tag."); 
                 }
              }
              else{
              Serial.println("\nRemove tag.\n"); 
              lcd.clear();
              lcd.write("Remove tag.");          
              }
    }
    else{
      Serial.println("\nDo you wish to write to this tag?(Y/N)\n");
      lcd.clear();
      lcd.setCursor(2,0);
      lcd.write("Do you wish to write to this tag?(Y/N)"); 
      while(t<25){
      lcd.scrollDisplayLeft();
      t++;
      delay(250);
      }
      t=0;
      while(Serial.available()==0){
        } 
      incoming=Serial.readString();
      if(incoming == "Y\n"){
        eraseNFC();
        writeNFC();
      }
      else{
        Serial.println("\nPlease remove tag.\n");
        lcd.clear();
        lcd.write("Please remove tag.");
      }
  }
}
}
delay(3000);
}

void writeNFC() {
  if (nfc.tagPresent()) {
        NdefMessage message = NdefMessage();
        Serial.println("Write a message:");
        lcd.clear();
        lcd.write("Write a message:");
        while(Serial.available()==0){
        }
        Serial.print("\nPlease place tag on NFC shield.\n"); 
        lcd.clear(); 
        lcd.setCursor(4,0);
        lcd.write("Please place tag on NFC shield.");
        while(t<20){
        lcd.scrollDisplayLeft();
        t++;
        delay(250);
        }
        t=0;
        while(nfc.tagPresent()==0){
        }
        message.addTextRecord(Serial.readString()); // Text Message you want to Record
        Serial.println("\nDo you wish to add a link?(Y/N)\n");
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.write("Do you wish to add a link?(Y/N)");
        while(t<25){
        lcd.scrollDisplayLeft();
        t++;
        delay(250);
        }
        t=0;
        while(Serial.available()==0){
        }
          incoming=Serial.readString();
          if(incoming=="Y\n"){
          Serial.println("\nAdd URL:");
          lcd.clear();
          lcd.write("Add URL:");
          while(Serial.available()==0){
          }
          Serial.println("\nPlease place tag on NFC shield.\n"); 
          lcd.clear();
          lcd.setCursor(2,0);
          lcd.write("Place tag on NFC shield.");
          while(t<17){
          lcd.scrollDisplayLeft();
          t++;
          delay(250);
          }
          t=0;
          while(nfc.tagPresent()==0){
          }
          message.addUriRecord(Serial.readString());
          Serial.print("here");      
        }
        boolean success = nfc.write(message);
        if (success) {
            Serial.println("Good Job, now read it with your phone!"); // if it works you will see this message 
            lcd.clear();
            lcd.setCursor(2,0);
            lcd.write("Good Job, now read it with your phone!");
            while(t<25){
            lcd.scrollDisplayLeft();
            t++;
            delay(250);
            }
            t=0;
        } 
        else {
            Serial.println("Write failed"); // If the the rewrite failed you will see this message
            lcd.clear();
            lcd.setCursor(4,0);
            lcd.write("Write failed");
        }
}
delay(3000);
}

void eraseNFC() {
    if (nfc.tagPresent()) {
        bool success = nfc.erase();
        if (success) {
            Serial.println("\nSuccess, tag contains an empty record.");
            lcd.clear();
            lcd.setCursor(4,0);
            lcd.write("Success,tag contains an empty record.");  
            while(t<25){
            lcd.scrollDisplayLeft();
            t++;
            delay(250);
            }
            t=0;      
        } 
        else {
            Serial.println("\nUnable to erase tag.");
            lcd.clear();
            lcd.setCursor(4,0);
            lcd.write("\nUnable to erase tag.");
            while(t<37){
            lcd.scrollDisplayLeft();
            t++;
            delay(250);
            }
            t=0;
        }

    }
}

[/code]
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0