RTC DS3231 adds 2 hours to actual time

Hey people,

I had a project with somebody else to program steppermotors controlling a mirror to reflect light into a living room. He wrote most of the code and the calculations have been written by another students group. Now I am getting a problem with the RTC-code, it subtracts 2 hours from the actual time when synced with the PC. I am really new with arduino and this is kind of the last thing I am planning to add to this code before giving it to the next group of students. I wonder if somebody could help me to wright the code to have the correct time, given by the RTC. There are a few bits of code I have disabled because the DATA-PIN 4 has been assigned to the LCD as well as to ‘something else’ and that interferes with one another. I have posted the code in parts because it was to big for one upload.

I hope its clear, thanks in advance.

//-----------RECIEVER-------------
/*
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#define VCC_PIN 2 // source 5V up to 40mA from this pin, oud 5
#define GND_PIN 5 // sink up to 40mA on this pin, Oud 2
#define DATA_PIN 4 // external int 1 on Uno, oud 3
int rfstate = 0;        //remote button status: 0 = nothing actuated/pressed / 1 = button A / 2 = button B / 3 = button C / 4 = button D
*/

//-----------RTC and LCD
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <Wire.h>

//#include <Stepper.h>


DateTime now;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

RTC_DS3231 rtc;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // (rs, e, d4, d5, d6, d7)
/*
  const int StepsPerRevolution = 32; //32*64=2048 is 1x draaien
  Stepper myStepperAzimuth(StepsPerRevolution, 8, 10, 9, 11);   //---old Code, don't use this---
  Stepper mySteppersunHeightAngle(StepsPerRevolution, 4, 3, 5, 6);
*/

void showDate(void);
void showTime(void);
void showDay(void);

const float Pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;

//---Time constants---
int   dayOfYear = 162;
float declination = 0.0;
float hourAngle = 0.0;
float timeInHours = 0.0;
float sunHeightAngle = 0.0; //---Altitude---
float latitude = 52.0;
float azimut1 = 0.0; //---Azimut sun---
float A = 0.0;
float E4 = 0.0;
float E5 = 0.0;
float Results = 0.0;

float Hour = 0.0;
float Minute = 0.0;

//---Vectors---
float lengthR = 0.0; //---lengthR is the value of the length of the radius generated by the 3 vectors, projected on horizontal plane
float Sx = 0.0;
float Sy = 0.0;      //---Sun vectors---
float Sz = 0.0;

float Hr = 0.0;
float Hx = 0.0;      //---Vectors of the house---
float Hy = 0.0;     
float Hz = 0.0;


float Mx = 0.0;
float My = 0.0;      //---Mirror vectors/vectors perpendicular to mirror's plane---
float Mz = 0.0;

float nMx = 0.0;
float nMy = 0.0;     //---Normalised vectors/vectors perpendicular to mirror's plane---
float nMz = 0.0;

float WS = 0.0;
float WH = 0.0;      //---Values of the lengts, resulted from all 3 vectors. WS: Sun. WH: House. WM: Mirror. These are used to
float WM = 0.0;      //used for calculating the normalised vectors of the sun, house and mirror.---

float azimutM = 0.0;    //---azimut mirror---
float sunHeightM = 0.0; //---sun height angle of mirror---

float azimutMold = azimutM;      //---Old values become current values---
float sunHeightMold = sunHeightM;

/*
float StepsCountAzimuth = 0.0; //---SECRET_AZIMUTSPELLING Azimuth=azimut?
float StepsCountsunHeight = 0.0;

float StepsHorizontal = 0.01;
float StepsVertical = 0.01;
*/



//---Motor1=sunheight angle/altitude---
//---Motor2=Aziumut---

int reststate = 0;
int motorselect = 0;    //motor selectie: 0 = motor 1 / 1 = motor 2

int selectstate1 = 0;   //To select motor press button B 
int selectstate2 = 0;   //To toggle between safemode off/on press button C     
int safemode = 0;       //safemode 0 = off/1 = on --- motors are turned off with safemode turned on
const int safePin = 7;  //status led, safemode, shows if safemode is on/off

//------------MOTOR----------------

const int stepPin1 = 10; //motor 1 turns 1 step
const int dirPin1 = 11;  //motor 1 turns in a direction

