Unix Time 'if' statements

Apologies if this post is in the wrong section. this is m first project.
So I am working on a basic aquarium controller using an Arduino Uno to control LEDs via PWM according to time of day defined by RTC

I have started on the code with the help of the RTC Library, this prints to serial monitor the time each second as desired, i now wish to add an if statement.
eg;

IF [Time] = [Defined Time]
Turn LEDs On

So my first try was:

  if (rtc.getTime()) == 12:00:00)
  Serial.print("***LED ON***");
  Serial.print('\n');

Note I will replace Serial.print once i get i working

It seems that whatever format i write the time it doesn't work. so I tried Unix time:

  if (rtc.getUnixTime(rtc.getTime()) == 1459255200)
  Serial.print("***LED ON***");
  Serial.print('\n');

This worked but Unix time includes date too, which isn't very helpful as i want this to run at the same time every day.

Is there a way to use unix and ignore date? or a better way achieve this?
Thanks

See below my code so far:

// Aquarium Control // Dean Cohen //

// v0.1 Testing PWM control of LED Driver
// v0.2 Adding DS3231 RTC, Configuring date/time, Show in serial monitor

//Connect 'Dim' wire from LED Driver to pin 10 
//Conect GND to Driver input GND
//Connect (RTC-Arduino): GND-GND, VCC-3.3v, SDA-A4, SCL-A5

#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231  rtc(A4, A5);

int led = 10;

  void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  rtc.begin();

  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(11, 47, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(29, 03, 2016);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Send Unix Time
  Serial.print(rtc.getUnixTime(rtc.getTime()));
  Serial.print('\n');
  
  // Wait one second before repeating
  delay (1000);

  
  if (rtc.getUnixTime(rtc.getTime()) == 1459255200)
  Serial.print("***LED ON***");
  Serial.print('\n');


//PWM LED code ignored for now
//analogWrite(led, 64);
//delay(1000);
//analogWrite(led, 127);
//delay(1000);
//analogWrite(led, 191);
//delay(1000);
//analogWrite(led, 255);
//delay(1000);
}

I can't tell which RTC library you are using. There are a couple floating around.

Sorry, didnt think to add that.
The Library I used is this:
http://www.rinkydinkelectronics.com/library.php?id=73
Thanks

If you dig through the source code of the library, you will find a method char *DS3231::getTimeStr(uint8_t format).

Not tested but the below might do what you need

// pointer to a time string
char *strTime;

// get formatted time (and print)
strTime = rtc.getTimeStr(FORMAT_LONG);
Serial.print("Time is "); Serial.println(strTime);

// compare time with a given time
if(strcmp(ptr, "12:00:00") == 0)
{
  // it's time to do something
}

You can also pass FORMAT_SHORT in which case you get HH:mm back instead of HH:mm:ss (as far as I understand it).

sterretje:
If you dig through the source code of the library, you will find a method char *DS3231::getTimeStr(uint8_t format).

Not tested but the below might do what you need

// pointer to a time string

char *strTime;

// get formatted time (and print)
strTime = rtc.getTimeStr(FORMAT_LONG);
Serial.print("Time is "); Serial.println(strTime);

// compare time with a given time
if(strcmp(ptr, "12:00:00") == 0)
{
 // it's time to do something
}




You can also pass FORMAT_SHORT in which case you get HH:mm back instead of HH:mm:ss (as far as I understand it).

Thank you for your reply, as i am a bit unfamilliar with everything still would you mind explaining things a bit more?
I kind of understand all but this section

// compare time with a given time
if(strcmp(ptr, "12:00:00") == 0)

what do: 'strcmp' and 'ptr' mean?

I gave it a go and came up with this error

Arduino: 1.6.8 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/deancohen/Documents/Arduino/Aquarium_Control_0.3/Aquarium_Control_0.3.ino: In function 'void loop()':
Aquarium_Control_0.3:60: error: 'ptr' was not declared in this scope
 if(strcmp(ptr, "12:00:00") == 0)
           ^
exit status 1
'ptr' was not declared in this scope

Thanks :slight_smile:

So after a bit of research i think i got it to work! thanks for your help.

so strcmp is String compare? ptr i assume was supposed to be 1 string (for my purpose i did strTime) and set the time to compare this with which would be my string 2, if equal this allows my command to run. (excuse the basic language)

// compare time with a given time
if(strcmp(strTime, "17:31:00") == 0)
  Serial.print("DOES THIS WORK");

