DS1302 RTC help

I’ve been trying to link the RTC module to the 1306 OLED. I have another program I used to test the OLED so I know it is all OK but when I run this code the OLED sticks on the ADAfruit logo and I cant see why??

// DS1302:  CE pin    -> Arduino Digital 27
//          I/O pin   -> Arduino Digital 29
//          SCLK pin  -> Arduino Digital 31
//          VCC pin   -> Arduino Digital 33
//          GND pin   -> Arduino Digital 35
//

#include <SPI.h> 
#include <Wire.h> 
#include <TimeLib.h>
#include <Time.h>
#include <DS1302RTC.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

   // If using software SPI (the default case): 
  #define OLED_MOSI  11   //D1 
  #define OLED_CLK   12   //D0 
  #define OLED_DC    9 
  #define OLED_CS    8 
  #define OLED_RESET 10 
  
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); 

// Init the DS1302
// Set pins:  CE, IO,CLK
 DS1302RTC RTC(27, 29, 31);

// Optional connection for RTC module
#define DS1302_GND_PIN 33
#define DS1302_VCC_PIN 35

void setup()
{
   
    display.begin(SSD1306_SWITCHCAPVCC); 
    display.display(); 
    delay(5000); 
    display.clearDisplay(); 
    display.setTextSize(1);
    display.setCursor(0,0); 
    display.print("Display test");
     
  
   // Activate RTC module
   digitalWrite(DS1302_GND_PIN, LOW);
   pinMode(DS1302_GND_PIN, OUTPUT);

   digitalWrite(DS1302_VCC_PIN, HIGH);
   pinMode(DS1302_VCC_PIN, OUTPUT);

    delay(500);

   // Check clock oscillation  
   display.clearDisplay();
   if (RTC.haltRTC())
     display.print("Clock stopped!");
  else
     display.print("Clock working.");

   // Check write-protection
   display.setCursor(0,1);
  if (RTC.writeEN())
    display.print("Write allowed.");
  else
     display.print("Write protected.");

   delay ( 2000 );

   // Setup Time library  
   display.clearDisplay();
   display.print("RTC Sync");
   setSyncProvider(RTC.get); // the function to get the time from the RTC
   if(timeStatus() == timeSet)
     display.print(" Ok!");
   else
     display.print(" FAIL!");

   delay ( 2000 );

   display.clearDisplay();

}


void loop()
{

   // Display time centered on the upper line
   display.setCursor(3, 0);
   print2digits(hour());
  display.print("  ");
   print2digits(minute());
   display.print("  ");
   print2digits(second());

   // Display abbreviated Day-of-Week in the lower left corner
   display.setCursor(0, 1);
   display.print(dayShortStr(weekday()));

   // Display date in the lower right corner
   display.setCursor(5, 1);
   display.print(" ");
   print2digits(day());
   display.print("/");
   print2digits(month());
   display.print("/");
   display.print(year());

   // Warning!
   if(timeStatus() != timeSet) {
     display.setCursor(0, 1);
     display.print(F("RTC ERROR: SYNC!"));
  }

   delay ( 1000 ); // Wait approx 1 sec
}

void print2digits(int number) {
//   // Output leading zero
   if (number >= 0 && number < 10) {
     display.write('0');
  }
   display.print(number);
}

Have you tested the RTC? Somewhere, somehow, someone has to set the time on the RTC. I don't see anything in your code that does that so any timing would kind of be left hanging as a discharged RTC running from a Unix epoch base would think it's midnight, January 1, 1970. Might take a while before 6 o'clock tonight kicks in.

Good point but my knowledge is limited as this is my first RTC project.

WIll see what I can find. Thanks for the pointer.

There are two built-in macros in the gcc compiler that return the host (PC) time when the code is compiled. They can be defined as,

const char DATE[12] = DATE;
const char TIME[9] = TIME;

and then parsed for values. DATE is yyyy-mm-dd, TIME is HH:mm:ss

You can add an adjustment of a couple seconds to account for compile/load time or tweak it later through some menu function.