I have a part of a project that uses a RFID reader and Oled, (both on SPI) and a RTC on I2C.
The RFID and OLED code work well together and if I run the OLED and RTC together in another sketch they work Ok together, but when I combine all three and call the RTC subroutine, the display just goes blank.
I cant see why calling the RTC makes it crash.
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <PN532_SPI.h>
#include "PN532.h"
#include "RTClib.h"
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
RTC_DS3231 rtc;
#define dc A2
#define cs 53
#define rst 4
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, rst);
boolean success;
String cardNum;
//unsigned long RFID;
int powerPin = 13;
int powerpinRead;
int prevpowerpinRead = LOW;
int powerpinState = LOW;
long time = 0;
long debounce = 200;
float p = 3.1415926;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
uint8_t secs = 0;
void setup(void) {
Serial.begin(9600);
tft.begin();
tft.fillRect(0, 0, 128, 128, BLACK);
tft.setCursor(30, 5);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("Suzuki");
tft.setTextSize(4);
tft.println(" 112");
delay (2000);
// tft.fillRect(0, 0, 128, 128, BLACK);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
nfc.setPassiveActivationRetries(0x01);
nfc.SAMConfig();
pinMode(powerPin, OUTPUT);
uint16_t time = millis();
// tft.fillRect(0, 0, 128, 128, BLACK);
time = millis() - time;
}
void loop()
{
readRfid();
powerpinRead = (success);
RTCNow();
}
void readRfid()
{
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
//Serial.println(success);
Serial.println("Found a card!");
for (uint8_t i=0; i < uidLength; i++)
{
// Serial.print(" 0x");Serial.print(uid[i], HEX);
}
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
delay(1000);
if (powerpinRead == HIGH && prevpowerpinRead == LOW && millis() - time > debounce)
{
if (powerpinState == HIGH)
powerpinState = LOW;
else
powerpinState = HIGH;
time = millis();
}
digitalWrite(powerPin, powerpinState);
prevpowerpinRead = powerpinRead;
if (powerpinState == HIGH|| powerpinState == 0x11)
{
RTCNow();
}
Serial.println (powerpinState);
}
void RTCNow()
{
DateTime now = rtc.now();
if (secs == now.second())//do more for hrs etc to save time
{
return;
}
secs = now.second();
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(12,7);
tft.print(" Clock ");
tft.setTextSize(2);
tft.setTextColor(MAGENTA, BLACK);
tft.setCursor(15, 25);
if ( now.hour()<10 ) tft.print(" ");
tft.print(now.hour(), DEC);
tft.print(":");
if ( now.minute()<10 ) tft.print("0");
tft.print(now.minute(), DEC);
tft.print(":");
if ( secs <10 ) tft.print("0");//previously uses second.now()but this should speed it up?
tft.print(secs, DEC);
tft.setTextSize(1);
tft.setCursor(30, 45);
tft.print(now.day(), DEC);
tft.print("-");
tft.print(now.month(), DEC);
tft.print("-");
tft.print(now.year(), DEC);
tft.setCursor(17, 55);
tft.setTextSize(2);
tft.setTextColor(GREEN, BLACK);
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.print(" "); // erase any previous date string that was longer
}



