Hi
I am trying to make my first practical code to use in a real project.
Model train: Have a NFC-tag in each train, a NFC reader over the different stops in the railyard. Then the LCD show what train is on the different stops. Track 1 - Train 2
After trying to merge two codes, I got it to work after some trying. There is a problem i can`t figure out tho. There is two errors occurring randomly after reading the NFC-tag.
Error 1: The Serial monitor say “read failed 4” and everything just stops working. I have to reset the Arduino to make it run again.
Error 2: The Serial monitor say “read failed 5” and puts out alot of “###**⸮⸮” in random order and length.
Can anyone help me out?
I have a goal to make the code as short as possible. I have tried to remove the function for “// If you have more than 1 Message then it will cycle through them” as I only have one line. Not very important as long as it works.
Got my two codes here:
Arduino Uno
PN532
LCD 16/2 with i2c-shield
Code:
// Include Wire Library for I2C
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h> // The following files are included in the libraries Installed
#include <NfcAdapter.h>
#include <LiquidCrystal_I2C.h> // Include NewLiquidCrystal Library for I2C
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c); // Indicates the Shield you are using
const int i2c_addr = 0x27; // Define I2C Address of LCD-display
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3; //Define the LCD-pinout
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE); //Define the LCD-pinout
void setup(void) {
Serial.begin(9600); // Initiate a serial communication
nfc.begin(); // Initiate MP532
Serial.print("\n"); // Set a emty space between info from PN532 and first tag-read
lcd.begin(16,2); // Set display type as 16 char, 2 rows
lcd.setCursor(2,0); // Set cursor to top left corner
lcd.print(“Track 1”); // Set location
}
void loop(void) {
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();
if (message.getRecordCount() != 1) {
}
int recordCount = message.getRecordCount(); // If you have more than 1 Message then it will cycle through them
for (int i = 0; i < recordCount; i++){
NdefRecord record = message.getRecord(i);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String payloadAsString = “”; // Processes the message as a string vs as a HEX value
for (int c = 0; c < payloadLength; c++){
payloadAsString += (char)payload
;
}
Serial.println(payloadAsString);
lcd.setCursor(0,1);
lcd.print(payloadAsString);
lcd.setCursor(0,1);
lcd.print(" ");
String uid = record.getId();
if (uid != ""){
Serial.print(" ID: ");Serial.println(uid); // Prints the Unique Identification of the NFC Tag
}
}
}
}
}
[u]Serial monitor error 1:[/u]
⸮Found chip PN532
Firmware ver. 1.6
Mifare Classic
UID: 1E 2D 23 D9
en Train 2
Mifare Classic
UID: 1E 2D 23 D9
en Train 2
Read failed 4
[u]Serial monitor error 2:[/u]
Found chip PN532
Firmware ver. 1.6
Mifare Classic
UID: 1E 2D 23 D9
en Train 2
Read failed 5
Mifare Classic
UID: 1E 2D 23 D9
en Train⸮⸮u
[i](both can be as short as this, or after 10/20/30 tests)[/i]
I have looked around the forum and google, but can`t find this problem. Sorry if I am bad to search, please respond with a link if there is.
Kind regards
Terje
