Trouble with SPI, SD and RTC

G'day, I am using a Nano 33 IOT and using Adafruit DS3231 RTC, DFrobot V1.0 SD card and a 0.9" I2C lcd screen. These devices work using the example sketches on their own ie CardInfo, or RTClib etc. However when you try to integrate the RTC with the SD card it fails.

I have had no success with RTClib.h; marginal success with RTCZero.h - that worked until I tried to save data to an SD card file. Can anyone please tell me where I am going wrong?

I get compile errors as follows. (these are the last few) - Bottom of the list are the Library Versions used.

C:\Users\PaulB\Documents\Arduino\Sept5\Copy_CardInfo2\Copy_CardInfo2\Copy_CardInfo2.ino: In function 'void setup()':
Copy_CardInfo2:58:12: error: 'class Sd2Card' has no member named 'begin'
if (card.begin(chipSelect)) { // if (!card.init(SPI_HALF_SPEED, chipSelect)) {NO LONGER works
^~~~~
C:\Users\PaulB\Documents\Arduino\Sept5\Copy_CardInfo2\Copy_CardInfo2\Copy_CardInfo2.ino: In function 'void loop()':
Copy_CardInfo2:186:8: error: 'class Sd2Card' has no member named 'begin'
card.begin(chipSelect);
^~~~~
Copy_CardInfo2:187:22: error: 'class Sd2Card' has no member named 'open'
File myFile = card.open("mytest1.txt", FILE_WRITE); //SD.open("mytest1.txt", FILE_WRITE);
^~~~
Copy_CardInfo2:192:5: error: 'myfile' was not declared in this scope
myfile.println(dataString);
^~~~~~
C:\Users\PaulB\Documents\Arduino\Sept5\Copy_CardInfo2\Copy_CardInfo2\Copy_CardInfo2.ino:192:5: note: suggested alternative: 'myFile'
myfile.println(dataString);
^~~~~~
myFile
Using library SPI at version 1.0 in folder: C:\Users\PaulB\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\libraries\SPI
Using library SD at version 1.2.4 in folder: C:\arduino-1.8.13\libraries\SD
Using library WiFiNINA at version 1.8.13 in folder: C:\Users\PaulB\Documents\Arduino\libraries\WiFiNINA
Using library RTCZero at version 1.6.0 in folder: C:\Users\PaulB\Documents\Arduino\libraries\RTCZero
Using library ArduinoBLE at version 1.2.1 in folder: C:\Users\PaulB\Documents\Arduino\libraries\ArduinoBLE
exit status 1
'class Sd2Card' has no member named 'begin'

/***************************Start of code

/*
  SD card test

  This example shows how use the utility libraries on which the'
  SD library is based in order to get info about your SD card.
  Very useful for testing a card when you're not sure whether its working or not.

  The circuit:
    SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
 ** CS - depends on your SD card shield or module.
     Pin 4 used here for consistency with other Arduino examples


  created  28 Mar 2011
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>
#include <WiFiNINA.h>
#include <RTCZero.h>
#include <ArduinoBLE.h>

//#include <RTClib.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
//File myFile;
RTCZero rtc;

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;

void setup() {
  pinMode(chipSelect, OUTPUT);
  // Open serial communications and wait for port to open:
  SerialUSB.begin(115200);
  while (!SerialUSB) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  SerialUSB.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (card.begin(chipSelect)) {            //  if (!card.init(SPI_HALF_SPEED, chipSelect)) {NO LONGER works
    SerialUSB.println("initialization failed. Things to check:");
    SerialUSB.println("* is a card inserted?");
    SerialUSB.println("* is your wiring correct?");
    SerialUSB.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } 
  else {
    SerialUSB.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  SerialUSB.println();
  SerialUSB.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      SerialUSB.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      SerialUSB.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      SerialUSB.println("SDHC");
      break;
    default:
      SerialUSB.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {     //  if (!volume.init(card)) {
    SerialUSB.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }

  SerialUSB.print("Clusters:          ");
  SerialUSB.println(volume.clusterCount());
  SerialUSB.print("Blocks x Cluster:  ");
  SerialUSB.println(volume.blocksPerCluster());

  SerialUSB.print("Total Blocks:      ");
  SerialUSB.println(volume.blocksPerCluster() * volume.clusterCount());
  SerialUSB.println();

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  SerialUSB.print("Volume type is:    FAT");
  SerialUSB.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1KB)
  SerialUSB.print("Volume size (Kb):  ");
  SerialUSB.println(volumesize);
  SerialUSB.print("Volume size (Mb):  ");
  volumesize /= 1024;
  SerialUSB.println(volumesize);
  SerialUSB.print("Volume size (Gb):  ");
  SerialUSB.println((float)volumesize / 1024.0);

  SerialUSB.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

//*************************************
// //Comment out this block and the rest of the uncommented code works.
rtc.begin();
//  if (! rtc.begin()) {
//    SerialUSB.println("Couldn't find RTC");
//    SerialUSB.flush();
//    abort();
//  }
  //**************************
//   if (rtc.lostPower()) {
//    SerialUSB.println("RTC lost power, let's set the time!");
//    // When time needs to be set on a new device, or after a power loss, the
//    // following line sets the RTC to the date & time this sketch was compiled
//    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//    // This line sets the RTC with an explicit date & time, for example to set
//    // January 21, 2014 at 3am you would call:
//    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  byte hour = rtc.getHours();
  byte minute = rtc.getMinutes();
  byte second = rtc.getSeconds(); // Get the second
  byte day = rtc.getDay(); // Get the day
  byte month = rtc.getMonth(); // Get the month
  byte year = rtc.getYear(); // Get the year

}   
// void setup() end

float Temp = 10;
bool Exit = false;
int loopCount = 5;


void loop(void) {
    SerialUSB.println("In main LOOP: ");  
    SerialUSB.print("Temperature is: ");  
    SerialUSB.println(Temp); 

//    DateTime now = rtc.now();
    String dataString = "";
    String strDate = "";
    String strTime = "";
    String csvData = "";
//    String Temp = "";
//    strDate += String(now.year()) + "/" + String(now.month()) + "/" + String(now.day());
//    strTime += String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second());
    strDate += String(rtc.getYear()) + "/" + String(rtc.getMonth()) + "/" + String(rtc.getDay());
    strTime += String(rtc.getHours()) + ":" + String(rtc.getMinutes()) + ":" + String(rtc.getSeconds());
    dataString = strDate + ", " + strTime;      //String(now());
    dataString += ", ";

    SerialUSB.println(dataString);

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  card.begin(chipSelect);
  File myFile = card.open("mytest1.txt", FILE_WRITE);  //SD.open("mytest1.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    SerialUSB.print("Writing to test.txt...");
    myfile.println(dataString);
    
//    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
//    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
   delay(10000); // wait 10 seconds
//    for (int x = 1; x <= loopCount; x++) {
//      SerialUSB.print("In FOR LOOP: x is: ");  
//      SerialUSB.println(x);  
//      
//      Temp = Temp + 2;
//      SerialUSB.print("Temperature is: ");  
//      SerialUSB.println(Temp);  
//      if (x == loopCount) {
//        Exit = true;
//      }
//    }
//    if (Exit == true) exit(0);
}

/******************************end of code

Can you please edit your opening post, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

sterretje, thank you for your time, I am new to this.