const int stepPin2 = 12; //motor 2 turns 1 step
const int dirPin2 = 13;  //motor 2 turns in a direction

const int sleepPin1 = 8; //sleep motor 1
const int sleepPin2 = 9; //sleep motor 2

long motorbereik1 = 34000;  //motor 1 range in steps count
int motorbereik2 = 7538;  //motor 2 range in steps count

int motorposition1 = 0; //motor 1 position steps count
int motorposition2 = 0; //motor 2 position steps count

int newmotorposition1 = 0;   //motor 1 position update (used for input of the values that come from the calculations)
int newmotorposition2 = 0;   //motor 2 position update (used for input of the values that come from the calculations)

int oldmotorposition1 = 0;   //store the motor 1 position  (for when in safemode)
int oldmotorposition2 = 0;   //store the motor 2 position  (for when in safemode)

int steptime1 = 500; //delay between motor puls / speed of motor 1 (full step)
int steptime2 = 1000; //delay between motor puls / speed of motor 2 (quarter step)

float motor1Angle=0.0014382195; //Change in angle per step// sunheight angle/altitude 
float motor2Angle=0,0932142857; //Change in angle per step// azimut



//-----BLINK WITHOUT DELAY---------
//RECIEVER
unsigned long previousMillis1 = 0;
const long interval1 = 75;          //reciever read interval time

//MOTOR SLEEP
unsigned long previousMillis2 = 0; 
const long interval2 = 1000;        //motor 1 time until in sleepmode

unsigned long previousMillis3 = 0; 
const long interval3 = 1000;        //motor 2 time until in sleepmode

//-----------POSITION--------------
int safestep1 = 0;
int safestep2 = 0;
int safemove = 0;
void setup ()
{
  //---Setting up RTC and adjusting to the correct time---
  Wire.begin();
  Serial.begin(19200);
  lcd.begin(16, 2);
  //delay(2000);
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC Module");
    while (1);
  }

  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));






/*

This piece off code is disabled for the reason of the LCD using the same pin as the reciever in this code,
enable this to start using the reciever again

  //-----------RECIEVER-------------
 pinMode(DATA_PIN, INPUT);
 // just leave D4 tristated
 
 pinMode(GND_PIN, OUTPUT);
 digitalWrite(GND_PIN, LOW); 

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

 Serial.begin(19200);
 mySwitch.enableReceive(1);  // Receiver on interrupt 1 => that is pin D3
 Serial.println("Reciever started");
 
 */
 
 

 
 
//------------MOTOR----------------
  pinMode(stepPin1,OUTPUT); 
  pinMode(dirPin1,OUTPUT);
  pinMode(stepPin2,OUTPUT); 
  pinMode(dirPin2,OUTPUT);
  
  pinMode(sleepPin1,OUTPUT);
  digitalWrite(sleepPin1,HIGH);

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

  pinMode(safePin,OUTPUT);
}
static unsigned long count = 0;



