SOLVED: Script Using DS1307.h Appears Not to Update Variables Each Loop

I am trying to create a script to change the output of a DS3231 RTC into Unixtime, using the DS1307 library created by Matt Joyce. The DS1307RTC library has a nice mtkime function for this, but unfortunately I can’t use that library. I need to use Mr. Joyce’s library because it works in conjunction with other libraries that I need (i.e. phi prompts, phi interfaces). I created a short script for this that kind of works:

#include <DS1307.h> 
#include <Wire.h>
#include <math.h>

int rtc[7];
int Leap_Year_Correction_Factor;
int Months_Past;
int Days_From_Months;
unsigned long Current_Time;
int DST_Correction_Factor;


void setup()
{
  Serial.begin(9600);
}  


void loop()
{
  RTC.get(rtc,true);
  
  //Create variable to account for current year being a leap year 
  int Year_Type = (rtc[6] % 4);
  if (Year_Type == 0){
     Leap_Year_Correction_Factor = 1;
    }
  else
    {
    Leap_Year_Correction_Factor = 0;
    }
    
 
  //Create variable to define number of days elapsed in previous months 
  Months_Past = (rtc[5] - 1);
  if (Months_Past == 1) {
    Days_From_Months = 31;
    }
  else if (Months_Past == 2) {
    Days_From_Months = 59;
    }
  else if (Months_Past == 3) {
    Days_From_Months = 90;
    }
  else if (Months_Past == 4) {
    Days_From_Months = 120;
    }
  else if (Months_Past == 5) {
    Days_From_Months = 151;
    }
  else if (Months_Past == 6) {
    Days_From_Months = 181;
    }
  else if (Months_Past == 7) {
    Days_From_Months = 212;
    }
  else if (Months_Past == 8) {
    Days_From_Months = 243;
    }
  else if (Months_Past == 9) {
    Days_From_Months = 273;
    }
  else if (Months_Past == 10) {
    Days_From_Months = 304;
    }
  else if (Months_Past == 11) {
    Days_From_Months = 334;
    }
  else { 
    Days_From_Months = 0;
    }

   //Create variable to correct for US Day light savings time
  if (rtc[6] == 2015 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2016 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2017 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2018 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2019 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2020 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2021 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2022 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2023 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2024 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2025 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2026 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2027 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2028 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2029 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2030 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else {
    DST_Correction_Factor = 0;
    }


  //Current_Time is Unixtime or the number of seconds since 1/1/1970
  Current_Time = (((((rtc[6] - 1970) * 365.25) + Days_From_Months + (rtc[4] - 1) + Leap_Year_Correction_Factor) * 86400) + (((rtc[2] - 1) * 60) * 60) + (rtc[1] * 60) + (rtc[0]) - DST_Correction_Factor);

  Serial.print(Current_Time); 
  Serial.println();  
  
  delay(1000);
}

The script initially outputs the correct current unixtime from the DS3231 RTC, but it does not update. The serial monitor just keeps printing the same unixtime line after line. I don’t understand why. Is the Current_Time Variable not updating? Is the RTC data not updating? I don’t see why they wouldn’t. Any help on this would be greatly appreciated.

RTC_to_Unixtime_Test.ino (4.22 KB)

I don't understand why. Is the Current_Time Variable not updating?

Try printing the time you actually read from the RTC. If that isn't changing, Current_Time won't either.

Thanks Paul. I Tried that. The RTC time can print and is changing but the Current_time variable does not change, which is baffling the hell out of me because Current_time is a function of the RTC’s out put.

I used the following code to check that and the RTC’s out put to the serial monitor changed while Current_time stayed the same:

#include <DS1307.h> 
#include <Wire.h>
#include <math.h>

int rtc[7];
int Leap_Year_Correction_Factor;
int Months_Past;
int Days_From_Months;
unsigned long Current_Time;
int DST_Correction_Factor;


void setup()
{
  Serial.begin(9600);
}  


void loop()
{
  RTC.get(rtc,true);
  
    for(int i=0; i<7; i++)
  {
    Serial.print(rtc[i]);
    Serial.print(" ");
  }
  Serial.println();

  
  //Create variable to account for current year being a leap year 
  int Year_Type = (rtc[6] % 4);
  if (Year_Type == 0){
     Leap_Year_Correction_Factor = 1;
    }
  else
    {
    Leap_Year_Correction_Factor = 0;
    }
    
 
  //Create variable to define number of days elapsed in previous months 
  Months_Past = (rtc[5] - 1);
  if (Months_Past == 1) {
    Days_From_Months = 31;
    }
  else if (Months_Past == 2) {
    Days_From_Months = 59;
    }
  else if (Months_Past == 3) {
    Days_From_Months = 90;
    }
  else if (Months_Past == 4) {
    Days_From_Months = 120;
    }
  else if (Months_Past == 5) {
    Days_From_Months = 151;
    }
  else if (Months_Past == 6) {
    Days_From_Months = 181;
    }
  else if (Months_Past == 7) {
    Days_From_Months = 212;
    }
  else if (Months_Past == 8) {
    Days_From_Months = 243;
    }
  else if (Months_Past == 9) {
    Days_From_Months = 273;
    }
  else if (Months_Past == 10) {
    Days_From_Months = 304;
    }
  else if (Months_Past == 11) {
    Days_From_Months = 334;
    }
  else { 
    Days_From_Months = 0;
    }

   //Create variable to correct for US Day light savings time
  if (rtc[6] == 2015 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2016 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2017 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2018 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2019 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2020 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2021 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2022 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2023 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2024 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2025 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2026 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2027 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2028 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2029 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2030 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else {
    DST_Correction_Factor = 0;
    }


  //Current_Time is Unixtime or the number of seconds since 1/1/1970
  Current_Time = (((((rtc[6] - 1970) * 365.25) + Days_From_Months + (rtc[4] - 1) + Leap_Year_Correction_Factor) * 86400) + (((rtc[2] - 1) * 60) * 60) + (rtc[1] * 60) + (rtc[0]) - DST_Correction_Factor);

  Serial.print(Current_Time); 
  Serial.println();  
  
  delay(1000);
}

Cant Figure out how can the RTC change and not Current_Time?

Then, the next step is to break that ridiculously long calculation down into smaller pieces, with intermediate variables that you can print.If the time changes, but the calculation doesn't, then there is something wrong with the calculation.

You can get rid of the long cascading if statement and replace it with a single statement:

    Days_From_Months = daysSince[MonthsPast];

using the technique shown in the following example:

void setup() {
  Serial.begin(115200);  
}


void loop() {
  char buffer[5];
  int charsRead;

  int daysSince[] = {0, 31, 59, 90, 120, 151, 
                     181, 212, 243, 273, 304, 334, 0};
  int Days_From_Months;
  int MonthsPast;

  
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', buffer, 4);
    buffer[charsRead] = '\0';
    MonthsPast = atoi(buffer);
    Serial.print("MonthsPast = ");
    Serial.print(MonthsPast);
    Serial.print(" and days = ");
    Days_From_Months = daysSince[MonthsPast];     // All the work is here, the rest just creates a demo
    Serial.println(Days_From_Months);
  }

}

I got it to work. Thanks PaulS and econjack. I didn’t realize the Mega would have trouble with the calculations. The math was sound, I checked it with a scientific calculator. I didn’t realize the Mega needed me to brake it up into a couple of steps by using variables (thanks PaulS). The working code is pasted below:

#include <Wire.h>
#include <DS1307.h>
#include <math.h>

int rtc[7];
unsigned long Current_Time;
int Months_Past;
int Days_From_Months;
int Year_Type;
int Leap_Year_Correction_Factor;
float Past_Days;
unsigned long Past_Seconds;
unsigned long Current_Seconds;
unsigned long Total_Seconds;
unsigned long Seconds_From_Hours;
unsigned long Hour_In_Minutes;
int Seconds_From_Minutes;
int Hour;
int DST_Correction_Factor;
int Days_Since[] = {0, 31, 59, 90, 120, 151, 
                    181, 212, 243, 273, 304, 334};


void setup()
{
  Serial.begin(9600);
  /*
  RTC.stop();
  RTC.set(DS1307_SEC,1);
  RTC.set(DS1307_MIN,23);
  RTC.set(DS1307_HR,12);
  RTC.set(DS1307_DOW,1);
  RTC.set(DS1307_DATE,1);
  RTC.set(DS1307_MTH,10);
  RTC.set(DS1307_YR,7);
  RTC.start();
  */
}

void loop()
{
  RTC.get(rtc,true);

  
  //Create variable to account for current year being a leap year 
  int Year_Type = (rtc[6] % 4);
  if (Year_Type == 0 && rtc[5] >= 3){
     Leap_Year_Correction_Factor = 1;
    }
  else
    {
    Leap_Year_Correction_Factor = 0;
    }
  
  //Create variable to define number of days elapsed in previous months 
  Months_Past = (rtc[5] - 1);
  Days_From_Months = Days_Since[Months_Past];

   //Create variable to correct for US Day light savings time
  if (rtc[6] == 2015 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2016 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2017 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2018 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2019 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2020 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2021 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2022 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2023 && (Days_From_Months + rtc[4]) >= 71 && (Days_From_Months + rtc[4]) <= 309){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2024 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2025 && (Days_From_Months + rtc[4]) >= 68 && (Days_From_Months + rtc[4]) <= 306){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2026 && (Days_From_Months + rtc[4]) >= 67 && (Days_From_Months + rtc[4]) <= 305){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2027 && (Days_From_Months + rtc[4]) >= 73 && (Days_From_Months + rtc[4]) <= 311){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2028 && (Days_From_Months + rtc[4]) >= 72 && (Days_From_Months + rtc[4]) <= 310){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2029 && (Days_From_Months + rtc[4]) >= 70 && (Days_From_Months + rtc[4]) <= 308){
    DST_Correction_Factor = 3600;
    }
  else if (rtc[6] == 2030 && (Days_From_Months + rtc[4]) >= 69 && (Days_From_Months + rtc[4]) <= 307){
    DST_Correction_Factor = 3600;
    }
  else {
    DST_Correction_Factor = 0;
    }


  Past_Days = (((rtc[6] - 1970) * 365.25) + Days_From_Months +(rtc[4] - 1) + Leap_Year_Correction_Factor);
  Past_Seconds = (Past_Days * 86400);
  Hour = (rtc[2] - 1);
  Hour_In_Minutes = (Hour * 60);
  Seconds_From_Hours = (Hour_In_Minutes * 60);
  Seconds_From_Minutes = (rtc[1] * 60);
  Current_Seconds = (Seconds_From_Hours + Seconds_From_Minutes + rtc[0]);
  Total_Seconds = (Past_Seconds + Current_Seconds); 
  Current_Time = (Total_Seconds - DST_Correction_Factor);

  Serial.print(Current_Time);
  Serial.println();


  delay(1000);

}

econjack was right, using the array of values makes the code much cleaner than using the long cascading if statements.