Understanding RTC on Adafruit Datalogger shield

Hello!

As some may know from previous posts, I'm working in a group to make a datalogging pump control group as a school project.

The goal of our program:

You turn on the program with a switch, if not activated the program will not run nor the lights will be lit.
If it is activated it starts it program and a light will indicate that the switch is being activated.
A sensor will detect if the water level is high or low, if the water level is low, after a short amount of time: hysteresis, the pump has to be activated. When the pump activates he should write the date and time to a SD card.
If the sensor has detected that there is enough water the pump will turn off and again the date and time has to be written to the SD card.

We also need a small screen to indicate if the pump is running or not.
If the main switch is on and no one pushed a button, then the LCD screen backlight will turn off.
If someone presses a button the LCD will lit up for a while. It will scroll through the status: 1. is there an error 2.Status of the pump.

I have noticed a major error too: the millis function can only count up to roughly 50days or so (Source) but since my 'team' has no ear for me, there is not much I can do.

Now myself I document everything very carefully (even a 12yo could understand what I'm doing..) But too bad for me: my teammates not so much.. Usually I'm also the one who has to pick up a broken program and glue it together even though I've made fully working programs before, but just not as they like to see it.

So I stumble upon this code in the arduino program that my group has made:

#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); 
#endif
  rtc.begin();

  rtc.adjust(DateTime(2015,3,26,9,44,0));  // Het juist zetten van de datum en de tijd in het real time shield.
                                           // (DateTime(jaar,maand,dag,uur,minuten,seconden))

Question:
It should be used to initiate the RTC? But I'm wondering.. I searched it on the arduino website but the only thing I can make from that is that it's used for I2C communication?

Can someone explain this piece of code to me?
We're just plugging in the Addafruit datalogger to the left side of the Mega so the 5V GND etc matches.. So I'm pretty sure we don't use I2C or TWI connections..


Our setup:

Arduino Mega 2560
Adafruit datalogger shield (Documentation
HD44780 LCD display
And I'm not planning on using I2C except if I have to.. The reason is simple: the only thing I know of I2C: It's used to communicate with the arduino board like the USB does, and I don't really have the time to study into it.

This is the whole program as my teammate gave to me, ignore the dutch commentary.

If you have free time, and like to check other peoples program, you can dig right in ;D

#include <LiquidCrystal.h> // Library voor LCD aan te sturen.
LiquidCrystal lcd(40, 50, 49, 48, 47, 46);

#include <SPI.h>
#include <SD.h>
File myFile;

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;

/*Ingangen benoemen*/

const int EH = 23;                      //Sensor: Endress & Hauser trilvork                   
const int MainSwitch = 39;              //Hoofdschakelaar                                     

/*Uitgangen benoemen*/
const int PumpHigh = 27;                //Geel lampje als pomp werkt                          
const int PumpLow = 29;                 //Rood lampje als de pomp niet werkt                  
const int LSHigh = 31;                  //Geel lampje als niveau goed is                      
const int Pump = 33;                    //Relais van de pomp                                 
const int MainSwitchHigh = 35;          //Rood lampje Hoofdschakelaar
const int ledPin =  36;                 // De pin nummer van het lcd scherm
const int ledPin2 =  37;                // De pin nummer van het lcd scherm
const int PumpPin = 30;                 // De pin waar het signaal van "de motor draait" op aangesloten wordt.
const int buttonPin2 = 31;              // Knop voor het lcd scherm aan te zetten
const int SDaanwezig = 2;               // Hardware aansluiting dat SD aanwezig is of niet.
const long interval = 10000;            // tijd dat het lcd scherm aan blijft in miliseconden
const long interval2 = 10000;           // Hysteresis van de pomp!!

int SensorState = 0;                    //In rust is de status van de sensor laag
int MainSwitchState =0;                 //In rust is de status van de hoofdschakelaar laag
int buttonState = 0;                    // De initiële stand van de knop ("motor draait ingang")
int lastButtonState = 0;                // Vorige staat van de knop (zorgt voor flankdetectie)
int buttonState2 = 0;                   // De initiële stand van de knop lcd scherm
int ledState = LOW;                     // De initiële stand van de pin van het scherm
int ledState2 = LOW;                    // De initiële stand van de background pin van het scherm

/*In- en uitgangen instellen*/

Had to chop up the program since it exceeded the 9k characters...

[code]
void setup ()
{
Serial.begin(9600);                     //Serieel inlezen van data met een snelheid van 9600 baud
  pinMode(EH,INPUT);              
  pinMode(PumpHigh,OUTPUT);      
  pinMode(PumpLow,OUTPUT);
  pinMode(LSHigh,OUTPUT);
  pinMode(Pump,OUTPUT);
  pinMode(MainSwitch,INPUT_PULLUP);     //PULLUP weerstand om stoorsignaal te ellimineren
  pinMode(MainSwitchHigh,OUTPUT);
  pinMode(PumpPin, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(SDaanwezig,INPUT_PULLUP);     //pullup omdat deze rehtstreeks aan de ground hangt
 
  lcd.begin(16,2);
  lcd.noDisplay();
  SD.begin(10, 11, 12, 13);               // De pinnen die gebruikt worden om op de SD-kaart weg te schrijven
  
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); 
#endif
  rtc.begin();

  rtc.adjust(DateTime(2015,3,26,9,44,0));  // Het juist zetten van de datum en de tijd in het real time shield.
                                           // (DateTime(jaar,maand,dag,uur,minuten,seconden))

}

/*Het programma*/
void loop()
{

    DateTime now = rtc.now();
    buttonState2 = digitalRead(buttonPin2);                       // Hier word de status van de buttonpin weggeschreven in de buttonState2
    unsigned long currentMillis = millis();                       // Hier word de tijd dat het systeem draait weggeschreven naar currentMillis
    unsigned long currentMillis2 = millis();

MainSwitchState = digitalRead (MainSwitch);    //Status van de hoofdschakelaar inlezen
if (MainSwitchState == LOW)                    //Als de status v.d. hoofdschakelaar laag is
{
  digitalWrite(MainSwitchHigh,HIGH);           //Led hoofdschakelaar hoog
  SensorState = digitalRead (EH);              //Status van de sensor inlezen op ingang EH
      if (SensorState == HIGH)                 //Als status sensor = hoog
        {
          previousMillis2 = currentMillis;
          digitalWrite (Pump,LOW);             //Pomp => laag
          digitalWrite (PumpHigh,LOW);         //Led dat zegt dat pomp aan staat => laag
          digitalWrite (PumpLow,HIGH);         //Led dat zegt dat pomp uit staat => hoog
          digitalWrite(LSHigh,HIGH);           //Led dat zegt dat sensor bediend wordt => hoog
          Serial.println("pomp = LOW");        //Status van de pomp doorsturen naar seriële monitor
          digitalWrite (buttonState, LOW);
        }
      else {                                     //Sensor niet bediend
          if (currentMillis - previousMillis2 >=interval2) {
            
           digitalWrite (Pump,HIGH);           //Pomp => aan
           digitalWrite (PumpHigh,HIGH);       //Led pomp aan => hoog
           digitalWrite (PumpLow,LOW);         //Led pomp uit => laag
           digitalWrite(LSHigh,LOW);           //LevelSwitchHoog Led => laag
           Serial.println("pomp = HIGH");      //Status van de pomp doorsturen naar seriële monitor
           digitalWrite (buttonState, HIGH);
          }
       };
 }
 else                                          //Als de hoofdschakelaar af staat werkt de pomp niet nog branden de ledjes
 {
   digitalWrite(Pump,LOW);                     
   digitalWrite(PumpLow,LOW);
   digitalWrite(PumpHigh,LOW);
   digitalWrite(LSHigh,LOW);
   digitalWrite(MainSwitchHigh,LOW);
   digitalWrite (buttonState, LOW);
   Serial.println("alles = LOW");
 };
 if (buttonState != lastButtonState) {                         // Edge detection of the pump
                                                                  // Het geïnverteerde van lastButtonState wordt dan weggeschreven in buttonState
    
      if (buttonState == HIGH) {                                  // Als buttonState hoog is wil dit zeggen dat de pomp begint met draaien
      
    
      myFile = SD.open("werkuren.txt", FILE_WRITE);               // Voor men in een bestand kan schrijven moet men dit eerst openen
      
      myFile.print("Start pompen");                               //
      myFile.println();  
      myFile.print(now.year(), DEC);
      myFile.print('/');
      myFile.print(now.month(), DEC);
      myFile.print('/');
      myFile.print(now.day(), DEC);
      myFile.print(' ');
      myFile.print(now.hour(), DEC);
      myFile.print(':');
      myFile.print(now.minute(), DEC);
      myFile.print(':');
      myFile.print(now.second(), DEC);
      myFile.println();
      myFile.close();  
      }
    
      else {
           
      myFile = SD.open("werkuren.txt", FILE_WRITE);
      myFile.print(now.year(), DEC);
      myFile.print('/');
      myFile.print(now.month(), DEC);
      myFile.print('/');
      myFile.print(now.day(), DEC);
      myFile.print(' ');
      myFile.print(now.hour(), DEC);
      myFile.print(':');
      myFile.print(now.minute(), DEC);
      myFile.print(':');
      myFile.print(now.second(), DEC);
      myFile.println();
      myFile.print("Einde pompen");
      myFile.println();
      myFile.println();
      myFile.close();
      }
    }
    if (buttonState2 == HIGH) {                                   // Als de knop van lcd scherm aan hoog wordt dan gebeurt het volgende (zier hieronder)
      previousMillis = currentMillis;                             // PreviousMillis wordt gelijk aan currentMillis, previousMillis houd de tijd bij van de laatste keer dat er op de knop lcd aan werd gedrukt
      digitalWrite(ledPin , HIGH);                                // De pin van het scherm en van de background worden hier hoog gezet, het scherm gaat dus aan
      lcd.display();
    }
      
    if(currentMillis - previousMillis >= interval) {              // Als currentMillis (momentele systeem tijd) min de previousMillis (laatste keer dat op knop is gedrukt)
                                                                  // groter of gelijk wordt aan het interval (tijd dat lcd aan mag blijven) dan gebeurt het volgende
      digitalWrite(ledPin , LOW);                                 // De schermpin en backgroundpin worden hier weer laag gezet wanneer de tijd verlopen is.
      lcd.noDisplay();
    }
    
    if (digitalRead (SDaanwezig) == HIGH)          // origineel SD.begin(10, 11, 12, 13), werkt niet , high = geen detectie
  {
                                              //Als niemand op het knopje voor de error te skippen duwt
    while(digitalRead (buttonState) == LOW)                 //Dan blijf je de error op het LCD schrijven. (while loop gaat oneindig keer door. -- aangepast
    {
      while(digitalRead (SDaanwezig) == HIGH);
   lcd.clear(); 
   lcd.setCursor(0,0); 
   lcd.println("SD card error");         // Error if SD doesnt load
    }
                                   
    { delay(2000);
    }
    
     if (buttonState == HIGH) {
      lcd.clear();
      lcd.setCursor(0,0);  
      lcd.print("pomp aan");
    }
    else {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("pomp uit");
    }
    
    lastButtonState = buttonState;
    delay (1000);
};

[/code]

Defink:
I have noticed a major error too: the millis function can only count up to roughly 50days or so (Source) but since my 'team' has no ear for me, there is not much I can do.

That's not a real issue. Since you can use millis to time a second, you can just increment a variable every second. That will run for 50,000 days if it's another unsigned long int.

No one who can help me with the wire commands?

Defink:
We're just plugging in the Addafruit datalogger to the left side of the Mega so the 5V GND etc matches.. So I'm pretty sure we don't use I2C or TWI connections..
.................................
And I'm not planning on using I2C except if I have to.. The reason is simple: the only thing I know of I2C: It's used to communicate with the arduino board like the USB does, and I don't really have the time to study into it.

Well, if you plug in less than half the connections between the Mega and the shield, you expect it to work? The RTC will certainly not work without I2C. But there is the I2C code, staring you in the face. So you need to have a talk with your dev team about it.