I am using an UNO with a RTC to control a light board. I'm using a simple control structure to decide when to turn things off and on. It's my understanding that a time_t
value is just a number (number of seconds since 1970), and that I should be able to compare these time_t
values as I would any integer- in this case comparing two time_t
values to see which one is larger.
#include <Wire.h> // Needed for I2C protocol (RTC uses I2C)
#include "RTClib.h" //Good Library for RTC functions
#include <Time.h> // Needed for time_t variables and tmElements
RTC_DS3231 rtcVM; // From the RTClib library for use with an DS3231 RTC. Creates an object called rtcVM
DateTime nowVM; // Declare a Date Time object that will be used to hold the current time
time_t nowUniVM;// Declare a time_t object (Unix time) which is a float value (good for straight comparisons) to hold current time
time_t starter;// Declare a time_t object for the start time (time_t)
tmElements_t MagStart; // Declare a tmElement_t object to manually input the start Time
//FILL OUT START TIME AND DATE FOR FIRST SAMPLE (NO LEADIN ZEROS--"8" not "08")
byte startHour = 17;
byte startMin = 58;
byte startSec = 0;
byte startDay = 12;
byte startMonth = 10;
int twoDigYear = 16; // Enter last two digits of year
byte startYear = twoDigYear + 30 ; // This is a little confusing. This is the year offset from 1970 (AKA 2016 should be input as 46. Or just add 30 to last two digits).
void setup() {
Serial.begin(9600);
Wire.begin();
rtcVM.begin();
// Creates a tmElement by putting together all of Maggies Start Time and Dates
MagStart.Hour=startHour;
MagStart.Minute = startMin;
MagStart.Second = startSec;
MagStart.Day = startDay;
MagStart.Month = startMonth;
MagStart.Year = startYear;
// Takes the tmElement and converts it to a time_t variable. Which can now be compared to the current (now) unix time
starter = makeTime(MagStart);
// Start message
Serial.println("Start time is: ");
Serial.print(hour(starter));
Serial.print(":");
Serial.print(minute(starter));
Serial.print(":");
Serial.print(second(starter));
Serial.print(" ");
Serial.print(month(starter));
Serial.print("/");
Serial.print(day(starter));
Serial.print("/");
Serial.println(year(starter));
Serial.println("****************************");
}
void loop() {
nowVM = rtcVM.now(); // get current time and hold in "nowVM"
time_t nowUniVM = nowVM.unixtime(); // Calculate current time in Unix time
samplerStarter();
// RTC Get and Display
DateTime now = rtcVM.now();// Getting the current Time and storing it into a DateTime object
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(3000);
//Use these 2 to check the numerical values for comparisons
Serial.println(starter);
Serial.println(nowUniVM);
}
void samplerStarter() {
if (starter < nowUniVM) {
//if (1 < 2) {
Serial.println("Start Time");
}
}
I've included my entire test code here, but it's the last if statement that puzzles me. The idea behind that last statement is that it should print out "Start Time" when the start time is reached. The statement works if I compare something simple like (1 < 2), and the statement gets printed. So I assumed it would work if I did the same thing but with two different time_t
values.
But if I input an appropriate start time under the "Fill Out Start Time" section, the print statement is not triggered when the start time comes to pass (AKA- when the start time (starter) is less than the current time (nowUniVM
).
Is there something about time_t values that does not allow them to be compared like this? Or is there something in the way I'm creating my values that is messing up the comparisons?
My output to the serial monitor shows the actual time_t
values, and they look like normal integers to me when they print out, so I'm not sure whats going on here. Why doesn't that last if statement print out when the start time is reached?
Time.h
?