i corrected the issues with hours and days in mine.
here is side by side comparisons between our calculations:
mine:
Time Until 10 / 10 / 2014 - 0 : 0 : 0 hours
01 Years : 01 Months : 21 Days
13 Hours : 9 Minuets : 36 Seconds
yours:
Time Until 10 / 10 / 2014 - 0 : 0 : 0 hours
1 years, 1 month, 22 days
31 hours, 21 minutes, 15 seconds
both are counting down until 0:0:0 hours, the codes are using the real time clock on my setup for the current time and so will be off from one-another by around 1 or so minutes as time continues to move.
based on my calculation result , taking a calender, starting on today's date, "flipping " 13 months, that lands me on the 18th of September 2014. if i then add on the 13 hours, 9 minutes, 36 seconds to today, that brings me to midnight 19th of September. moving 21 days from the 19th, brings be to the 10th of October as i wanted
i am not sure how your code is calculating 31 hours, but it does not get the result i am looking for, this is the same issue i said i saw last night.
i understand mine is longer, but i wrote it based on how a person would perform the math on paper by performing date/time subtraction which to me is easy to follow...
i would gladly use your more efficient and elegant code, but i consistently cannot get it to calculate what i am expecting.
my final code appears to be this:
/*
#include <SPI.h>
#include <Wire.h>
#include "Wire.h"
byte seconds, minutes, hours, dayOfWeek, dayOfMonth, months, years;
#define DS1307_I2C_ADDRESS 0x68 //address of the RTC on the SPI bus
byte days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void setup() {
Serial.begin(115200);
Wire.begin();//needed to comunicate with the real time clock
}
double prevDisplay=0;
void loop()
{
getDateDs1307(seconds, minutes, hours, dayOfMonth, dayOfMonth, months, years);
char buffer[40];
byte remaining_seconds, remaining_minuets, remaining_hours, remaining_days, remaining_months, remaining_years;
if ((seconds + minutes + hours) != prevDisplay) { //update the display only if time has changed
prevDisplay = (seconds + minutes + hours);
calculate_countdown(0,0,0,10,10,14, remaining_seconds, remaining_minuets, remaining_hours, remaining_days, remaining_months, remaining_years);
sprintf (buffer, "Time Until %.d / %.d / %.d ", 10, 10, 2014);
Serial.println(buffer);
sprintf (buffer, "%.2d Years : %.2d Months : %.2d Days", remaining_years, remaining_months, remaining_days);
Serial.println(buffer);
sprintf(buffer, "%d Hours : %d Minuets : %d Seconds", remaining_hours, remaining_minuets, remaining_seconds);
Serial.println(buffer);
Serial.println();
}
}
void calculate_countdown(byte future_seconds,
byte future_minutes,
byte future_hours,
byte future_days,
byte future_months,
byte future_years,
byte &remaining_seconds,
byte &remaining_minuets,
byte &remaining_hours,
byte &remaining_days,
byte &remaining_months,
byte &remaining_years){
bool borrow = false;
/******************************
/*CALCULATE REMIANING SECONDS
/*****************************/
if (future_seconds - seconds <0){
remaining_seconds = 60 - seconds;
borrow = true;
}else{
remaining_seconds = future_seconds - seconds;
borrow = false;
}
/******************************
/*CALCULATE REMIANING MINUTES
/*****************************/
if (borrow == true){
if (future_minutes - minutes - 1 <0){
remaining_minuets = 60 - minutes - 1 + future_minutes;
borrow = true;
}else{
remaining_minuets = future_minutes - minutes - 1;
borrow = false;
}
}else{
if (future_minutes - minutes <0){
remaining_minuets = 60 - minutes + future_minutes;
borrow = true;
}else{
remaining_minuets = future_minutes - minutes;
borrow = false;
}
}
/******************************
/*CALCULATE REMIANING HOURS
/*****************************/
if (borrow == true){
if (future_hours - hours - 1 <0){
remaining_hours = 24 - hours - 1 + future_hours;
borrow = true;
}else{
remaining_hours = future_hours - hours - 1;
borrow = false;
}
}else{
if (future_hours - hours <0){
remaining_hours = 24 - hours + future_hours;
borrow = true;
}else{
remaining_hours = future_hours - hours;
borrow = false;
}
}
/******************************
/*CALCULATE REMIANING DAYS
/*****************************/
if (borrow == true){
if ((future_days - dayOfMonth - 1) <0){
remaining_days = days_per_month[months-1] - dayOfMonth - 1 + future_days;
borrow = true;
}else{
remaining_days = future_days - dayOfMonth - 1;
borrow = false;
}
}else{
if (future_days - dayOfMonth <0){
remaining_days = days_per_month[months-1] - dayOfMonth + future_days;
borrow = false;
}else{
remaining_days = future_days - dayOfMonth;
borrow = false;
}
}
/******************************
/*CALCULATE REMIANING MONTHS
/*****************************/
if (borrow == true){
if ((future_months - months) - 1 <0){
remaining_months = 12 - abs(future_months - months) - 1;
if ((months + remaining_months) > 12){
remaining_days -= (days_per_month[months-1] - days_per_month[(months + remaining_months - 12)-1]);
}else{
remaining_days -= (days_per_month[months-1] - days_per_month[(months + remaining_months)-1]);
}
borrow = true;
}else{
remaining_months = future_months - months - 1;
if ((months + remaining_months) > 12){
remaining_days -= (days_per_month[months-1] - days_per_month[(months + remaining_months - 12)-1]);
}else{
remaining_days -= (days_per_month[months-1] - days_per_month[(months + remaining_months)-1]);
}
borrow = false;
}
}else{
if (future_months - months <0){
remaining_months = 12 - abs(future_months - months);
borrow = true;
}else{
remaining_months = future_months - months;
borrow = false;
}
}
/******************************
/*CALCULATE REMIANING YEARS
/*****************************/
if (borrow == true){
if (future_years - years - 1 <0){
remaining_years = 0;
}else{
remaining_years = future_years - years - 1;
}
}else{
if ((future_years - years) <0){
remaining_years = 0;
}else{
remaining_years = future_years - years;
}
}
}
// Gets the date and time from the ds1307
void getDateDs1307(byte &get_second,
byte &get_minute,
byte &get_hour,
byte &get_dayOfWeek,
byte &get_dayOfMonth,
byte &get_month,
byte &get_year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
get_second = bcdToDec(Wire.read() & 0x7f);
get_minute = bcdToDec(Wire.read());
get_hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
get_dayOfWeek = bcdToDec(Wire.read());
get_dayOfMonth = bcdToDec(Wire.read());
get_month = bcdToDec(Wire.read());
get_year = bcdToDec(Wire.read());
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
thanks for all your assistance! 