Time change and Tracking

Hi,

Do I need to do the following with the attached code

  1. Change Hour = Hour -5 inorder to convert the Greenwich time to New York time.

  2. How can I tell its AM or PM?

//Sun Position Calculation by Mowcius (mowcius.co.uk)
//Provides sun position (relative) from static variables

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
#include <math.h>
#define pi    3.14159265358979323846
#define twopi (2*pi)
#define rad   (pi/180)
#define EarthMeanRadius     6371.01 // In km
#define AstronomicalUnit    149597890 // In km

float Longitude = 16.348883; //enter longitude here   
float Latitude = 48.991569; //enter latitude here
//--------

//Program Variables
 float ZenithAngle;
 float Azimuth;
  float RightAscension;
 float Declination;
  float Parallax;
  float ElevationAngle;

 float ElapsedJulianDays;
 float DecimalHours;
 float EclipticLongitude;
 float EclipticObliquity;
//--------

void setup() 
{
  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(00,58,16,3,7,7,15);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year, float Minutes)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
int *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = 2000+bcdToDec(Wire.read());
}
void displayTime(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
  char buf[26];
  snprintf(buf,sizeof(buf),"%02d.%02d.%04d  %02d:%02d:%02d  ", dayOfMonth, month, year, hour, minute, second);
  Serial.print(buf);
}


void sunPos(int Minutes, int Hours, int Day, int Month, int Year){


 // Auxiliary variables
 float dY;
 float dX;

 // Calculate difference in days between the current Julian Day
 // and JD 2451545.0, which is noon 1 January 2000 Universal Time

 float JulianDate;
 long int liAux1;
 long int liAux2;
 // Calculate time of the day in UT decimal hours
 DecimalHours = Hours + (Minutes / 60.0);
 // Calculate current Julian Day
 liAux1 =(Month-14)/12;
 liAux2=(1461*(Year + 4800 + liAux1))/4 + (367*(Month
 - 2-12*liAux1))/12- (3*((Year + 4900
 + liAux1)/100))/4+Day-32075;
 JulianDate=(float)(liAux2)-0.5+DecimalHours/24.0;
 // Calculate difference between current Julian Day and JD 2451545.0
 ElapsedJulianDays = JulianDate-2451545.0;

 // Calculate ecliptic coordinates (ecliptic longitude and obliquity of the
 // ecliptic in radians but without limiting the angle to be less than 2*Pi
 // (i.e., the result may be greater than 2*Pi)

 float MeanLongitude;
 float MeanAnomaly;
 float Omega;
 Omega=2.1429-0.0010394594*ElapsedJulianDays;
 MeanLongitude = 4.8950630+ 0.017202791698*ElapsedJulianDays; // Radians
 MeanAnomaly = 6.2400600+ 0.0172019699*ElapsedJulianDays;
 EclipticLongitude = MeanLongitude + 0.03341607*sin( MeanAnomaly )
 + 0.00034894*sin( 2*MeanAnomaly )-0.0001134
 -0.0000203*sin(Omega);
 EclipticObliquity = 0.4090928 - 6.2140e-9*ElapsedJulianDays
 +0.0000396*cos(Omega);

 // Calculate celestial coordinates ( right ascension and declination ) in radians
 // but without limiting the angle to be less than 2*Pi (i.e., the result may be
 // greater than 2*Pi)

 float Sin_EclipticLongitude;
 Sin_EclipticLongitude= sin( EclipticLongitude );
 dY = cos( EclipticObliquity ) * Sin_EclipticLongitude;
 dX = cos( EclipticLongitude );
 RightAscension = atan2( dY,dX );
 if( RightAscension < 0.0 ) RightAscension = RightAscension + twopi;
 Declination = asin( sin( EclipticObliquity )*Sin_EclipticLongitude );

 // Calculate local coordinates ( azimuth and zenith angle ) in degrees

 float GreenwichMeanSiderealTime;
 float LocalMeanSiderealTime;
 float LatitudeInRadians;
 float HourAngle;
 float Cos_Latitude;
 float Sin_Latitude;
 float Cos_HourAngle;
 GreenwichMeanSiderealTime = 6.6974243242 +
 0.0657098283*ElapsedJulianDays
 + DecimalHours;
 LocalMeanSiderealTime = (GreenwichMeanSiderealTime*15
 + Longitude)*rad;
 HourAngle = LocalMeanSiderealTime - RightAscension;
 LatitudeInRadians = Latitude*rad;
 Cos_Latitude = cos( LatitudeInRadians );
 Sin_Latitude = sin( LatitudeInRadians );
 Cos_HourAngle= cos( HourAngle );
 ZenithAngle = (acos( Cos_Latitude*Cos_HourAngle
 *cos(Declination) + sin( Declination )*Sin_Latitude));
 dY = -sin( HourAngle );
 dX = tan( Declination )*Cos_Latitude - Sin_Latitude*Cos_HourAngle;
 Azimuth = atan2( dY, dX );
 if ( Azimuth < 0.0 )
  Azimuth = Azimuth + twopi;
 Azimuth = Azimuth/rad;
 // Parallax Correction
 Parallax=(EarthMeanRadius/AstronomicalUnit)
 *sin(ZenithAngle);
 ZenithAngle=(ZenithAngle //Zenith angle is from the top of the visible sky (thanks breaksbassbleeps)
 + Parallax)/rad;
    ElevationAngle = (90-ZenithAngle); //Retrieve useful elevation angle from Zenith angle
}

