Hi,
I know this should work :), but connecting an RFID reader and a TFT screen both to the SPI bus seems very erratic. I am using an Arduino MKR WiFi 1010 board and the MFRC_fixed library:
Individually each piece of code works fine; however, when I combine code to print to the TFT screen and code to read the UID tag of the RFID cards it is a bit more hit and miss! Sometimes it works as it should - but sometimes it seems to need a reset of the board to work (i.e. it doesn't work immediately after the code had been loaded on, but if I then reset the board it does work).
Code for minimal (sometimes) working example:
// LIBRARY IMPORTS
// include the necessary libraries for the LCD
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// include the RFID card library
#include <MFRC522.h>
// INITIAL VARIABLES
// RFID
// set the additional SPI pins for the rfid board
#define MFC_CS 4
#define MFC_RS 3
// create the rfid object
MFRC522 rfid(MFC_CS, MFC_RS);
// DISPLAY
// set the additional SPI pins for the TFT break out
#define TFT_DC 6
#define TFT_CS 7
#define TFT_RS 5
// create the tft object using the hardware SPI pins
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RS);
// CODE
// this method runs once (when the sketch starts)
void setup()
{
// start serial comms and wait until its up and running
Serial.begin(115200);
while (!Serial)
{
delay(10);
}
Serial.println("LCD hello world program running ...");
// start the rfid reader
SPI.begin();
rfid.PCD_Init();
// start the tft screen and set it up
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
// rotate the display, reset the cursor to the top left, and write
// a header to it
tft.setRotation(1);
tft.setCursor(0, 0);
tft.println(" embedded lcd hello world ");
}
// this method loops continuously
void loop()
{
// check the rfid card
read_rfid();
}
// UTILITY METHODS
// read rfid card and print uid
void read_rfid()
{
// check there is a card present and if so read the card data
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial())
{
// get the uid as a byte array and the number of bytes that make up that
// array
byte *buffer = rfid.uid.uidByte;
byte buffer_size = rfid.uid.size;
// print the uid of the card to the serial monitor
Serial.print("DEBUGGING: ");
Serial.print("card: ");
for (byte i = 0; i < buffer_size; i++)
{
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
Serial.println();
// halt PICC
rfid.PICC_HaltA();
// stop encryption on PCD
rfid.PCD_StopCrypto1();
}
}
Sometimes this will work immediately after it has been uploaded, but often it takes a reset for the RFID reader to detect a card. I'm guessing there is some sort of timing issue when the program starts but I've tried putting various delays around bits in the set-up function and haven't managed to get anything that works reliably (because it is intermittent sometimes I think I've cracked it ... only for it to stop working the next time I upload the code!).
Any advice gratefully received!
Regards,
Alex