Refreshing the oled repeatedly causes it to crash (go black or white)

Using this OLED HILetgo ILI9341 SPI TFT LCD Display Touch Panel.

Here is my sketch .. it works fine for a few seconds then goes black ( or sometimes white )
Any help would be appreciated

#include <ArduinoBLE.h>
#include <WiFiNINA.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <RTCZero.h>

#define TFT_DC 6
#define TFT_CS 7

RTCZero rtc;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

unsigned long last;

void setup() {

Serial.begin(9600);

tft.begin();
tft.setRotation(2);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
delay(500);

rtc.begin();
rtc.setEpoch(1637318416);

}

void loop() {

{
   char datetime[18];
   sprintf( datetime, "%02d/%02d/%02d %02d:%02d:%02d", rtc.getMonth(), rtc.getDay(), rtc.getYear(),
   rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());

   
   tft.setCursor(0, 300);
   tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK); 
   tft.print(datetime);
   yield();
   delay(200);

   
}

}

Your datetime array is too short - you forgot to allow one more character for the terminating null.

Thanks for your reply. I corrected that but I'm still having this issue - I tried other text with similar results.

-David

For example this sketch has the same results

#include <ArduinoBLE.h>
#include <WiFiNINA.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <RTCZero.h>

#define TFT_DC 6
#define TFT_CS 7

RTCZero rtc;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

long randNumber;

void setup() {

Serial.begin(9600);

tft.begin();
tft.setRotation(2);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
delay(500);

// rtc.begin();
// rtc.setEpoch(1637318416);

randomSeed(analogRead(0));

}

void loop() {

{

// char datetime[18];
// sprintf( datetime, "%02d/%02d/%02d %02d:%02d:%02d", rtc.getMonth(), rtc.getDay(), rtc.getYear(),
// rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
//

    randNumber = random(300);
   
   tft.setCursor(0, 300);
   tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK); 
   //tft.print(datetime);
   tft.print(randNumber);
   yield();
   delay(200);

   
}

}

What is the yield for?

just an attempt at getting it to work .. I thought maybe it was blocking the cpu or something .. saw it in some example code ..

Might be a bad board .. I got a new one and the problem seems to go away.

Might have been a wiring issue that you inadvertently fixed when you swapped parts. Did you replace the Arduino or the display?

You are correct .. I had some wiring for the touch sensor attached to the SPI wires that was (unpredictably ) causing the main LCD to fail ..