Hello,
I am working on a project, where I use the DS1307 RTC module. To be specific, I have a date for example 2020.07.10. 14:40. What I'm trying to achieve, is to display the difference between the current date and that example one. So far, I have used now.unixtime() and subtracted a constant from it. This worked perfectly with days. The constant was the elapsed seconds between now.unixtime() and the example date.
unsigned long result = now.unixtime() - diff;
I wanted to display the result in days, months and years. (YY:MM:DD) Days are easy to work with as I only have to divide with 86400. But because months can be 28, 29, 30, or 31 days I was faced with this problem. Fortunately, I figured out something: I count the days from 0 to the current day, then at a certain point let's say 31, I increment the month counter by 1. After that, I add the next month's number to the previous one and so on... till I reach 12 where I close the loop. It is getting kinda complicated so I attach the code to this topic.
I used a for loop, but I think somethin went wrong there, as the "honap" variable overflows rapidly, and if I only execute the for loop when the day changes, that variable counts everything, and I does not work as it supposed to do.
#include <RTClib.h>
#include <Wire.h>
RTC_DS1307 rtc;
unsigned int prevv;
boolean happened = false;
const unsigned long diff = 1598063880;
int honap;
int kamunap;
unsigned long ev;
int prevday;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.adjust(DateTime(2020, 11, 10, 2, 37, 50));
}
void loop() {
// if (beallitobanvan1 == true) setora();
DateTime now = rtc.now();
unsigned long result = now.unixtime() - diff;
unsigned int nap = result / 86400;
int honapszam = now.month();
int evszam = now.year();
kamunap = nap;
if (prevday != nap) happened = true;
if (happened == false) {
for (int i = 0; i < nap; i++) {
if (i < 31) kamunap = nap;
if (i = 31) {
honap++;
kamunap = 0;
}
if (31 < i < 61) kamunap = nap - 31;
if (i = 61) {
honap++;
kamunap = 0;
}
if (61 < i < 92) kamunap = nap - 61;
if (i = 92) {
honap++;
kamunap = 0;
}
if (92 < i < 122) kamunap = nap - 92;
if (i = 122) {
honap++;
kamunap = 0;
}
if (122 < i < 153) kamunap = nap - 122;
if (i = 153) {
honap++;
kamunap = 0;
}
if (153 < i < 184) kamunap = nap - 153;
if (i = 184) {
honap++;
kamunap = 0;
}
if (184 < i < 213) kamunap = nap - 184;
if (i = 213) {
honap++;
kamunap = 0;
}
if (213 < i < 244) kamunap = nap - 213;
if (i = 244) {
honap++;
kamunap = 0;
}
if (244 < i < 274) kamunap = nap - 244;
if (i = 274) {
honap++;
kamunap = 0;
}
if (274 < i < 305) kamunap = nap - 274;
if (i = 305) {
honap++;
kamunap = 0;
}
if (305 < i < 335) kamunap = nap - 305;
if (i = 335) {
honap++;
kamunap = 0;
}
if (335 < i < 366) kamunap = nap - 335;
if (i = 366) {
honap++;
ev++;
kamunap = 0;
}
}
happened = true;
prevday = nap;
}
int yy = honap / 12;
int mm = honap % 12;
int dd = kamunap;
}