I'm currently building an anniversary gift for my wife. It's our six-month anniversary on Wednesday the 21. I wanted to build her a wooden box that displays how many days we've been together, so I went and purchased a DS3231 Precision breakout and a 7-segment display w/I2C from Adafruit as well as a servo.
What I'm trying to do is cut a hole in the box at the top where I'll stick out the display to show the days, and on the inside I'll hide the RTC to keep track of the time and the servo will be attached to the latch so it will open on a specific day.
My issue is that I'm new to the world of programming, and thus know nothing about how to make this work. I want the RTC to keep track of how many days have passed since our first date (January 21, 2015) so it can print that to the display. It will all keep working normally until our 12 month anniversary where the servo will move itself and allow the box to be opened.
How can I get the technical know-how within 17 days to make this work?
I've figured most of it out, literally all I need now is help calculating the days passed since 1/21/2015. I know the theory, but I don't know what code to use. Any ideas?
Edit:
I realized that what I should do is calculate the unixtime from 1970 to present date, than the unixtime from the first date than subtract the two and the difference (if i divide each one by 86400) will give me the amount of days from the first date to present. Now all I need to do is program it.
Edit 2:
I can't believe I didn't know anything about programming this morning... I added a variable in setup:
firstDate = DateTime(2015, 1, 21, 0, 0, 0);
and I did the following operation in the loop in order to calculate days since than:
Use the Time library. You can use makeTime() to create the Unix time for 1/21/2015. Subtracting then from now in Unix time gives you the number of seconds. Then just divide by 606024.
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime weddingDay(2015, 1, 21); //EDITED to correct actual wedding date ;)
void setup ()
{
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop ()
{
DateTime now = rtc.now();
TimeSpan marriageDuration = now - weddingDay;
char timeNow[64] = "";
sprintf(timeNow, "%s\t%2d/%02d/%d\t%2d:%2d:%2d\n", daysOfTheWeek[now.dayOfTheWeek()], now.month(), now.day(), now.year(), now.hour(), now.minute(), now.second());
Serial.print(timeNow);
sprintf(timeNow, "Enjoying %d days of wedded bliss...\n", marriageDuration.days());
Serial.print(timeNow);
delay(1000);
}
int daysSince = floor((now.unixtime() / 86400L) - (firstDate.unixtime() / 86400L));
because unixtime() returns an integer, and your are dividing by an integer, you are performing Integer Division... there is no fractional remainder. So, floor will not affect the result and does nothing.
Alright, I've got most of the software up and ready now. I think this will be my final problem, but I don't know how to make the servo work with the rest of the design. When I plug the servo in to the arduino power the entire thing just ends up failing, how can I power it efficiently? (Hopefully without a transistor...)