Is this the right way to approach this?
Many thanks for your help!

Sorry for the mistake; I usually call those things ptr (pointer) because that is what they are (pointer to a character array).

It's the correct approach.

strcmp is one function in the family of function to process strings (lowercase s); you probably found out that it stands for stringcompare. strstr can be used to find a string in a string, strchr and strrchr to find a character in a string and strtok to parse a string (internally it basically uses strchr so you can achieve the same with strchr).

It's the good old way of working with strings (lowercase s) in C.

If you download a library and install it, you can always look at the .h file to see which function, variables and defines are available to the program that needs to use it. For the details, you can look in the .cpp file; that is where I found what FORMAT_LONG and FORMAT_SHORT actually do.

No worries, I managed to figure it out anyway and you helped a lot :slight_smile:

Thank you for the explanation, I prefer to understand things than blindly copy/paste peoples code.
I never knew to look into the library files! so I've learnt that too.

Below is what I have so far; And a serial monitor snippet so I know it works :slight_smile:

// Aquarium Control // Dean Cohen //

// v0.1 Testing PWM control of LED Driver
// v0.2 Added DS3231 RTC, Configuring date/time to show in serial monitor (Thanks to http://www.rinkydinkelectronics.com/library.php?id=73)
// v0.3 Configured to use real time instead of unix time (Thanks to sterretje)

//Connect 'Dim' wire from LED Driver to pin 10 
//Connect GND to Driver input GND
//Connect (RTC-Arduino): GND-GND, VCC-3.3v, SDA-A4, SCL-A5

  #include <DS3231.h>
// Init the DS3231 using the hardware interface
  DS3231  rtc(A4, A5);

  int led = 10;

  void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  rtc.begin();

  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(TUESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(11, 47, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(29, 03, 2016);   // Set the date to January 1st, 2014
  }

  void loop()
  {
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Send Unix Time
  Serial.print(rtc.getUnixTime(rtc.getTime()));
  Serial.print('\n');
  
  // Wait one second before repeating
  delay (1000);

  //  Not used - if Unix time
  //  if (rtc.getUnixTime(rtc.getTime()) == 1459255200)
  //  Serial.print("***LED ON***");
  //  Serial.print('\n');

  // pointer to a time string
  char *strTime;
  // Get formatted Time 
  strTime = rtc.getTimeStr(FORMAT_LONG);

  // compare time with a given time
  if(strcmp(strTime, "18:52:00") == 0)
  Serial.print("***LED ON***");
  Serial.print('\n');

//PWM LED code ignored for now
//analogWrite(led, 64);
//delay(1000);
//analogWrite(led, 127);
//delay(1000);
//analogWrite(led, 191);
//delay(1000);
//analogWrite(led, 255);
//delay(1000);
}
Tuesday 29.03.2016 -- 18:51:58
1459277518

Tuesday 29.03.2016 -- 18:51:59
1459277519
***LED ON***
Tuesday 29.03.2016 -- 18:52:00
1459277520

Tuesday 29.03.2016 -- 18:52:01
1459277521

I'm quite newbee to Arduino but I think You made it more complicated with pointers than it really is...

Mentioned library does not only return time string but also time object (compound data) with separate info:

Time.sec
Time.min
Time.hour
Time.dow
Time.date
Time.mon
Time.year

before setup{} if You want to use it in whole project (in setup and loop) or within loop{} if only in loop You shall declare

Time MyTime;

then in loop{}

MyTime=rtc.getTime();  // get RTC compund data (object)

MySec=MyTime.sec;  // extract seconds - not used in condition further below
MyMin=MyTime.min;  //extract minutes
MyHour=MyTime.hour;  // extrac hours

and then

 if (MyHour==alarm_time_hour && MyMin==alarm_time_min) {digitalWrite(9,HIGH);}  // set alarm ON

of course "alarm_time_hour" and "...min" are values and may be hard coded or interactively entered (keyboard?)

hard-coded example

 if (MyHour==13 && MyMin==15) {digitalWrite(9,HIGH);}  // set alarm ON quarter past 1pm (13:15)

works perfectly well...

Rgds
Gregory

Excuse me, but you seem to have overlooked the beauty of Unix time. If you want to compare Unix times, you can do it directly with 32 bit math. So you really could say:
if (Time == definedTime)but it would be safer to compare to see if it has passed, in case your check misses some seconds:

if (Time >= definedTime)