3

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?

  • Please edit your question and state what error, if any, you are concerned about. As is, your question provides some general background but it doesn't say what is wrong, or what is different from what you expected. – James Waldby - jwpat7 Oct 13 '16 at 5:07
  • Thanks for the feedback, I made a few edits to further explain what I'm looking for. – Vinterwoo Oct 13 '16 at 7:29
  • 1
    Where did you get this Time.h? – KIIV Oct 13 '16 at 8:09
  • Version 1.5 from the IDE Library downloader – Vinterwoo Oct 13 '16 at 16:46
3

You have two variables called nowUniVM. The fist one is a global declared at the top of your program, and implicitly initialized to zero by the compiler. Since you never update it, it is always zero. The second one is local to loop(): it shadows the global variable inside loop() and is updated periodically.

The function samplerStarter() only sees the global variable, which is zero. Thus, the comparison boils down to

if (starter < 0)

But since time_t is an unsigned type, the test can never be true.

  • Scope! Bad practice on my part – Vinterwoo Oct 13 '16 at 16:45
  • Though a quick question on scope in this matter. I defined the "starter" variable in my "setup" function. How is it that the "void loop" and "sampler starter" functions can see this "starter" variable, if void functions don't return any values? – Vinterwoo Oct 13 '16 at 17:27
  • 1
    You defined starter at global scope. You initialized it in setup(), which is perfectly fine. – Edgar Bonet Oct 13 '16 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.