void loop(){
  int year;
  byte month,dayOfWeek,dayOfMonth,hour,minute,second;
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  displayTime(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  sunPos(minute, hour, dayOfMonth, month, year); //Run sun position calculations
  Serial.print("Elevation Angle:  ");
  Serial.print(ElevationAngle, 3); //Print Elevation (Vertical) with no decimal places as accuracy is not really great enough
  Serial.print("\tAzimuth:  ");
  Serial.print(Azimuth, 3); //Print Azimuth (Horizontal) with no decimal places
  if(ElevationAngle < 0) Serial.println("  The sun has set. Get some sleep!");
  else Serial.println();
  delay(1000); // every second
}

I tired to change time and date in the code but it does not change kept showing me the same time and date.

walraven92:
2. How can I tell its AM or PM?

if(hour() < 12)
{
  // is am
}

Thanks! How about the time conversion? Do I need to subtract 5 plus the DS3231 get stuck with one time and date value no matter what I enter

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(30)); // set seconds
  Wire.write(decToBcd(29)); // set minutes
  Wire.write(decToBcd(12)); // set hours
  Wire.write(decToBcd(5)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(24)); // set date (1 to 31)
  Wire.write(decToBcd(12)); // set month
  Wire.write(decToBcd(15)); // set year (0 to 99)
  Wire.endTransmission();
  1. Change Hour = Hour -5 inorder to convert the Greenwich time to New York time.

Obviously, this won't work if the GMT hour is less than 5. The date has to change too. Changing time zones is not nearly as simple as you seem to think!

If you want the DS3231 to keep track of local time, enter the local time.

the DS3231 get stuck with one time and date value

Which time and date value?

walraven92:
Thanks! How about the time conversion? Do I need to subtract 5 plus the DS3231 get stuck with one time and date value no matter what I enter

you can constantly test for DST with a function like this:

bool IsDST(int dayOfMonth, int month, int dayOfWeek)
{
  if (month < 3 || month > 11)
  {
    return false;
  }
  if (month > 3 && month < 11)
  {
    return true;
  }
  int previousSunday = dayOfMonth - (dayOfWeek - 1); // Spark Sunday = 1
  //In march, we are DST if our previous sunday was on or after the 8th.
  if (month == 3)
  {
    return previousSunday >= 8;
  }
  //In November we must be before the first sunday to be dst.
  //That means the previous sunday must be before the 1st.
  return previousSunday <= 0;
}

and then add the hours to the time in seconds; something like this:

time -= (IsDST()? 5 * 3600 : 4 * 3600);

I tried the local time for example

Hour 10

minutes 52

Second 30

month 12

year 15

day 24

or if enter any other time

but the code is giving me

Hour 10

minutes 25

Second 20

month 12

year 15

day 22

