RTC error message "Not declared in scope"

I have set up a temperature data logger with SD card on an Uno which works. Now I need to include an RTC for time stamps and well as logging at specific times of the day.
For the past week I have tried to add a DS1302 RTC.
I used a new sketch for the RTC on the same board with the data logger components connected. This worked as planned and gave me real time readings.
I inserted the RTC sketch into the Data logger sketch and it crashed.
I then copied parts of the RTC sketch until it crashed again.
My sketch refers:
The error occured after Line 40 RtcDateTime compiled = RtcDateTime(DATE, TIME);
When I used "printDateTime(compiled);" on line 41 I received the Error: "'printDateTime' was not declared in this scope"
Every attempt to access the Date time is foiled. Every working examples I have looked at with "DateTime now = rtc.now();" gives me "Not declared in this scope"
I have only been working with Arduino for 2 months so still learning.
Please point me in the right direction.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library
//for RTC
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(4, 5, 2); // RTC Data pin 4, SCLK pin 5, CE pin 2
RtcDS1302 Rtc(myWire);

// Data wire is plugged into digital pin (was 2) now 7 on the Arduino
#define ONE_WIRE_BUS 7

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
OneWire ds(7); //changed from 2 for RTC
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

float tempSensor1, tempSensor2, tempSensor3, tempSensor4, tempSensor5;
// Addresses of DS18B20s
uint8_t sensor1[8] = { 0x28, 0x28, 0xF6, 0x75, 0xD0, 0x01, 0x3C, 0xDA };
uint8_t sensor2[8] = { 0x28, 0x9D, 0x9C, 0x76, 0xE0, 0x01, 0x3C, 0xCC };
uint8_t sensor3[8] = { 0x28, 0x47, 0x2F, 0x75, 0xD0, 0x01, 0x3C, 0x90 };
uint8_t sensor4[8] = { 0x28, 0x9E, 0x42, 0x75, 0xD0, 0x01, 0x3C, 0x3F };
uint8_t sensor5[8] = { 0x28, 0xBF, 0x1C, 0x75, 0xD0, 0x01, 0x3C, 0x89 };

int chipSelect = 8; //chipSelect pin for the SD card Reader - Changed from 4 to cater for RTC
File mySensorData; //Data object for writing sesnor data to

