I am using a ILI9342 Touchscreen TFT and the MCUFRIEND_kbv.h library and the examples all work fine. I am trying to adapt a working RC522 RFID I2C sketch which uses the MFRC522_I2C.h library to use the TFT rather than the serial monitor. The TFT uses SPI and the RFID uses I2C so there is no conflict there. The problem I encounter it that the TFT stops displaying and presents a white screen when I include this statement in the sketch:
// Look for new cards, and select one if present
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50);
return;
}
It seems that this statement, which is testing for the presence of a readable card, works fine with the serial monitor but somehow stops the Arduino from communicationg with the TFT.
What exactly is going wrong here and/or is there another way of testing for the presence of a card that the above "if statement"?
For completeness the cutdown sketch is below. If the commented out if statement is included the TFT shows nothing but a white screen. If it is commented out the screen show the message.
#include <Wire.h>
#include "MFRC522_I2C.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <MCUFRIEND_kbv.h> // Hardware-specific library
MFRC522 mfrc522(0x28); // Create MFRC522 instance.
MCUFRIEND_kbv tft; // Create tft instance.
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define WHITE 0xFFFF
#define GREY 0x8410
struct CardName {
const char* cardId;
const char* userName;
}
registeredCards [] {
{"214:136:105:109", "One"},
{"006:159:111:109", "Two"},
{"059:173:223:018", "Three"},
{"059:173:223:015", "Four"}
};
const int nCards = sizeof registeredCards / sizeof 0 [registeredCards];
void setup() {
Serial.begin(9600);
Wire.begin(); // Initialize I2C
mfrc522.PCD_Init(); // Init MFRC522
Serial.println("Scanning for a card...");
uint16_t ID = tft.readID();
Serial.println("Example: Font_simple");
Serial.print("found ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; //force ID if write-only display
tft.begin(ID);
tft.setRotation(0);
}
void loop() {
tft.fillScreen(BLACK);
showmsgXY(10, 80, 1, &FreeSans12pt7b, "Place card over RFID");
// Look for new cards, and select one if present
/* if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
tft.fillScreen(BLACK);
showmsgXY(10, 80, 1, &FreeSans12pt7b, "Place card over RFID");
//return;
}*/
}
void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg) {
int16_t x1, y1;
uint16_t wid, ht;
tft.drawFastHLine(0, y, tft.width(), WHITE);
tft.setFont(f);
tft.setCursor(x, y);
tft.setTextColor(GREEN);
tft.setTextSize(sz);
tft.print(msg);
delay(2000);
}