but the code is giving me

What code?

The code you posted in reply #3 has nothing to do with the code in your first post.

Why not use the method shown in your first post to set the time?

  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(00,58,16,3,7,7,15);

I tried to use

// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(00,58,16,3,7,7,15);

but got the following errors

: In function 'void setup()':
75: error: 'DS3231' was not declared in this scope
75: error: expected ';' before 'seconds'
'DS3231' was not declared in this scope

My code is as follows. I am trying to get the RTC to set to local time (New York) but no luck so far! Any suggestions. Thanks

//Sun Position Calculation by Mowcius (mowcius.co.uk)
//Provides sun position (relative) from static variables

#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
#include <math.h>
#define pi    3.14159265358979323846
#define twopi (2*pi)
#define rad   (pi/180)
#define EarthMeanRadius     6371.01 // In km
#define AstronomicalUnit    149597890 // In km

float Longitude = 72.14; //enter longitude here   
float Latitude = 42.07; //enter latitude here
//--------

//Program Variables
 float ZenithAngle;
 float Azimuth;
  float RightAscension;
 float Declination;
  float Parallax;
  float ElevationAngle;

 float ElapsedJulianDays;
 float DecimalHours;
 float EclipticLongitude;
 float EclipticObliquity;
//--------


void setup() 
{
  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(00,58,16,3,7,7,15);
   
}

bool IsDST(int dayOfMonth, int month, int dayOfWeek)
{
  if (month < 3 || month > 11)
  {
    return false;
  }
  if (month > 3 && month < 11)
  {
    return true;
  }
  int previousSunday = dayOfMonth - (dayOfWeek - 1); // Spark Sunday = 1
  //In march, we are DST if our previous sunday was on or after the 8th.
  if (month == 3)
  {
    return previousSunday >= 8;
  }
  //In November we must be before the first sunday to be dst.
  //That means the previous sunday must be before the 1st.
  return previousSunday <= 0;
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year, float Minutes)

{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(30)); // set seconds
  Wire.write(decToBcd(54)); // set minutes
  Wire.write(decToBcd(10)); // set hours
  Wire.write(decToBcd(5)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(24)); // set date (1 to 31)
  Wire.write(decToBcd(12)); // set month
  Wire.write(decToBcd(15)); // set year (0 to 99)
  Wire.endTransmission();
}


void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
int *year)


{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = 2000+bcdToDec(Wire.read());
}
void displayTime(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
  char buf[26];
  snprintf(buf,sizeof(buf),"%02d.%02d.%04d  %02d:%02d:%02d  ", dayOfMonth, month, year, hour-5, minute, second);
  Serial.print(buf);
}


