Hi @arshath786
you put the if-condition into the function TimePeriodIsOver
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
if ("minuteOfDay" == 0) {
digitalWrite(12, HIGH);
}
}
There is a spelling-error
you wrote minuteofDay
but it has to be
minuteOfDay
CAPITAL "O"
What was the reason for you to put it inside this function?
The condition that you defined itself does not work
if ("minuteOfDay" == 0) {
what your condition is
compare if fixed string with charactersequence minuteofDay is the same as number zero
which of course is never true
the double-hyphen make the characters a string
"minuteOfDay"
it works if you code it without the double-hyphen
if ("minuteOfDay" == 0) { // double-hyphen is WRONG
if (minuteOfDay == 0) { // WITHOUT double-hyphen it works
but you should not put it into the function TimePeriodIsOver
This would counteract massively on the concept that each function does one thing
The function TimePeriodIsOver does what its name says:
check if a certain period of time is over
The name says absolutely nothing about:
"and if its midnight switch on a LED!"
You would have almost hidden away the LED-switching and you and everybody else would have to analyse the code very very carefully to detect ah ! here the LED is switched on !
You should use self-explaining names for everything
for two reasons
-
- it makes the code easier to read
-
- if you change the value that is represented by this name there is exactly one place to change the value
example: you want to change the LED from IO-pin 12 to IO-pin 8
With a self-explaining name you can't forget the third place where you have coded "12" to change it to "8" because there is only one place to change it
...
pinMode(12, OUTPUT);
...
digitalWrite(12,LOW);
...
if (minuteOfDay == 0) {
digitalWrite(12,HIGH);
}
changes to
.
...
pinMode(8, OUTPUT);
...
digitalWrite(8,LOW);
...
if (minuteOfDay == 0) {
digitalWrite(8,HIGH);
}
.
.
const byte LED_Pin = 12; // define constants ofr IO-pin numbers
in combination with
...
pinMode(LED_Pin, OUTPUT);
...
digitalWrite(LED_Pin,LOW);
...
if (minuteOfDay == 0) {
digitalWrite(LED_Pin,HIGH);
}
changes to
const byte LED_Pin = 8; // define constants ofr IO-pin numbers
and you are done !
Because at all other places in your code the name LED_Pin is used
.
.
I defined to new constants
const int MOD_to_Switch_On = 0 * 60; // M)inutes-O)f-D)ay
const long SOD_to_switch_off = 1 * 60 + 10; // S)econds-Of-D)ay at 00:01:10
which are used to switch on / off the LED at the bottom of loop
if (minuteOfDay == MOD_to_Switch_On) {
digitalWrite(LED_Pin, HIGH);
}
if (secondsOfDay == SOD_to_switch_off) {
digitalWrite(LED_Pin, LOW);
}
The self-explaining names make commenting obsolete
The code explains itself
if it does not occur regular but only from time to time I suspect this is a bad signal problem or a wrong RTC-type problem
the constructor has anoption for a second parameter
define explicitly the your RTC-type
This name "URTCLIB_MODEL_DS1307" is defined in the uRTC.h-file of the library
// uRTCLib rtc; // URTCLIB_MODEL_DS1307
//uRTCLib rtc(0x68);
uRTCLib rtc(0x68,URTCLIB_MODEL_DS1307); // define EXPLICIT the model
Does the RTC-module have pullup-resistors?
On the I2C-bus do all pins have secure contact?
So here is a version with the modifications like describes above
/*structure (= function-call hierachry) of the code
setup()
- calls setRTC_Time()
loop()
- calls UpDateTime()
- calls rtc.year() etc.
- calls TimePeriodIsOver()
- conditionally calls PrintTime()
*/
#include "Arduino.h"
#include "uRTCLib.h"
// uRTCLib rtc; // URTCLIB_MODEL_DS1307
//uRTCLib rtc(0x68);
uRTCLib rtc(0x68,URTCLIB_MODEL_DS1307); // define EXPLICIT the model
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int myYear;
int myMonth;
int myDay;
int myDayOfWeek;
int myHour;
int myMinute;
int mySecond;
int minuteOfDay;
// 23*3600+59*60+59 = 86399 which is a to big number for int
long secondsOfDay;
const int MinOfDay_9OClock = 9 * 60;
const int MOD_to_Switch_On = 0 * 60; // M)inutes-O)f-D)ay
const long SOD_to_switch_off = 1 * 60 + 10; // S)econds-Of-D)ay at 00:01:00
unsigned long myTimer;
const byte LED_Pin = 12; // define constants ofr IO-pin numbers
void setup() {
Serial.begin(9600);
delay(3000); // wait for console opening
pinMode(LED_Pin, OUTPUT);
URTCLIB_WIRE.begin();
setRTC_Time();
}
void loop() {
UpDateTime();
// check if more than 1000 milliseconds have passed by
// since last time the 1000-ms-period-was-over
if ( TimePeriodIsOver(myTimer, 1000) ) {
// if REALLY 1000 milliseconds have passed by
PrintTime();
}
if (minuteOfDay == MOD_to_Switch_On) {
digitalWrite(LED_Pin, HIGH);
}
if (secondsOfDay == SOD_to_switch_off) {
digitalWrite(LED_Pin, LOW);
}
}
void setRTC_Time() {
// Comment out below line once you set the date & time.
// Following line sets the RTC with an explicit date & time
// for example to set January 13 2022 at 12:56 you would call:
myYear = 22;
myMonth = 1;
myDay = 13;
myDayOfWeek = 7;
myHour = 23;
myMinute = 59;
mySecond = 50;
// using variables makes comments obsolete
//void uRTCLib::set(const uint8_t second, const uint8_t minute, const uint8_t hour, const uint8_t dayOfWeek, const uint8_t dayOfMonth, const uint8_t month, const uint8_t year) {
//rtc.set( mySecond, myMinute, myHour, myDayOfWeek, myDay, myMonth, myYear) ;
rtc.set(mySecond, myMinute, myHour, myDayOfWeek, myDay, myMonth, myYear) ;
// set day of week (1=Sunday, 7=Saturday)
}
void UpDateTime() {
rtc.refresh();
myYear = rtc.year();
myMonth = rtc.month();
myDay = rtc.day();
myHour = rtc.hour();
myMinute = rtc.minute();
mySecond = rtc.second();
minuteOfDay = myHour * 60 + myMinute;
secondsOfDay = myHour * 3600 + myMinute * 60 + mySecond;
}
void PrintTime() {
Serial.print("Current Date & Time: ");
Serial.print(myYear);
Serial.print('/');
Serial.print(myMonth);
Serial.print('/');
Serial.print(myDay);
Serial.print(" (");
Serial.print(daysOfTheWeek[rtc.dayOfWeek() - 1]);
Serial.print(") ");
Serial.print(myHour);
Serial.print(':');
Serial.print(myMinute);
Serial.print(':');
Serial.print(mySecond);
Serial.println();
Serial.print("minuteOfDay:");
Serial.print(minuteOfDay);
Serial.print(" secondsOfDay:");
Serial.println(secondsOfDay);
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
If you want to understand the function TimePeriodIsOver you can read this tutorial about non-blocking timing