LCD Display message disappears completely

Hi everyone,

I am having some issues with the 16x2 LCD Display from the Genuino Starter Kit.

I want to build a NFC authenticator using the Seeed Studio NFC Shield v1.0. When starting the program, the text on the LCD display should say something like "Scan your NFC Tag here".
If the scanned NFC Tag is correct, it should say "Correct NFC Tag", otherwise it should say "Wrong NFC Tag".

Here's the code of my program:

// include the library code:
#include <LiquidCrystal.h>
#include <PN532.h>
#include <SPI.h>

#define PN532_CS 10
PN532 nfc(PN532_CS);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
Serial.begin(9600);
Serial.println("Hello!");
  
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Scan NFC Tag to");
  lcd.setCursor(0,1);
  lcd.print("open the box!");

  nfc.begin();
  nfc.SAMConfig();
}

void loop() {
 uint32_t id;
 id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);

 if (id == 4218538142) {
  lcd.clear();
  lcd.print("Code correct!");
  lcd.setCursor(0,1);
  lcd.print("Open the box!");
 }
 else if (id != 4218538142) {
  lcd.clear();
  lcd.print("Wrong code!");
  lcd.setCursor(0,1);
  lcd.print("Try again!");
 }
}

The NFC code is derived from the sketch "readMifareTargetID" combined with LCD Display code elements.

#include <PN532.h>
#include <SPI.h>

/*Chip select pin can be connected to D10 or D9 which is hareware optional*/
/*if you the version of NFC Shield from SeeedStudio is v2.0.*/
#define PN532_CS 10
PN532 nfc(PN532_CS);
#define  NFC_DEMO_DEBUG 1

void setup(void) {
#ifdef NFC_DEMO_DEBUG
  Serial.begin(9600);
  Serial.println("Hello!");
#endif
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Didn't find PN53x board");
#endif
    while (1); // halt
  }
#ifdef NFC_DEMO_DEBUG
  // 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);
  Serial.print("Supports "); 
  Serial.println(versiondata & 0xFF, HEX);
#endif
  // configure board to read RFID tags and cards
  nfc.SAMConfig();
}


void loop(void) {
  uint32_t id;
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);

  if (id != 0) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Read card #"); 
    Serial.println(id);
#endif
  }
 if (id == 4218538142) {
  lcd.clear();
  lcd.print("Code correct!");
  lcd.setCursor(0,1);
  lcd.print("Open the box!");
 }
 else if (id != 4218538142) {
  lcd.clear();
  lcd.print("Wrong code!");
  lcd.setCursor(0,1);
  lcd.print("Try again!");
 }
}

Attached you will find a picture of the wiring.

Whenever I try to run the program now, the text "Scan NFC tag to open the box!" appears, but after a while the letters disappear until the screen is blank.
In the serial monitor I can see that the Code from the Mifare Card is scanned, but nothing appears on the screen.
When I clear the void loop() section the letters stay on the screen, but with the shown code the display goes blank.

Does anybody know what my mistake is?

Any help is highly appreciated!

Cheers,
Tractorbeam

[OP] Sorry the size of the picture was too large.

Here's the picture of the wiring mentioned in my post.

I recommend that you not 'clear' the LCD. A better solution is to pad out your lcd.print statements with blanks so that each of the messages is 16 characters long. Each new message will completely overwrite the previous one with no need to first overwrite the old message with blanks (which is what the 'clear' command does).

You should also consider how often you want to run the code in loop(). Perhaps a delay or two are called for. As it stands now you are clearing the screen, writing a message, clearing the screen, writing a message, etc. You really don't have time to see the message before the screen is cleared again.

Once you get things working you should consider whether or not you really need to rewrite a message that is already on the display.

Don

floresta:
I recommend that you not 'clear' the LCD. A better solution is to pad out your lcd.print statements with blanks so that each of the messages is 16 characters long.

So you mean something like this?

if (id == 4218538142) {
  lcd.print("Code correct!  ");
  lcd.setCursor(0,1);
  lcd.print("Open the box!   ");
 }
 else if (id != 4218538142) {
  lcd.print("Wrong code!     ");
  lcd.setCursor(0,1);
  lcd.print("Try again!      ");
 }

I got rid of the clear command. But with the extra blanks in the messages the screen shows weird signs (picture attached).

floresta:
You should also consider how often you want to run the code in loop(). Perhaps a delay or two are called for. As it stands now you are clearing the screen, writing a message, clearing the screen, writing a message, etc. You really don't have time to see the message before the screen is cleared again.

Can you give me a suggestion on how to structure the code in the loop section? Because I am not sure how to place the delays in the loop.

What I'd like the program to do is that it keeps showing one of the above messages after the scanning process (either "Code correct" or "Wrong code").
But from my understanding the loop runs multiple times and that's why the message won't stay on the screen. Do I have to add variables in order to keep the text on the screen?

Thanks for your help!!!

So you mean something like this?

Almost. The 'clear' command also resets the cursor to (0,0). Since you have removed that command you will have to add a 'set cursor' command before you write to the top row.

As long as power is applied any text that you send to the display will remain on the screen until overwritten.

You are running around the loop rewriting the screen each time around even if nothing changes. At a bare minimum you need some sort of delay somewhere in the loop so you can read the screen.

Don