void sunPos(int Minutes, int Hours, int Day, int Month, int Year){

//Hours = Hours - 5;
 // Auxiliary variables
 float dY;
 float dX;

 // Calculate difference in days between the current Julian Day
 // and JD 2451545.0, which is noon 1 January 2000 Universal Time

 float JulianDate;
 long int liAux1;
 long int liAux2;
 // Calculate time of the day in UT decimal hours
 DecimalHours = Hours + (Minutes / 60.0);
 // Calculate current Julian Day
 liAux1 =(Month-14)/12;
 liAux2=(1461*(Year + 4800 + liAux1))/4 + (367*(Month
 - 2-12*liAux1))/12- (3*((Year + 4900
 + liAux1)/100))/4+Day-32075;
 JulianDate=(float)(liAux2)-0.5+DecimalHours/24.0;
 // Calculate difference between current Julian Day and JD 2451545.0
 ElapsedJulianDays = JulianDate-2451545.0;

 // Calculate ecliptic coordinates (ecliptic longitude and obliquity of the
 // ecliptic in radians but without limiting the angle to be less than 2*Pi
 // (i.e., the result may be greater than 2*Pi)

 float MeanLongitude;
 float MeanAnomaly;
 float Omega;
 Omega=2.1429-0.0010394594*ElapsedJulianDays;
 MeanLongitude = 4.8950630+ 0.017202791698*ElapsedJulianDays; // Radians
 MeanAnomaly = 6.2400600+ 0.0172019699*ElapsedJulianDays;
 EclipticLongitude = MeanLongitude + 0.03341607*sin( MeanAnomaly )
 + 0.00034894*sin( 2*MeanAnomaly )-0.0001134
 -0.0000203*sin(Omega);
 EclipticObliquity = 0.4090928 - 6.2140e-9*ElapsedJulianDays
 +0.0000396*cos(Omega);

 // Calculate celestial coordinates ( right ascension and declination ) in radians
 // but without limiting the angle to be less than 2*Pi (i.e., the result may be
 // greater than 2*Pi)

 float Sin_EclipticLongitude;
 Sin_EclipticLongitude= sin( EclipticLongitude );
 dY = cos( EclipticObliquity ) * Sin_EclipticLongitude;
 dX = cos( EclipticLongitude );
 RightAscension = atan2( dY,dX );
 if( RightAscension < 0.0 ) RightAscension = RightAscension + twopi;
 Declination = asin( sin( EclipticObliquity )*Sin_EclipticLongitude );

 // Calculate local coordinates ( azimuth and zenith angle ) in degrees

 float GreenwichMeanSiderealTime;
 float LocalMeanSiderealTime;
 float LatitudeInRadians;
 float HourAngle;
 float Cos_Latitude;
 float Sin_Latitude;
 float Cos_HourAngle;
 GreenwichMeanSiderealTime = 6.6974243242 +
 0.0657098283*ElapsedJulianDays
 + DecimalHours;
 LocalMeanSiderealTime = (GreenwichMeanSiderealTime*15
 + Longitude)*rad;
 HourAngle = LocalMeanSiderealTime - RightAscension;
 LatitudeInRadians = Latitude*rad;
 Cos_Latitude = cos( LatitudeInRadians );
 Sin_Latitude = sin( LatitudeInRadians );
 Cos_HourAngle= cos( HourAngle );
 ZenithAngle = (acos( Cos_Latitude*Cos_HourAngle
 *cos(Declination) + sin( Declination )*Sin_Latitude));
 dY = -sin( HourAngle );
 dX = tan( Declination )*Cos_Latitude - Sin_Latitude*Cos_HourAngle;
 Azimuth = atan2( dY, dX );
 if ( Azimuth < 0.0 )
  Azimuth = Azimuth + twopi;
 Azimuth = Azimuth/rad;
 // Parallax Correction
 Parallax=(EarthMeanRadius/AstronomicalUnit)
 *sin(ZenithAngle);
 ZenithAngle=(ZenithAngle //Zenith angle is from the top of the visible sky (thanks breaksbassbleeps)
 + Parallax)/rad;
    ElevationAngle = (90-ZenithAngle); //Retrieve useful elevation angle from Zenith angle
}

void loop()

{
  int year;
  byte month,dayOfWeek,dayOfMonth,hour,minute,second;
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  displayTime(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  sunPos(minute, hour, dayOfMonth, month, year); //Run sun position calculations
  Serial.print("Elevation Angle:  ");
  Serial.print(ElevationAngle, 3); //Print Elevation (Vertical) with no decimal places as accuracy is not really great enough
  Serial.print("\tAzimuth:  ");
  Serial.print(Azimuth, 3); //Print Azimuth (Horizontal) with no decimal places
  Serial.print("\tHour:");
  Serial.print(hour);
  if(ElevationAngle < 0) Serial.println("  The sun has set. Get some sleep!");
  else Serial.println();
  delay(1000); // every second
  
  
}

No matter what I try the RTC is giving me the following values. The date and time is all screwed up. I am using DS3231.

23.12.2015 -3:54:57

I set the time and date values using

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year, float Minutes)

{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(30)); // set seconds
Wire.write(decToBcd(54)); // set minutes
Wire.write(decToBcd(10)); // set hours
Wire.write(decToBcd(5)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(24)); // set date (1 to 31)
Wire.write(decToBcd(12)); // set month
Wire.write(decToBcd(15)); // set year (0 to 99)
Wire.endTransmission();
}

Stop hitting your head against the wall and use the Time, RTC, and TimeZone libraries so you can concentrate on the astronomy stuff. Always set the RTC to UTC and adjust to local time. Then you won't have problems with daylight savings time, for example.