void setup()
{
  Serial.begin(9600);
  Serial.print("compiled: ");
  Serial.print(DATE);
  Serial.println(TIME);
  Rtc.Begin();
  RtcDateTime compiled = RtcDateTime(DATE, TIME);
  printDateTime(compiled); //ERROR MESSAGE printDateTime' was not declared in this scope
  Serial.println();
  sensors.begin();
  SD.begin(8); //Initialize the SD card reader
}
void loop()
{
  sensors.requestTemperatures();
  tempSensor1 = sensors.getTempC(sensor1); // Ambient temperature
  tempSensor2 = sensors.getTempC(sensor2); // Inlet temperature
  tempSensor3 = sensors.getTempC(sensor3); // Inside Container temperature
  tempSensor4 = sensors.getTempC(sensor4); // Top of Container temperature
  tempSensor5 = sensors.getTempC(sensor5); // Top of Container temperature
  Serial.print(" Inside_container:");
  Serial.print(tempSensor3);
  Serial.print(" Top_container:");
  Serial.print(tempSensor4);
  Serial.print(" Top_Chimney:");
  Serial.print(tempSensor5);
  Serial.println("");
  delay(5000);
  mySensorData = SD.open("PTData.txt", FILE_WRITE);
  if (mySensorData)
  {
    mySensorData.print(tempSensor3);          //write temperature 1 data to card
    mySensorData.print(",");                  //write a commma
    mySensorData.print(tempSensor4);
    mySensorData.print(",");
    mySensorData.println(tempSensor5);
    mySensorData.close();                    //close the file
  }

Please read How to get the best out of this forum and add code tags to your posted code to make it easier to read and copy and post the full error message copied from the IDE using the "Copy error message" button and paste that code tags too

tempSensor1 = sensors.getTempC(sensor1); // Ambient temperature

What does the error message actually say?

You are using variables such as tempSensor1 etc but I cannot see them declared anywhere. You have to declare a variable before you use it.

Please use code tags </> when posting code.

[PB edit]
Ok, so I now see tempSensor1 etc, were they always there and I missed them? Quite possible!

Where in the code is the printDateTime() function declared ?

I have added code tags to your original post

I would have a look at the examples provided with the RTC library and see if that runs ok , then look carefully at the syntax/modifications you’ve made to see if you’ve a mistake.
( I’d modify and save changes to the example ( as a different file name)as you go )

Thank you.
That is my problem. When it was in the original RTC sketch it was not asked for. As soon as I included it into the logger sketch, everything I try including rtc.(now) gets thrown out. "Not declared in this scope"

Just click on the pencil at the bottom of your post to edit .

Make one change at a time and check the result

That is the path I followed.
I looked at 10s of sketches.
My sketch for the RTC works perfectly.
I eventually added line by line from the RTC sketch, copying and pasting into the Logger sketch until I reached the error line.
There appears to be a programme clash

I assume that the RTC sketch has the printDateTime() function in it. You will get nowhere adding one line at a time from the RTC sketch when what you add depends on something that you have not yet added

A major requirement when merging sketches is that you understand how each of them works

I’d also think this looks suspicious ….

Maybe … print (DateTime());

Or something like that ???? ( I’m on me phone ); if it says it’s not declared , to me it suggests the command doesn’t exist or the syntax is wrong .
Go back to an example and add that line you have and see if it compiles ?

Other libraries are around for those clocks , incidentally the DS3231 is far more accurate

Everything such as printDateTime() , print (DateTime()), rtc.now() gets thrown out. Anything trying to read the RTC in date time format.
Yet it works when it is by itself in the identical physical environment (All pins identified and connected).
I can only read the RTC in that strange number format that increments in seconds but is gibberish otherwise.
In trying to understand it, any idea what the statement RtcDS1302(LT symbol)ThreeWire(GT symbol) Rtc(myWire); just after the #include statements does?

If you can post the code that works, one of us may be able to spot the problem.

I just download this onto the UNO in place of the logger sketch and get this - perfect!
compiled: Jul 4 202117:33:09
2021/07/04 17:33:09
RTC is newer than compile time. (this is expected)
2021/07/04 17:35:59
2021/07/04 17:36:04

// DS1302 CLK/SCLK - Pin5-> 5
// DS1302 DAT/IO -Pin3->
// DS1302 RST/CE -Pin6-> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <ThreeWire.h>
#include <RtcDS1302.h>

ThreeWire myWire(4, 5, 2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

void setup ()
{
  Serial.begin(9600);

  Serial.print("compiled: ");
  Serial.print(__DATE__);
  Serial.println(__TIME__);

  Rtc.Begin();

  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  printDateTime(compiled);
  Serial.println();

  if (!Rtc.IsDateTimeValid())
  {
    // Common Causes:
    //    1) first time you ran and the device wasn't running yet
    //    2) the battery on the device is low or even missing

    Serial.println("RTC lost confidence in the DateTime!");
    Rtc.SetDateTime(compiled);
  }

  if (Rtc.GetIsWriteProtected())
  {
    Serial.println("RTC was write protected, enabling writing now");
    Rtc.SetIsWriteProtected(false);
  }

  if (!Rtc.GetIsRunning())
  {
    Serial.println("RTC was not actively running, starting now");
    Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled)
  {
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
    Rtc.SetDateTime(compiled);
  }
  else if (now > compiled)
  {
    Serial.println("RTC is newer than compile time. (this is expected)");
  }
  else if (now == compiled)
  {
    Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  }
}

void loop ()
{
  RtcDateTime now = Rtc.GetDateTime();

  printDateTime(now);
  Serial.println();

  if (!now.IsValid())
  {
    // Common Causes:
    //    1) the battery on the device is low or even missing and the power line was disconnected
    Serial.println("RTC lost confidence in the DateTime!");
  }

  delay(5000); // five seconds
}

#define countof(a) (sizeof(a) / sizeof(a[0]))

void printDateTime(const RtcDateTime& dt)
{
  char datestring[20];

  snprintf_P(datestring,
             countof(datestring),
             PSTR("%02u/%02u/%02u %02u:%02u:%02u"),
             dt.Year(),
             dt.Month(),
             dt.Day(),
             dt.Hour(),
             dt.Minute(),
             dt.Second() );
  Serial.print(datestring);
}

Preformatted text

I am beginning to think I need take this original RTC sketch and introduce parts of the logger code bit by bit.
Is it possible that one of the libraries is countermanding the read instruction?

If you want to use printDateTime(), you will need to include the function that is at the bottom of the RTC sketch you posted, as well as any variables or other functions that are used within this function.

void printDateTime(const RtcDateTime& dt)
{
  char datestring[20];

  snprintf_P(datestring,
             countof(datestring),
             PSTR("%02u/%02u/%02u %02u:%02u:%02u"),
             dt.Year(),
             dt.Month(),
             dt.Day(),
             dt.Hour(),
             dt.Minute(),
             dt.Second() );
  Serial.print(datestring);
}

Similarly, you have tried to use DATE whereas in the program that works you have

 __DATE__

Same thing for time.

As I said previously you will get nowhere by adding one sketch to another bit by bit because of dependencies on code that you have not yet added

Do you understand how each sketch works ?

As i said, I am very new at this and learning daily.
I think I understand the logger sketch but hav'nt delved into the libraries such as RtcDS1302
I copied and pasted the RTC sketch in the IDE from top to bottom and pasted it.
The last statement has the words (datetestring) by the curly brackets on the line below.
Where would the additional information be hiding?

wildbill - I copied this RTC sketch from the internet and it works. Don't know why the author added the underscores.
The point is that, in the logger sketch, this instruction works and reports it correctly on the com port,
In contrast, as soon as the logger gets to line 24 with printDateTime it throws it out with "not declared in scope" where as the RTC sketch continues on.

__DATE__ is a pre-defined macro that tells the compiler pre-processor to insert a string constant containing the current date when the sketch is being compiled.