RTC syntax for time functions

Hello,

i am working with DS3231 RTC module. I made my own module and i am using only SCL and SDA pins, because i need only accurate clock. I configured clock and RTC library without any bigger problems so now i can see correctly setted time and date via serial interface.

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

Now i want to use this with some other I/O devices but i don't know how. For example, i would like to turn LED on at 20h and turn it off at 6h every day. I can't find any similar tutorials to learn how to do that, because this is the first time i am working with RTC.

I wonder if this should look something like this?

if (now.hour() >= 20) digitalWrite (led, HIGH);

if      (now.hour() >= 20) digitalWrite (led, HIGH);
else if (now.hour() <   6) digitalWrite (led, HIGH);
else                       digitalWrite (led, LOW);

fungus:

if      (now.hour() >= 20) digitalWrite (led, HIGH);

else if (now.hour() <   6) digitalWrite (led, HIGH);
else                       digitalWrite (led, LOW);

Thank you for quick response!

I tried it with minutes and seconds and it works! So, if i understand it correctly, all tasks, which are time depended, could be treated like this (since i don't need alarms).

Some things like digitalWrite() are idempotent (which means calling them several times in a row with same
arguments is equivalent to calling once only), and these can be done this way.

If you have something non-idempotent (such as outputing a beep) you would probably need to test for the
time havnig changed since the last test.

Say you want an alarm at 15:00, code like this:

void loop ()
{
  ...
  if (hour == 15 && minute == 0)
    beep () ;
  ..
}

Would cause repeated beeping for a whole minute (since loop() gets run again and again).

Ok, good to know! I will try..

I would like to add two push buttons to set hours and minutes only. I tried to combine a lot of different tutorial codes from web but i can't change only hours or minutes. If i want to change only hour, i can't update the time in that format. I would like to use only LCD screen and 2 push buttons, not the whole keypad with menu options for complete date and time setting. Like simple car clock.

Idea is to read current hour setting and update it for 1 hour on push button click (the same for minute). Something like this "if (button == HIGH) now.hour()++"

Currently i am trying to change this part of code..

void setDateTime(){

  byte second =      0; //0-59
  byte minute =      33; //0-59
  byte hour =        19; //0-23
  byte weekDay =     7; //1-7
  byte monthDay =    9; //1-31
  byte month =       6; //1-12
  byte year  =       13; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

.. to something like this

void setDateTime(){

  byte hour = now.hour()++;

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(hour));

  Wire.write(zero); //start 

  Wire.endTransmission();

Is even possible to read current hour and update it that way, without resetting whole time and date format?