How can I set the RTC to UTC and set the time to local time? Please give me some direction!

I changed the code a little bit. Now I got the date right and t think time right too . I uncommented the following piece of code and is working much better. But now, for example I am trying to calculate the elevation of Sun in NY at 11:00 am but the RTC thinks that its 11pm.

DS3231 seconds, minutes, hours, day, date, month, year;
setDS3231time(00,58,16,3,7,7,15);

walraven92:
How can I set the RTC to UTC and set the time to local time? Please give me some direction!

I don't need to. There are good directions packaged with the libraries. The principle is very simple, the library code adjusts the RTC times to make them local times. Set and get functions are provided. You won't have to putz around with the Wire library.

I changed the code a little bit.

But you won't show us that.

DS3231 seconds, minutes, hours, day, date, month, year;

Is not a valid line of code.

If the DS3231 thinks is it 11 PM, you have it set to 12 hour mode. Change that to 24 hour mode.

Thanks for your reply! Can you give me an example code to do that? I will really appreciate it

Can you give me an example code to do that?

You still need to post your working code.

Here you go. This is so far I get it to work.

#include <DS3231.h>
//Sun Position Calculation by Mowcius (mowcius.co.uk)
//Provides sun position (relative) from static variables

#include "Wire.h"

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}
#include <math.h>
#define pi    3.14159265358979323846
#define twopi (2*pi)
#define rad   (pi/180)
#define EarthMeanRadius     6371.01 // In km
#define AstronomicalUnit    149597890 // In km

float Longitude = 73.55; //enter longitude here   
float Latitude = 40.44; //enter latitude here
//--------

//Program Variables
 float ZenithAngle;
 float Azimuth;
  float RightAscension;
 float Declination;
  float Parallax;
  float ElevationAngle;

 float ElapsedJulianDays;
 float DecimalHours;
 float EclipticLongitude;
 float EclipticObliquity;
//--------



void setup() 
{
  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
 // DS3231 seconds, minutes, hours, day, date, month, year;
  setDS3231time(00,00,24,6,25,12,15);

}

bool IsDST(int dayOfMonth, int month, int dayOfWeek)
{
  if (month < 3 || month > 11)
  {
    return false;
  }
  if (month > 3 && month < 11)
  {
    return true;
  }
  int previousSunday = dayOfMonth - (dayOfWeek - 1); // Spark Sunday = 1
  //In march, we are DST if our previous sunday was on or after the 8th.
  if (month == 3)
  {
    return previousSunday >= 8;
  }
  //In November we must be before the first sunday to be dst.
  //That means the previous sunday must be before the 1st.
  return previousSunday <= 0;
}

//void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
//dayOfMonth, byte month, byte year, float Minutes)

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)

{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
 // setYear(byte Year); 
}


void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
int *year)


{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = 2000+bcdToDec(Wire.read());
}
void displayTime(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
  char buf[26];
  snprintf(buf,sizeof(buf),"%02d.%02d.%04d  %02d:%02d:%02d  ", dayOfMonth, month, year, hour, minute, second);
  Serial.print(buf);
}


