I wrote an earlier post trying to figure out how to use a colour OLED and a RFID module on a hardware SPI.
In the end it seemed aside from possible level shifter issue there were also library issue which are beyond my current skills.
So I decided to try to run a hardware and a software SPI side by side. It uses more pins but it does what I need.
There is a software SPI library for the MRC522 RFID and a Digital I/O library to run the pins.
The result is below for those still struggling.....
/**
---------------------------------------------------------------------------------------
* Pin layout:
SOFT_SPI_MISO_PIN = 5 ----- can be changed in the RFID.cpp file
SOFT_SPI_MOSI_PIN = 6 ----- can be changed in the RFID.cpp file
SOFT_SPI_SCK_PIN = 7 ----- can be changed in the RFID.cpp file
SOFT_SPI_SS_PIN: Pin 8 ----- can be changed in the code below
SOFT_SPI_RST_PIN: Pin 4 ----- can be changed in the code below
*/
#include "RFID.h"
#include <DigitalIO.h> // our software SPI library
#define SS_PIN 8
#define RST_PIN 4
RFID rfid(SS_PIN, RST_PIN); //create an instance rfid for the class RFID
// varables to store data
String cardNum;
unsigned long RFID;
// These are OLED hardware spi
#define sclk 52
#define mosi 51
#define dc 15
#define cs 53//OCS on big display
#define rst 2
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
//#include <SPI.h> //Don't seem to need this to make OLED work?
Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, mosi, sclk, rst);
float p = 3.1415926;
void fillpixelbypixel(uint16_t color) {
for (uint8_t x=0; x < tft.width(); x++) {
for (uint8_t y=0; y < tft.height(); y++) {
tft.drawPixel(x, y, color);
}
}
delay(100);
}
void setup(void) {
Serial.begin(9600);
Serial.println("initilizing RFID...");
rfid.init(); // initilize the RFID module
Serial.println("start ");
Serial.println("Initialise RFID!");
tft.begin();
Serial.println("start");
uint16_t time = millis();
tft.fillRect(0, 0, 128, 128, BLACK);
time = millis() - time;
// Serial.println(time, DEC);
delay(500);
}
void tftPrintTest() {
tft.fillScreen(BLACK);
tft.setCursor(0, 5);
tft.setTextColor(RED);
tft.setTextSize(1);
tft.println("Hello Kevin and Mandy!");
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.println("Its nice to be alive!");
// tft.setTextColor(BLUE);
// tft.setTextSize(3);
// tft.print(1234.567);
delay(1500);
tft.setCursor(0, 5);
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(0);
tft.println("Hello World!");
tft.setTextSize(1);
tft.setTextColor(GREEN);
tft.print(p, 6);
tft.println(" Want pi?");
tft.println(" ");
tft.print(8675309, HEX); // print 8,675,309 out in HEX!
tft.println(" Print HEX!");
tft.println(" ");
tft.setTextColor(WHITE);
tft.println("Sketch has been");
tft.println("running for: ");
tft.setTextColor(MAGENTA);
tft.print(millis() / 1000);
tft.setTextColor(WHITE);
tft.print(" seconds.");
delay(500);
}
void lcdTestPattern(void)
{
uint32_t i,j;
tft.goTo(0, 0);
for(i=0;i<128;i++)
{
for(j=0;j<128;j++)
{
if(i<16){
tft.writeData(RED>>8); tft.writeData(RED);
}
else if(i<32) {
tft.writeData(YELLOW>>8);tft.writeData(YELLOW);
}
else if(i<48){tft.writeData(GREEN>>8);tft.writeData(GREEN);}
else if(i<64){tft.writeData(CYAN>>8);tft.writeData(CYAN);}
else if(i<80){tft.writeData(BLUE>>8);tft.writeData(BLUE);}
else if(i<96){tft.writeData(MAGENTA>>8);tft.writeData(MAGENTA);}
else if(i<112){tft.writeData(BLACK>>8);tft.writeData(BLACK);}
else {
tft.writeData(WHITE>>8);
tft.writeData(WHITE);
}
}
}
}
void loop() {
lcdTestPattern();
delay(500);
tftPrintTest();
delay(500);
readRfid();
printRfid();
}
void readRfid()
{
if (rfid.isCard())
{
if (rfid.readCardSerial())
{
for (int i=0; i<=4; i++)//card value: "xyz xyz xyz xyz xyz" (15 digits maximum; 5 pairs of xyz)hence 0<=i<=4 //
{
RFID = rfid.serNum[i];
cardNum += RFID; // store RFID value into string "cardNum" and concatinate it with each iteration
}
}
}
rfid.halt();
}
void printRfid()
{
if (cardNum != '\0')//if string cardNum is not empty, print the value
{
Serial.println("Card found");
Serial.print("Cardnumber: ");
Serial.println(cardNum);
cardNum.remove(0);
//This is an arduino function.
//remove the stored value after printing. else the new card value that is read
// will be concatinated with the previous string.
delay(500);
}
}