I have built the following code and while the display does its test startup in setup and works fine, it wont display again as the pages are selected through the counter.
I can see the counter is incrementing.
I have tried adding tft.begin again within the sub routines but still no display.
Also if I just run RTC subroutine in the loop on its own it still doesnt write to the OLED.
The RFID works fine.
#include <SPI.h>
#include <PN532_SPI.h>
#include "PN532.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include "RTClib.h"
#define sclk 52
#define mosi 51
#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
PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);
Adafruit_SSD1351 tft = Adafruit_SSD1351(cs, dc, rst);
RTC_DS3231 rtc;
boolean success;
String cardNum;
//unsigned long RFID;
int powerPin = 13;
int powerpinRead;
int prevpowerpinRead = LOW;
int powerpinState = LOW;
long time = 0;
long debounce = 200;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
uint8_t secs = 0;
int dispChngpin = 9;
int dispChngpinState;
int buttonPushCounter = 1;//sets first page at startup
int lastdispChngState =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(" 9");
delay (2000);
pinMode (dispChngpin,INPUT);
pinMode(powerPin, OUTPUT);
uint16_t time = millis();
tft.fillRect(0, 0, 128, 128, BLACK);
time = millis() - time;
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
nfc.setPassiveActivationRetries(0x01);
nfc.SAMConfig();
if (! rtc.begin())
{
while (1);
}
rtc.adjust(DateTime(__DATE__, __TIME__));
}
void loop()
{
readRfid();//runs RTCNow if on
powerpinRead = (success);
}
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
}
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("Found a card!");
for (uint8_t i=0; i < uidLength; i++)
{
// Serial.print(" 0x");Serial.print(uid[i], HEX);
}
}
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(); //test around changeDisplay
changeDisplay();
}
else
{tft.fillScreen(BLACK);
}
Serial.println (powerpinState);
}
void changeDisplay()
{
dispChngpinState = digitalRead(dispChngpin);
//Serial.println (dispChngpinState);
Serial.println (buttonPushCounter);
if (dispChngpinState != lastdispChngState)
{
if (dispChngpinState == HIGH)
{
buttonPushCounter++;
delay(100);
}
}
lastdispChngState = dispChngpinState;
if (buttonPushCounter == 1)
{
RTCNow();
}
if (buttonPushCounter == 2)
{
tft.fillScreen(BLACK);
buttonPushCounter = 3;
}
if (buttonPushCounter == 3)
{
tft.setCursor(30, 5);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("Suzuki");
tft.setCursor(55,40);
tft.setTextSize(4);
tft.println("7");
}
if (buttonPushCounter == 4)
{
tft.fillScreen(BLACK);
buttonPushCounter = 5;
}
if (buttonPushCounter == 5)
{
tft.setCursor(30, 5);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.println("Fuel");
tft.setTextSize(4);
tft.println("Gauge");
}
if (buttonPushCounter == 6)
{
tft.fillScreen(BLACK);
buttonPushCounter = 1;
}
delay(1000);
}