void loop () 
{

  now = rtc.now();
  {
  if ((now.hour() == 0) && (now.minute() == 0) && (now.second() == 0))
  {
    dayOfYear = dayOfYear + 1;
  }
  else if ((dayOfYear == 365) && (now.hour() == 23) && (now.minute() == 59) && (now.second() == 59))
  {
    dayOfYear = 0;
 }
  else
 {
    dayOfYear = dayOfYear;
  }
  
  ///*
  showDate();
  showDay();
  showTime();
  //*/
  
  Hour=now.hour();
  Minute=now.minute();
  timeInHours = ( now.hour() + ( now.minute() * (1.0 / 60.0)));
  Serial.println((String)"TIME:" + "HOUR:" + now.hour() + " MIN:" + now.minute() + " SEC:" + now.second() + " DayOfYear:" + dayOfYear +
  "                                T-hours: " + timeInHours);
  

  //Serial.println((String)"DayOfYear:"+ dayOfYear);
  //Serial.println((String)"T-hours: "+timeInHours);

 
  //---Formula's for calculation of angles---
  declination = 23.44 * sin(360.0 / 365.0 * (284.0 + dayOfYear) * Pi / 180.0);

  hourAngle = (timeInHours * 15.0);

  sunHeightAngle = asin((sin(latitude * Pi / 180.0) * sin(declination * Pi / 180.0)) - (cos(latitude * Pi / 180.0) * cos(declination * Pi / 180.0) * cos(hourAngle * Pi / 180.0))) * 180.0 / Pi;

  azimut1 = asin((cos(declination * Pi / 180.0) * sin(hourAngle * Pi / 180.0)) / (cos(sunHeightAngle * Pi / 180.0))) * 180.0 / Pi;

  //---Equations to make the unit circle have correct values---

  E4 = 360.0 + azimut1;
   
   A = asin((cos(declination*Pi/180.0)*sin((hourAngle - 0.1) *Pi/180.0))/(cos(sunHeightAngle*Pi/180.0)))*180.0/Pi; //Berekening voor de IF-statement om E5 te kunnnen uitrekenen//
  
   if (azimut1 > A)
     {
        E5 = asin((cos(declination*Pi/180.0)*sin(hourAngle*Pi/180.0)) / (cos(sunHeightAngle*Pi/180.0)))*(180/Pi);
     }
   else
     { 
        E5 = 180.0 - asin((cos(declination*Pi/180.0)*sin(hourAngle*Pi/180.0)) / (cos(sunHeightAngle*Pi/180.0)))*(180/Pi);
     }
   
    
   if(E5 < 0.0)
    {
       Results = E4;
    }
   else
    {
       Results = E5;
    }
 
    lengthR = (cos(sunHeightAngle*Pi/180.0));
    Sx = (sin(Results*Pi/180.0)*lengthR);      //---Sun vectors---
    Sy = (cos(Results*Pi/180.0)*lengthR);
    Sz = (tan(sunHeightAngle*Pi/180.0)*lengthR);
   
    Hr = (cos(5.0*Pi/180.0));
    Hx = (sin(250.0*Pi/180.0)*Hr);    //---House vectors---
    Hy = (cos(250.0*Pi/180.0)*Hr);
    Hz = (tan(5.0*Pi/180.0)*Hr);
   
    WS = (sqrt ( (Sx*Sx) + (Sy*Sy) + (Sz*Sz)));     //---Values of the lengts, resulted from all 3 vectors. WS: Sun. WH: House. WM: Mirror. These are used to
    WH = (sqrt ( (Hx*Hx) + (Hy*Hy) + (Hz*Hz)));     //used for calculating the normalised vectors of the sun, house and mirror.---//   
    WM = (sqrt ( (Mx*Mx) + (My*My) + (Mz*Mz)));

    Mx = ( (Sx / WS) + ( Hx / WH ));    //---Calculates the mirror's perpendicular vector---
    My = ( (Sy / WS) + ( Hy / WH ));
    Mz = ( (Sz / WS) + ( Hz / WH ));

    nMx = ( Mx / WM);    //---Calculating the normalised vectors of the mirror's perpendicular vector---
    nMy = ( My / WM);
    nMz = ( Mz / WM);


   if(nMx > 0.0 && nMy > 0.0)
     { 
       azimutM = ((atan(nMx/nMy)) * 180/Pi);
     }
    else if(nMx > 0.0 && nMy <= 0.0)
     {
       azimutM = (90.0 - (atan(nMx/nMy)) * 180/Pi);
     }
    else if (nMx <= 0.0 && nMy <= 0.0) 
     {
       azimutM = (180.0 + (atan(nMx/nMy)) * 180/Pi);
     }
    else
     {
       azimutM = 360 + ((atan(nMx/nMy)) * 180/Pi);
     }

     sunHeightM = (atan (nMz/ (sqrt((nMx*nMx) + (nMy*nMy))))) * 180/Pi;
 
    }

}
//*/
//---End of calculations---


/*

//-----------used to update the motorposition and set it to a new position-----------

if(now.second() == 59)
     {
         
         zonhoogteSoud = zonhoogteS;
         StepsCountZonhoogte = 0.0;
         StappenVertikaal = 0.01;
     } 
     
  if(now.second() == 59)
     {
         azimutSoud = azimutS;
         StepsCountAzimuth = 0.0;
         StappenHorizontaal = 0.01;
         
     }
*/

//-----------Change the above to something like the following-----------

if(now.second() == 59)
     {
        sunHeightMold=sunHeightM;
        motorposition1 = 0.0;
        ZonMotorUp()
        motorposition1++;
}

//-------------MOTOR--------------

  //-----------Adjust mirror with remote-----------
