I have added a Serial.print into the RTCNow code to show the RTC seconds so I can see the RTC code is running from the loop, it just isnt displaying. I've also added a RTC test into the sketch to check its running as well and its fine,
RTC is on I2C (20/21) on mega.
full code is;
#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;//was 3
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);
tft.fillScreen(BLACK);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(__DATE__, __TIME__));
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(0, 5);
tft.print(" Clock ");
delay(3000);
tft.fillScreen(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()
{
Serial.println ("loop running");
RTCNow();
readRfid();
powerpinRead = (success);
}
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();
tft.setTextSize(2);
tft.setTextColor(MAGENTA, BLACK);
tft.setCursor(0, 0);
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 == now.second())//do more of these IF's to save time on days and hours etc. not printing
{
return;
}
secs = now.second();
if ( now.second()<10 ) tft.print("0");
tft.print(now.second(), DEC);
tft.setTextSize(1);
tft.setCursor(0, 18);
tft.print(now.day(), DEC);
tft.print("-");
tft.print(now.month(), DEC);
tft.print("-");
tft.print(now.year(), DEC);
tft.setCursor(0, 30);
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.print(" "); // erase any previous date string that was longer
Serial.println (secs);//test
}