void sunPos(int Minutes, int Hours, int Day, int Month, int Year){

//Hours = Hours - 5;
 // Auxiliary variables
 float dY;
 float dX;

 // Calculate difference in days between the current Julian Day
 // and JD 2451545.0, which is noon 1 January 2000 Universal Time

 float JulianDate;
 long int liAux1;
 long int liAux2;
 // Calculate time of the day in UT decimal hours
 DecimalHours = Hours + (Minutes / 60.0);
 // Calculate current Julian Day
 liAux1 =(Month-14)/12;
 liAux2=(1461*(Year + 4800 + liAux1))/4 + (367*(Month
 - 2-12*liAux1))/12- (3*((Year + 4900
 + liAux1)/100))/4+Day-32075;
 JulianDate=(float)(liAux2)-0.5+DecimalHours/24.0;
 // Calculate difference between current Julian Day and JD 2451545.0
 ElapsedJulianDays = JulianDate-2451545.0;

 // Calculate ecliptic coordinates (ecliptic longitude and obliquity of the
 // ecliptic in radians but without limiting the angle to be less than 2*Pi
 // (i.e., the result may be greater than 2*Pi)

 float MeanLongitude;
 float MeanAnomaly;
 float Omega;
 Omega=2.1429-0.0010394594*ElapsedJulianDays;
 MeanLongitude = 4.8950630+ 0.017202791698*ElapsedJulianDays; // Radians
 MeanAnomaly = 6.2400600+ 0.0172019699*ElapsedJulianDays;
 EclipticLongitude = MeanLongitude + 0.03341607*sin( MeanAnomaly )
 + 0.00034894*sin( 2*MeanAnomaly )-0.0001134
 -0.0000203*sin(Omega);
 EclipticObliquity = 0.4090928 - 6.2140e-9*ElapsedJulianDays
 +0.0000396*cos(Omega);

 // Calculate celestial coordinates ( right ascension and declination ) in radians
 // but without limiting the angle to be less than 2*Pi (i.e., the result may be
 // greater than 2*Pi)

 float Sin_EclipticLongitude;
 Sin_EclipticLongitude= sin( EclipticLongitude );
 dY = cos( EclipticObliquity ) * Sin_EclipticLongitude;
 dX = cos( EclipticLongitude );
 RightAscension = atan2( dY,dX );
 if( RightAscension < 0.0 ) RightAscension = RightAscension + twopi;
 Declination = asin( sin( EclipticObliquity )*Sin_EclipticLongitude );

 // Calculate local coordinates ( azimuth and zenith angle ) in degrees

 float GreenwichMeanSiderealTime;
 float LocalMeanSiderealTime;
 float LatitudeInRadians;
 float HourAngle;
 float Cos_Latitude;
 float Sin_Latitude;
 float Cos_HourAngle;
 GreenwichMeanSiderealTime = 6.6974243242 +
 0.0657098283*ElapsedJulianDays
 + DecimalHours;
 LocalMeanSiderealTime = (GreenwichMeanSiderealTime*15
 + Longitude)*rad;
 HourAngle = LocalMeanSiderealTime - RightAscension;
 LatitudeInRadians = Latitude*rad;
 Cos_Latitude = cos( LatitudeInRadians );
 Sin_Latitude = sin( LatitudeInRadians );
 Cos_HourAngle= cos( HourAngle );
 ZenithAngle = (acos( Cos_Latitude*Cos_HourAngle
 *cos(Declination) + sin( Declination )*Sin_Latitude));
 dY = -sin( HourAngle );
 dX = tan( Declination )*Cos_Latitude - Sin_Latitude*Cos_HourAngle;
 Azimuth = atan2( dY, dX );
 if ( Azimuth < 0.0 )
  Azimuth = Azimuth + twopi;
 Azimuth = Azimuth/rad;
 // Parallax Correction
 Parallax=(EarthMeanRadius/AstronomicalUnit)
 *sin(ZenithAngle);
 ZenithAngle=(ZenithAngle //Zenith angle is from the top of the visible sky (thanks breaksbassbleeps)
 + Parallax)/rad;
    ElevationAngle = (90-ZenithAngle); //Retrieve useful elevation angle from Zenith angle
}

void loop()

{
  int year;
  byte month,dayOfWeek,dayOfMonth,hour,minute,second;
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  displayTime(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  sunPos(minute, hour, dayOfMonth, month, year); //Run sun position calculations
  Serial.print("Elevation Angle:  ");
  Serial.print(ElevationAngle, 3); //Print Elevation (Vertical) with no decimal places as accuracy is not really great enough
  Serial.print("\tAzimuth:  ");
  Serial.print(Azimuth, 3); //Print Azimuth (Horizontal) with no decimal places
  Serial.print("\tHour:");
  Serial.print(hour);
  if(ElevationAngle < 0) Serial.println("  The sun has set. Get some sleep!");
  else Serial.println();
  delay(1000); // every second

  
}

How will you handle daylight savings time?

setDS3231time(00,00,24,6,25,12,15);Why are you setting the RTC to 24 o'clock?