if(safemode == 0 && rfstate == 1 && motorselect == 0 && motorposition1 < motorbereik1 ){
    digitalWrite(sleepPin1,LOW);
    ZonMotorUp();
    motorposition1++;
    newmotorposition1++;
    previousMillis2 = currentMillis;}
  
if (safemode == 0 && rfstate == 4 && motorselect == 0 && motorposition1 > 0){
    digitalWrite(sleepPin1,LOW);
    ZonMotorDown();
    motorposition1--;
    newmotorposition1--;
    previousMillis2 = currentMillis;}
    
if (safemode == 0 && rfstate == 1 && motorselect == 1 && motorposition2 < motorbereik2 ){
    digitalWrite(sleepPin2,LOW);
    AziMotorUp();
    motorposition2++;
    newmotorposition2++;
    previousMillis3 = currentMillis;}
    
if (safemode == 0 && rfstate == 4 && motorselect == 1 && motorposition2 > 0){
    digitalWrite(sleepPin2,LOW);
    AziMotorDown();
    motorposition2--;
    newmotorposition2--;
    previousMillis3 = currentMillis;}
  
  
  


  //-----------spiegel verstellen met updates-----------
if (motorposition1 < newmotorposition1 || motorposition1 > newmotorposition1){
  if (motorposition1 < newmotorposition1){
    digitalWrite(sleepPin1,LOW);
    ZonMotorUp();
    motorposition1++;
    previousMillis2 = currentMillis;}
    
  if (motorposition1 > newmotorposition1){
    digitalWrite(sleepPin1,LOW);
    ZonMotorDown();
    motorposition1--;
    previousMillis2 = currentMillis;}
}
else if (motorposition2 < newmotorposition2 || motorposition2 > newmotorposition2){
  if (motorposition2 < newmotorposition2){
    digitalWrite(sleepPin2,LOW);
    AziMotorUp();
    motorposition2++;
    previousMillis3 = currentMillis;}   

  if (motorposition2 > newmotorposition2){
    digitalWrite(sleepPin2,LOW);
    AziMotorDown();
    motorposition2--;
    previousMillis3 = currentMillis;}       
}

//-----------sleepmode motor 1 toggle aan-----------
if((digitalRead(sleepPin1) == LOW)&&(currentMillis - previousMillis2 >= interval2)){
  previousMillis2 = currentMillis;
  digitalWrite(sleepPin1,HIGH);
}
//sleepmode motor 2 toggle aan
if((digitalRead(sleepPin2) == LOW)&&(currentMillis - previousMillis3 >= interval3)){
  previousMillis3 = currentMillis;
  digitalWrite(sleepPin2,HIGH);
  }

}

//-----------commands to control the motors rotating (counter)-clockwise-----------

void ZonMotorUp(){
    digitalWrite(dirPin1,LOW);
    digitalWrite(stepPin1,HIGH);
    delayMicroseconds(steptime1);
    digitalWrite(stepPin1,LOW);
    delayMicroseconds(steptime1);
}

void ZonMotorDown(){
    digitalWrite(dirPin1,HIGH);
    digitalWrite(stepPin1,HIGH);
    delayMicroseconds(steptime1);
    digitalWrite(stepPin1,LOW);
    delayMicroseconds(steptime1);
}

void AziMotorUp(){
    digitalWrite(dirPin2,HIGH);
    digitalWrite(stepPin2,HIGH);
    delayMicroseconds(steptime2);
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(steptime2);
  }
  
void AziMotorDown(){
    digitalWrite(dirPin2,LOW);
    digitalWrite(stepPin2,HIGH);
    delayMicroseconds(steptime2);
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(steptime2);
  }








//-----------Code voor lcd-----------
///*
void showDate()
{
  lcd.setCursor(0, 0);
  lcd.print(now.day());
  lcd.print('-');
  lcd.print(now.month());
  lcd.print('-');
  lcd.print(now.year()-2000);
}

void showDay()
{
  lcd.setCursor(8, 0);
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
}

void showTime()
{
  lcd.setCursor(0, 1);
  lcd.print("Time:");
  lcd.print((now.hour() )); 
  lcd.print(':');
  lcd.print(now.minute());
  lcd.print(':');
  lcd.print(now.second());
  lcd.print("    ");

Reply to #1: this the code where time gets programmed into the RTC.