Adding RTC to existing code w/SD Sheild, LCD, Relay Module

Board: Arduino Mega

I have a happy functioning code with the SD sheild, LCD and Relay module and would like to add a standalone RTC to the mix so I can grab the current time for my log file. Please keep in mind that I am very new to working with Arduinos and am not a very seasoned C++ programmer. There could easily be an obvious mistake in my wiring and/or coding

The following code is how I have tried to add the DS1307RTC.h and Time.h libraries, it seems to simply stop functioning at line 217 [at the end]. The code has been cut to the void Setup section as the entire code exceeds the 9000 character limit of this forum.

Basically anywhere I try to use that setSyncProvider command, which I believe is necessary to get a reading from the clock. It is very likely I am putting it in the wrong place, or just using it wrong in general however.

I have the RTC hooked to 5v+, GND, and pins 20 [SDA] and 21 [SCL].

If I power the Arduino, it gets stuck at line 217 [based on the LCD.print, I am not seasoned in debugging], if I unplug the RTC from SDA/CDL it seems to continue just fine. I am thinking I have the RTC hooked up wrong and it's conflicting with the SD sheild or I am just using the commands wrong.

Thank you for any help!

// Initializing Code

// Knob signal input - A2
// Pressure Sensor signal input - A5

uint32_t volumesize;
const int buttonPin = 49;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status
int Signal = 0;
int sensorValue;
int airPSI;
int switchValue;
int count = 1;
String var;
const int chipSelect = 4;
int level = 0;  // -3 = Max Right, +3 = Max Left
unsigned long tepTimer ;   
int value;
int err = 0;
int lcd_key     = 0;
int adc_key_in  = 0;
int SecondNow;
int LastSecond;

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

// Including the various required libraries
  
  #include <Wire.h>
  #include <SPI.h>
  #include <SD.h>
  #include <DS1307RTC.h>
  #include <Time.h>  
  #include <LiquidCrystal.h>
  
//This is being used for the LCD buttons

  LiquidCrystal lcd(8, 9, 4, 5, 6, 7);        // select the pins used on the LCD panel

  int read_LCD_buttons(){    // LCD Requirements, read the buttons
  adc_key_in = analogRead(A4);       // read the value from the sensor 
  
  if (adc_key_in > 1000) return btnNONE; 
  if (adc_key_in < 50)   return btnRIGHT;  
  if (adc_key_in < 250)  return btnUP; 
  if (adc_key_in < 450)  return btnDOWN; 
  if (adc_key_in < 650)  return btnLEFT; 
  if (adc_key_in < 850)  return btnSELECT;  
  
  return btnNONE;                // when all others fail, return this.
}

// Making Bars for Left/Right Graph, pixelwise, they are custom characters

    uint8_t bar0[8]  = {
      0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}; 
    uint8_t bar5[8]  = {
      0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};

// Applying pins to run the relay board

  byte relayPin[6] = {22,24,26,28,30,32}; 
   //LOW turns relay OFF
   //HIGH turns relay ON


 //end initializing

    
