Hello,
once again I have another problem this time I'm using an rtc (real time clock) but I want to make an if statment with the time but I don't know how. This is what I tried so far but I got an error.
This is not a String comparison (look up the "comma operator")
if (time >= 13, 00, 00)
My favorite way of keeping track of day time for alarms, timed events, etc. is to use seconds or minutes past midnight. Then a simple integer if statement works well.
Here is a simple demo that uses that technique to print the date and time every five minutes:
//time library demo: 5 minute interval timer
#include "TimeLib.h"
time_t unixTime; // a time stamp
void setup() {
Serial.begin(9600);
// set internal clock time and date.
// setTime(hr,min,sec,day,mnth,yr);
setTime(23,0,0,6,11,2020);
Serial.print("Starting clock. now() = ");
Serial.println(now());
// print as date_time
print_date_time();
// adjust for DST
unixTime = now(); //get the unix timestamp
unixTime += 3600UL; //add DST in spring
setTime(unixTime);
Serial.println("After adding DST offset");
print_date_time();
Serial.println("Starting loop()");
}
void loop() {
static int minutes_past_midnight, last_print_time = 0;
// check the clock
minutes_past_midnight = hour() * 60 + minute();
// every 5 minutes, print the data and time
if (minutes_past_midnight != last_print_time && minutes_past_midnight % 5 == 0) {
last_print_time = minutes_past_midnight;
print_date_time();
}
}
void print_date_time() { //easy way to print date and time
char buf[20];
snprintf(buf,sizeof(buf),"%02d/%02d/%4d %02d:%02d:%02d", day(), month(), year(), hour(), minute(), second());
Serial.println(buf);
}
You will have to do some learning one way or another.
If you want to compare Strings or C-strings (which are not the same thing at all), there are tutorials on line explaining how. Search for "C/C++ compare strings". But you will find that the approach does not work well.
The approach I posted can easily be modified to use the functions built into DS3231.h (like getHour() and getMinute(), which return integer values), and are documented here.