Hello fiends. I want to make a gift to my wife on 14th February and I have troubles with my project.
Oh, forgot to tell you that I'm begginer and I'm trying to calculate how many years, months and days we are together and show it on 7seg displays.
I cuted sodes from different projects and now I can show on LCD difference this difference, but I forgot about leap years. Can anyone help me to upgrate my code?
I test in proteus, and I ordered PCB yesterday. I very want to finish within 2 months. ![]()
#include <Wire.h> //Libraries to communicate with RTC
#include "RTClib.h"
RTC_DS1307 rtc; //create rtc object
int segs[] = {0, 1, 2, 3, 4, 5, 6, 7};//abcdefg. segments
int digits[] = {12, 13, 10, 11, 8, 9}; //number of digits
String num[] = {"00000011", //0
"10011111", //1
"00100101", //2
"00001101", //3
"10011001", //4
"01001001", //5
"01000001", //6
"00011111", //7
"00000001", //8
"00001001" //9
};
DateTime dtBegin (2010, 6, 27, 0, 0, 0); //!!!! adjust time here!!!!!
TimeSpan Timepassed;
int Dayspassed, Yearpassed, Monthpassed, DaysTotal;
void setup()
{
rtc.begin();//begin rtc communication
for (int i = 0; i < 8; i++)
{
pinMode(segs[i], OUTPUT);//set segment pins output
}
for (int i = 0; i < 6; i++)
{
pinMode(digits[i], OUTPUT);//set digts as outputs
}
}
void loop()
{
DateTime nowTime = rtc.now();
Timepassed = nowTime - dtBegin;
DaysTotal = Timepassed.days();
Yearpassed = DaysTotal / 365;
Monthpassed = (DaysTotal - Yearpassed * 365) / 31;
Dayspassed = DaysTotal - Monthpassed * 31 - Yearpassed * 365;
printTime(Dayspassed, Monthpassed, Yearpassed);
}
void printNum(int number)//function to print number
{
for (int i = 0; i < 8; i++)
{
//if (state == true && state1 == true) //if date is displayed show decimal after day
// {
// num[number].setCharAt(7, '1');
// }
// //if (state != true || state1 != true) //if not then no decimal
// {
// num[number].setCharAt(7, '0');
// }
if (num[number].charAt(i) == '1')//set segment high
{
digitalWrite(segs[i], HIGH);
}
else
{
digitalWrite(segs[i], LOW);//set low
}
}
}
void printTime(int day, int month, int year)
{
int d[] = {floor(day / 10), day - 10 * floor(day / 10), floor(month / 10), month - 10 * floor(month / 10), floor(year / 10), year - 10 * floor(year / 10)};
for (int i = 0; i < 6; i++)
{
if (i != 0)
{
digitalWrite(digits[i - 1], LOW);
}
if (i == 0)
{
digitalWrite(digits[5], LOW);
}
if (i == 1)
{
// state = true;
}
else
{
// state = false;
}
digitalWrite(digits[i], HIGH);
printNum(d[i]);
delay(5);
}
}
I found this topic on same library, may be it can help me?
#include "RTClib.h"
RTC_DS3231 rtc; // Using DS3231 Real Time Clock
DateTime now = rtc.now(); // Current Date and Time
DateTime dob = DateTime(1984,8,16,0,0,0); // Date of Birth (1984.08.16 00:00:00)
int t0 = dob.year() * 12 + dob.month() - 1;
int t = now.year() * 12 + now.month() - 1;
int dm = t - t0;
int Y;
int M;
int D;
//Calculate age in Y/M/D
if(now.day() >= dob.day()){
Y = floor(dm/12);
M = dm % 12;
D = now.day() - dob.day();
}
else {
dm--;
t--;
Y = floor(dm/12);
M = dm % 12;
DateTime tmp = DateTime(floor(t/12),(t%12)+1,dob.day(),0,0,0);
D = (now.unixtime() - tmp.unixtime())/60/60/24;
}
14th_February.ino (2.46 KB)