void setup()  //this loop runs only once
{    
  
// Relay pins are initialized as outputs

  for(int i = 0; i < 6; i++)  pinMode(relayPin[i],OUTPUT);


// Initiating the input signal for the log switch

  pinMode(buttonPin, INPUT);

  lcd.begin(16, 2);                       // start the LCD library

  Serial.begin(9600);                     // start serial output
  Serial.println("System Check");

  //setSyncProvider(RTC.get);   // the function to get the time from the RTC  


  
  lcd.setCursor(0,0);                     // setCursor locates where the following inputs will end up on the display
  lcd.print("System Check");

// Assigning Bars for Graph that we made on line 56

  lcd.createChar(0, bar0);
  lcd.createChar(5, bar5);
  
  
//  System Check [Startup Check, quick just all "on", all "off"]

  //  All Relays "OFF"
    digitalWrite(relayPin[0], LOW); //Relay1 - Off
    digitalWrite(relayPin[1], LOW); //Relay2 - Off
    digitalWrite(relayPin[2], LOW); //Relay3 - Off
    digitalWrite(relayPin[3], LOW); //Relay4 - Off
    digitalWrite(relayPin[4], LOW); //Relay5 - Off
    digitalWrite(relayPin[5], LOW); //Relay6 - Off
    
    Serial.println();
    Serial.println("All Off");
    
    lcd.setCursor(0,1);  
    lcd.print("<   [^]   >     ");  

  //  Left & Right Level: 3
    digitalWrite(relayPin[0], HIGH); //Relay1 - Off
    digitalWrite(relayPin[1], HIGH); //Relay2 - Off
    digitalWrite(relayPin[2], HIGH); //Relay3 - Off
    digitalWrite(relayPin[3], HIGH); //Relay4 - Off
    digitalWrite(relayPin[4], HIGH); //Relay5 - Off
    digitalWrite(relayPin[5], HIGH); //Relay6 - Off
    
    Serial.println("Stage 3");
    
    lcd.setCursor(0,1);
    lcd.setCursor(0,1);
    lcd.print("<");
    lcd.write(5);
    lcd.write(5);
    lcd.write(5);
    lcd.print("[^]");
    lcd.write(5);
    lcd.write(5);
    lcd.write(5);
    lcd.print(">        ");
      
    delay(1500); //Delay 1.5s
  
  //  All Relays "OFF"
    digitalWrite(relayPin[0], LOW); //Relay1
    digitalWrite(relayPin[1], LOW); //Relay2
    digitalWrite(relayPin[2], LOW); //Relay3
    digitalWrite(relayPin[3], LOW); //Relay4
    digitalWrite(relayPin[4], LOW); //Relay5
    digitalWrite(relayPin[5], LOW); //Relay6
    
    Serial.println("All Off");
    
    lcd.setCursor(0,1);  
    lcd.print(" --- [^] --- ");  
    lcd.setCursor(0,0);
    lcd.print("Initializing");
    lcd.setCursor(0,1);
    lcd.print("                ");

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  Serial.print("Initializing SD card...");
  
  lcd.setCursor(0,0);
  lcd.print("Initializing.");
  
    
    if (!SD.begin(chipSelect)) {   // See if the card is present and can be initialized:
      Serial.println("Card failed, or not present");
        //don't do anything more:
      return;
    }
    
  Serial.println("Card Initialized.");
  
  lcd.print("Card Initialized     ");
  
//Checking if a log file is already there, if so, delete it and over-write it. 
//next steps will involve incrementing log files, if I can figure that out.
  
  if (SD.exists("datalog.csv")){    //Checking if File Exists
      Serial.println("datalog.csv exists - Overwritting Existing File");
      SD.remove("datalog.csv");
  }

//The following is required to write to to a .csv file, this portion is writing the headers for the file.

File dataFile = SD.open("datalog.csv", FILE_WRITE);

  // if the file is available, write to it:
  
    if (dataFile) {   //Creating .CSV File Header
      dataFile.println();                   //printing one blank line in file
      dataFile.println("Time, Stage, Position, Pressure");  //Alter file headers here
      dataFile.close();
      // print to the serial port too:
      Serial.println(level);
    }
  
    else {  // if the file isn't open, pop up an error:
      Serial.println("error opening datalog.csv - header");
    }

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Log Status: Off");
  lcd.setCursor(0,1);
  lcd.print(   " Ready to Log  ");



  
setSyncProvider(RTC.get);   // the function to get the time from the RTC  
  LastSecond = (second());    //Setting Up Counter to Prompt Datalog Entries
  SecondNow = (second());

  Serial.println(LastSecond);
  Serial.println(SecondNow);




}

Without the SD card, etc. connected, can you use the RTC?

Hi Paul,

I have used the RTC with a extra UNO board I have, it seems to function properly.

However, I have not tried it with the current code/setup I have minus the SD shield. I have quite a bit hooked up through the shield that I would need to rewire back up to get it to work. I can do that, if necessary.

Thanks