Turn LED on with RTC

Halo
I would like to program Arduino so it can switch on relay (relay will switch on light in the room) each day at 18:00 and switch off at 22:00.
I have Arduino UNO and DS3231 RTC. Im not so familiar with RTC but i did find the skech for RTC:

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // wait until Arduino Serial Monitor opens
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
  if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

How can RTC or Serial command trigger Arduino to set lets say DO3 to HIGH?

Thanx

I would like to program Arduino so it can switch on relay (relay will switch on light in the room) each day at 18:00 and switch off at 22:00.

How can RTC or Serial command trigger Arduino to set lets say DO3 to HIGH?

Your sketch is actually using the functions of the Time Library to access the time. The Time Library is referenced to external time with the RTC, and then periodically synchronized with the RTC to improve the timekeeping of the internal oscillator. This is a good approach.

At its's most basic and simple, you could add two conditional tests

if(hour() == 18) digitalWrite(3, HIGH);
if(hour() == 22) digitalWrite(3, LOW);

There are several refinements to optimize the code to minimize the time checking and redundant digitalWrite() commands, but the basics can get you going.

There is a TimeAlarms library which is good for all this stuff.

cattledog:
Your sketch is actually using the functions of the Time Library to access the time. The Time Library is referenced to external time with the RTC, and then periodically synchronized with the RTC to improve the timekeeping of the internal oscillator. This is a good approach.

At its's most basic and simple, you could add two conditional tests

if(hour() == 18) digitalWrite(3, HIGH);

if(hour() == 22) digitalWrite(3, LOW);




There are several refinements to optimize the code to minimize the time checking and redundant digitalWrite() commands, but the basics can get you going.

It work. Thank you.
I didnt know how to save Serial data but it is really easy. Now i can continue with my work

So I have different problem now. On 12.Nov evrything was fine and working. Because I need 18 DO I took MEGA and connect RTC on it. I started the program but time is not right. It is showing 11.11.19 and -1hour differance. Last time I only started the skatch (on UNO) and the time was from the start OK. Now it is 4 days and 1 hour in the past...

OK. I mannage to set the time again (with DS1307RTC->SetTime sketch). But way did it lose time in the first place?

I'm making a Project that will count how many days is left till Christmas and with the help of RTC it need to turn on specific LED between 18:00 - 22:00 and on LCD show how many days is left.

What can I do to stop RTC from loosing time?

I would think that the RTC is not running on battery power, and keeping time when the Arduino is not connected to a power source.

Is the battery new? What voltage is it reading? Is it installed so you can see the +?

When you were on the UNO, could you turn the Arduino off, wait a period of time, and see the correct time?

Do you have a multimeter, and can check if Vbat is present at the chip? You will need to look at a data sheet for the ds3231 to check which pin of the chip that is. Sometimes there have been defective battery holders or traces on the module.

The last half a hour arduino was disconneted. I just look at the time. It is OK. Tommorow I will see if the time OK is and test the battery

So RTC is working but I have same strange problem with the code now. The idea is to make advent wreath that will turn on LED as the day pass. This part is working. But I would like to turn off all LEDs with a button. That part is not working. Here is my code so far:

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#define DS3231_I2C_ADDRESS 104

const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};
const int ledOnOff = 2;
int buttonState = 0;

void setup()  {
 for(int x = 0; x < 19; x++){ //set all LEDs als OUTPUT
   pinMode(ledPins[x],OUTPUT);
 }
 pinMode(ledOnOff,INPUT);

Wire.begin();
 Serial.begin(9600);
 while (!Serial) ; // wait until Arduino Serial Monitor opens
 setSyncProvider(RTC.get);   // the function to get the time from the RTC
 if(timeStatus()!= timeSet)
    Serial.println("Unable to sync with the RTC");
 else
    Serial.println("RTC has set the system time");   

void loop() {
 buttonState = digitalRead(ledOnOff);
 Serial.println(buttonState);
 delay(20);
  if (timeStatus() == timeSet) {
   digitalClockDisplay();
 } else {
   Serial.println("The time has not been set.  Please run the Time");
   Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
   Serial.println();
   delay(4000);
 }
 
 delay(1000);

 while(buttonState == 0){ //the idea is to turn all LED with a button
 //turning on LEDs for each day in first week
 
  if(day() > 3 && day() < 10){
   for(int x = 0; x<=day()-4; x++){ //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
     digitalWrite(ledPins[x],HIGH);
     }
   }
  else if(day() > 10 && day() < 17){
   for(int x = 0; x<=day()-5; x++){ //same as above only we have one more sunday and thats way it is -5  
     digitalWrite(ledPins[x],HIGH);
     }   
  }
 }
}
//Funktion for RTC
void digitalClockDisplay(){
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(month());
 Serial.print(" ");
 Serial.print(year());
 Serial.println();
}

//Funktion for RTC
void printDigits(int digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits);
}

The problem is when I push a button in SerialMonitor I can see the code did only one loop. Strange. It looks like it program make only one run. I changed while with if and the program is looping and I can see that buttonState is 1 but nothing happens. LEDs are on. What Im I doing wrong?

How is the button wired? What is the digitalRead() value of the pressed and not pressed state?

It is important that the input signal is not "floating or undefined" when the button is not pressed. To prevent this, there needs to be a pullup or pulldown resistor to tie the unpressed button to a known state.? What is the digitalRead() value of the pressed and not pressed state?

I recommend that you wire the button using the internal pullup resistor which is engaged with pinMode(pin,INPUT_PULLUP). Then wire diagonally across the button between the input pin and ground. The button will read HIGH when not pressed, and LOW when pressed.

Please confirm the hardware situation.

Then we can proceed with reading when the button becomes pressed. That is the state change from unpressed to pressed. When that happens we know to turn on the leds.

To make the program responsive so it is quick to read a button press, there should not be a use of delay in the program. Remove the delay(1000) line in the code. Instead, the digitalClockDisplay() needs to be on a millis() based timer like used in the "Blink without delay" example of the ide. Here's for a one second update, it can be made longer by changing the displayInterval.

void digitalClockDisplay() {
  // digital clock display of the time
  static unsigned long lastDisplay = 0;
  const unsigned long displayInterval = 1000;
  if (millis() - lastDisplay >= displayInterval)
  {
    lastDisplay = millis();
    Serial.print(hour());
    printDigits(minute());
    printDigits(second());
    Serial.print(" ");
    Serial.print(day());
    Serial.print(" ");
    Serial.print(month());
    Serial.print(" ");
    Serial.print(year());
    Serial.println();
  }
}

I found the problem. it was stupid mistake. I wrote ifniite loop. That was the problem. I didnt put exit condition. On the weekend i will have final version of the program. I will post it with explanation what is shoud do. Because my coding skills are limited I would like you look in to it and tell me is there a way to make it shorter...

So here is my code. Some stuff work, some not. The idea is to wreath does the following:

  1. Turn LEDs on each day between 5 and 10 pm and I shoud be able to turn it on when ever I wont (with Smartphone and Blynk app or with Remote Controler)

  2. For each day the wreath needs to turn on 1 LED. I have total 18 LED (for 18 days between first Advent sunday and last Advent sunday). For sundays I dont have a LED because we light on the real candle.

  3. The Wreath has also a LCD Display that count down how many days is left till Christmas. Time recieve from RTC modul.

  4. With the help of Blynk and remote control I can switch between LEDs blinking patters. There are 3 patterns.

  5. I use Arduino Nano with ESP to connect Blynk app and Arduino Mega where all LEDs are connected. On Arduino Nano I used pin D2 - D4 and programed als Output. Pins D2-D4 on Mega are Inputs.

Here is my 1. Problem. I connected Inputs D2-D4 to pull-down resistors but when Arduino Nano send "1" or 5V, the output on Mega is 1 but if the digital input 0 ist, then Digital Input is not working properly. Althoug I connected input to pull down Resistor, Input is still switching states.

  1. Problem is that Remote control dont do anything. It supost to swith patters but it doesnt do that.
    I am still troubleshooting but if you have any idea, be free to share it.

Here is my code so far:

// ++++Smart Advent Wreath++++
/*After the first Sunday the program will start to work. It will do 2 things: 1. it will count how many days it is left till Christmass and show it on LCD. 
  2. for each day it will light up a led starting with 2.Dec (Monday). I dont have a LED for Sundays because on Sundays we light up a real candle. 
  For time i connected RTC modul. The RTC is also zaduzen for turning all LED after 18:00 and turning off after 22:00. I dont wont to be on all the time.
  Because I have 18 LEDs (from 2.12 till 21.12, without sundays) I used MEGA (didnt wont to have LED in matrix)
  With the help of Arduino NANO I can turn off LEDs
  The are 4 patterns how LEDs can blink
  Patterns can change with the Blynk app or with the remote control 
 */
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>                // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DS3231_I2C_ADDRESS 104
#define ON_OFF 2
#define PATTERN_1 3
#define PATTERN_2 4
#define PATTERN_3 5

const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};  //defining on which pins are LEDs
int ledOnOff = 0;
int ledPattern1 = 0;
int ledPattern2 = 0;
int ledPattern3 = 0;


const int recvPin = 7;
IRrecv irrecv(recvPin);
decode_results results;
unsigned long key_value = 0;

int daysTillChristmas = 0;
int pin2State, wreathOnOff, lastPin2State = 0;



void setup()  {
  for(int x = 0; x < 19; x++){          //set all LEDs als OUTPUT
    pinMode(ledPins[x],OUTPUT);
  }


  //this Inputs are activated from Arudino NANO and Blynk app
  pinMode(ON_OFF,INPUT);
  pinMode(PATTERN_1,INPUT);    
  pinMode(PATTERN_2,INPUT);
  pinMode(PATTERN_3,INPUT);
  
  //RTC && LCD
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);                    // wait until Arduino Serial Monitor opens
    setSyncProvider(RTC.get);         // the function to get the time from the RTC
    if(timeStatus()!= timeSet)
      Serial.println("Unable to sync with the RTC");
    else
      Serial.println("RTC has set the system time");   
  
  //REMOTE CONTROL 
  irrecv.enableIRIn();
  irrecv.blink13(true);
  
  //LCD
  lcd.init();
  lcd.backlight();
}

void loop()
{
  daysTillChristmas = 25 - day(); //for LCD count down

  //i did this because i would like to turn wreath on/off with one single button in the Blynk app
  pin2State = digitalRead(ledOnOff);
  if(pin2State != lastPin2State){
    if(pin2State == 1){
      wreathOnOff = 1;
    }
    else{
      wreathOnOff = 0;
    }
  }
  ledPattern1 = digitalRead(PATTERN_1);
  ledPattern2 = digitalRead(PATTERN_2);
  ledPattern3 = digitalRead(PATTERN_3);
  
  

  //Seting up a LCD
  lcd.setCursor(1,0);
  lcd.print("JOS");
  lcd.setCursor(5,0);
  lcd.print(daysTillChristmas);
  lcd.setCursor(8,0);
  lcd.print("DANA");
  lcd.setCursor(13,0);
  lcd.print("DO");
  lcd.setCursor(0,1);
  lcd.print("*****BOZICA*****");
  
  if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  
  delay(1000);

  if((hour()>17 && hour()<22) || (wreathOnOff == 1) || (irrecv.decode(&results))){  //turn the wreath on each day beetween 5 and 10 pm OR turn it on with the Blynk App OR with the Remote Control
    if(results.value == 0XFFFFFFFF)
      results.value = key_value;
    if(day() > 3 && day() < 10){                               //turning on LEDs for each day in first week
      for(x = 0; x<=day()-4; x++){                         //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
        if((ledPattern1 == 1) || (results.value == 0xFF6897)){
          pattern1(int x);
        }
        else if((ledPattern2 == 1) || (results.value == 0xFF30CF)){
          pattern2(int x);
          pattern3(int x);
        }
        else if((ledPattern3 == 1) || (results.value == 0xFF7A85)) {
          pattern3(int x);
        }
      }
    }
    else if(day() > 10 && day() < 17){
      for(x = 0; x<=day()-5; x++){ //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
        if((ledPattern1 == 1) || (results.value == 0xFF6897)){
          pattern1(int x);
        }
        else if((ledPattern2 == 1) || (results.value == 0xFF30CF)){
          pattern2(int x);
          pattern3(int x);
        }
        else if((ledPattern3 == 1) || (results.value == 0xFF7A85)) {
          pattern3(int x);
        }
      }
    }   
    else if(day() >17 && day() < 24){
      for(x = 0; x<=day() - 6;x++){
        digitalWrite(ledPins[x],HIGH);
        if((ledPattern1 == 1) || (results.value == 0xFF6897)){
          pattern1(int x);
        }
        else if((ledPattern2 == 1) || (results.value == 0xFF30CF)){
          pattern2(int x);
          pattern3(int x);
        }
        else if((ledPattern3 == 1) || (results.value == 0xFF7A85)) {
          pattern3(int x);
        }
      }
  
    }
  }
  else{
    for(x = 0; x<19; x++){
      digitalWrite(ledPins[x],LOW);
    }
  }
  
  lastPin2State = pin2State;
}

//PATTERNS
void pattern1(x){
  digitalWrite(ledPins[x],LOW);
  delay(300);
  digitalWrite(ledPins[x],HIGH);
  delay(300);
}

void pattern2(x){
  digitalWrite(ledPins[x],LOW);
  delay(300);
}
void pattern3(x){
  digitalWrite(ledPins[x],HIGH);
  delay(300);
}

void pattern4(x){
  int random_led = random(ledPins[x]);
  digitalWrite(random_led,LOW);
  delay(500);
  digitalWrite(random_led,HIGH);
  delay(500);
}


//Fuktion for RTC
void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
//Funktion for RTC
void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

So here is my code. Some stuff work, some not.

Your code does not compile, and once it is corrected to compile successfully, there appear to be many other significant errors.

You are not obviously not writing code in a disciplined fashion. You need to start from something that works properly, and then make small changes, testing as you go. Do you have working code which uses an on/off button and the rtc? No nano, no blynk, no patterns.
If so, post it and lets go from there.

Regarding the ir remote. Forget about that entirely until you have the blynk and rtc controlled lights working.

Here is my 1. Problem. I connected Inputs D2-D4 to pull-down resistors but when Arduino Nano send "1" or 5V, the output on Mega is 1 but if the digital input 0 ist, then Digital Input is not working properly. Although I connected input to pull down Resistor, Input is still switching states.

I suggest, that to start you separate the nano and blynk from the operation on the mega.

Control the lighting output of the mega with 4 buttons and the rtc. Start with the on/off button and then add patterns one at a time.

Can you attach a hand drawn sketch of how you have wired the buttons and the pulldowns?

Once you get that working properly you can add the nano with blynk to replace the buttons.
The nano with serial input can be used in place of the buttons before you ever get to blynk.

Adding in the ir remote on the mega can be final step.

EDIT: There is a fundamental problem in how you are handling the arrays which I did not notice before. Arrays are zero indexed. That is the first element is element[0].

You have 18 elements in the array. They are indexed 0 through 17. You need to modify the for loops to stay within bounds of your array.

const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};

void setup()  {
 //for(int x = 0; x < 19; x++){ //set all LEDs als OUTPUT
    for(int x = 0; x < 18; x++){ //set all LEDs als OUTPUT //indexes are 0 thru 17
   pinMode(ledPins[x],OUTPUT);

About arrays you got that right. It forgot to change that. The RTC and LEDs are working correctly. On Tuesday i can post that. Tommorow i will have the drawings in the Frizing dann so I can post that as well. I will try like you sad. Install 4 buttons that will "simulate" NANO and Blynk.

#define ON_OFF 2
int ledOnOff = 0;
pinMode(ON_OFF,INPUT); 

pin2State = digitalRead(ledOnOff);

You are not reading the ON_OFF button input, instead you are reading pin 0. You need to use this to read the the input on pin 2.

pin2State = digitalRead(ON_OFF);

Here is my code with one button and RTC&LCD. Everything is working so far. In attachment you can see the drawing of a button. I will start to add paterns now and try to switch them with more pushbuttons...

// ++++Smart Advent Wreath++++
/*After the first Sunday the program will start to work. It will do 2 things: 1. it will count how many days it is left till Christmass and show it on LCD. 
  2. for each day it will light up a led starting with 2.Dec (Monday). I dont have a LED for Sundays because on Sundays we light up a real candle. 
  For time i connected RTC modul. The RTC is also zaduzen for turning all LED after 18:00 and turning off after 22:00. I dont wont to be on all the time.
  Because I have 18 LEDs (from 2.12 till 21.12, without sundays) I used MEGA (didnt wont to have LED in matrix)
  With the help of Arduino NANO I can turn off LEDs
  The are 4 patterns how LEDs can blink
  Patterns can change with the Blynk app or with the remote control 
 */
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>                // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DS3231_I2C_ADDRESS 104



const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};  //defining on which pins are LEDs
const int ledOnOff = 2;
int pin2State = 0;
int wreathOnOff = 0;
long lastDebounceTime = 0;
long debounceDelay = 20;

int daysTillChristmas = 0;


void setup()  {
  for(int x = 0; x < 18; x++){          //set all LEDs als OUTPUT
    pinMode(ledPins[x],OUTPUT);
  }
  
  pinMode(ledOnOff,INPUT);

  
  //RTC && LCD
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);                    // wait until Arduino Serial Monitor opens
    setSyncProvider(RTC.get);         // the function to get the time from the RTC
    if(timeStatus()!= timeSet)
      Serial.println("Unable to sync with the RTC");
    else
      Serial.println("RTC has set the system time");   
  

  
  //LCD
  lcd.init();
  lcd.backlight();
}

void loop()
{
  daysTillChristmas = 25 - day(); //for LCD count down
  
  pin2State = digitalRead(ledOnOff);
  
  if((millis() - lastDebounceTime) > (debounceDelay)){
    if ((pin2State == HIGH) && (wreathOnOff == 0)){
      wreathOnOff = 1;
      lastDebounceTime = millis();
    }
    else if ((pin2State == HIGH) && (wreathOnOff == 1)){
      wreathOnOff = 0;
      lastDebounceTime = millis();
  }
}

  Serial.println(hour());
  Serial.println(wreathOnOff);

  //Seting up a LCD
  lcd.setCursor(1,0);
  lcd.print("JOS");               //in Croatian "STILL"
  lcd.setCursor(5,0);
  lcd.print(daysTillChristmas);   //number of days
  lcd.setCursor(8,0);
  lcd.print("DANA");              //in Croatian "DAYS"
  lcd.setCursor(13,0);
  lcd.print("DO");                // in Croatian "TILL"
  lcd.setCursor(0,1);
  lcd.print("*****BOZICA*****");  // in Croatian "CHRISTMAS"
  
  if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  
  delay(1000);

  if((hour()>20 && hour()<22) || (wreathOnOff == 1) ){  //turn the wreath on each day beetween 5 and 10 pm

    if(day() > 3 && day() < 10){                               //turning on LEDs for each day in first week
      for(int x = 0; x<=day()-4; x++){                         //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
      }
    }
    else if(day() > 10 && day() < 17){
      for(int x = 0; x<=day()-5; x++){ //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
      }
    }   
    else if(day() >17 && day() < 26){
      for(int x = 0; x<=day() - 6;x++){
        digitalWrite(ledPins[x],HIGH);
      }
    }
  }
  else{
    for(int x = 0; x<18; x++){
      digitalWrite(ledPins[x],LOW);
    }
  }
  
}

//Fuktion for RTC
void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
//Funktion for RTC
void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

So this was a productive night. I manage to add patters (2 patterns and 2 buttons) and also to add remote control. Evrething is working. Only thing that is left is to change buttons with arduino NANO. I will take out random pattern because i dont like the way it is working or i dont understand it. I took another screenshot. It is from Serial Monitor. I saw it that every 5 sec Arduino start a loop (so long does it take for my LEDs to turn off and on - pattern 1). In the meantime the Arduino is not "available". I mean I need to wait before pattern is that and then I can switch to second one. Is there a way to add a interupt or some thing like that?

Hier is the code:

// ++++Smart Advent Wreath++++
/*After the first Sunday the program will start to work. It will do 2 things: 1. it will count how many days it is left till Christmass and show it on LCD. 
  2. for each day it will light up a led starting with 2.Dec (Monday). I dont have a LED for Sundays because on Sundays we light up a real candle. 
  For time i connected RTC modul. The RTC is also zaduzen for turning all LED after 18:00 and turning off after 22:00. I dont wont to be on all the time.
  Because I have 18 LEDs (from 2.12 till 21.12, without sundays) I used MEGA (didnt wont to have LED in matrix)
  With the help of Arduino NANO I can turn off LEDs
  The are 4 patterns how LEDs can blink
  Patterns can change with the Blynk app or with the remote control 
 */
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>                // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DS3231_I2C_ADDRESS 104



const int ledPins[] = {22, 23, 26, 27, 30, 31, 34, 35, 38, 39, 42, 43, 46, 47, 50, 51, 52, 53};  //defining on which pins are LEDs
const int ledOnOff = 2;
const int pattern1 = 3;
const int pattern2 = 4;

const int recvPin = 7;
IRrecv irrecv(recvPin);
decode_results results;
unsigned long key_value = 0;

int turnOnOff = 0;

int pin2State = 0;
int pin3State = 0;
int pin4State = 0;

int wreathOnOff = 0;
int led_pattern1 = 0;
int led_pattern2 = 0;

long lastDebounceTime = 0;
long debounceDelay = 20;

int daysTillChristmas = 0;


void setup()  {
  for(int x = 0; x < 18; x++){          //set all LEDs als OUTPUT
    pinMode(ledPins[x],OUTPUT);
  }
  
  pinMode(ledOnOff,INPUT);
  pinMode(pattern1,INPUT);
  pinMode(pattern2,INPUT);

  //REMOTE CONTROL 
  irrecv.enableIRIn();
  irrecv.blink13(true);
    
  //RTC && LCD
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);                    // wait until Arduino Serial Monitor opens
    setSyncProvider(RTC.get);         // the function to get the time from the RTC
    if(timeStatus()!= timeSet)
      Serial.println("Unable to sync with the RTC");
    else
      Serial.println("RTC has set the system time");   
  

  
  //LCD
  lcd.init();
  lcd.backlight();
}

void loop()
{
  daysTillChristmas = 25 - day(); //for LCD count down
  
  pin2State = digitalRead(ledOnOff);
  pin3State = digitalRead(pattern1);
  pin4State = digitalRead(pattern2);

  //DEFINING WHAT REMOTE CONTROL BUTTONS DO AND HOW DO THEY CHANGE THE LED PATTERNS
  if (irrecv.decode(&results)){
    switch(results.value){
          case 0xFF6897:            //Master Button. It can turn off all LEDs
            Serial.println("master");
            if(turnOnOff == 0){
              turnOnOff = 1;
            } else if(turnOnOff == 1){
              turnOnOff = 0;
            }
          break;
          
          case 0xFF30CF:
            Serial.println("pattern 1");
            led_pattern1 = 1;
            led_pattern2 = 0;
          break;
          
          case 0xFF18E7:
            Serial.println("pattern 2");
            led_pattern1 = 0;
            led_pattern2 = 1;
          break;
    }
   
    irrecv.resume();
  }

  //BUTTONS TO CHANGE PATTERNS
  if((millis() - lastDebounceTime) > (debounceDelay)){
    if ((pin2State == HIGH) && (wreathOnOff == 0)){
      wreathOnOff = 1;
      led_pattern1 = 0;
      led_pattern2 = 0;
      lastDebounceTime = millis();
    }
    else if ((pin2State == HIGH) && (wreathOnOff == 1)){
      wreathOnOff = 0;
      led_pattern1 = 0;
      led_pattern2 = 0;
      lastDebounceTime = millis();
  }
  else if (pin3State == HIGH){
      led_pattern1 = 1;
      led_pattern2 = 0;
      lastDebounceTime = millis();
  }
  else if (pin4State == HIGH){
    led_pattern2 = 1;
    led_pattern1 = 0;
    lastDebounceTime = millis();
  }
}

  
  Serial.println(wreathOnOff);
  Serial.println(led_pattern1);
  Serial.println(led_pattern2);

  //Seting up a LCD
  lcd.setCursor(1,0);
  lcd.print("JOS");               //in Croatian "STILL"
  lcd.setCursor(5,0);
  lcd.print(daysTillChristmas);   //number of days
  lcd.setCursor(8,0);
  lcd.print("DANA");              //in Croatian "DAYS"
  lcd.setCursor(13,0);
  lcd.print("DO");                // in Croatian "TILL"
  lcd.setCursor(0,1);
  lcd.print("*****BOZICA*****");  // in Croatian "CHRISTMAS"
  
  if (timeStatus() == timeSet) {
    digitalClockDisplay();
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
    Serial.println();
    delay(4000);
  }
  
  delay(1000);
  
  if (turnOnOff == 0){
  if((hour()>20 && hour()<22) || (wreathOnOff == 1) ){  //turn the wreath on each day beetween 5 and 10 pm
  
    if(day() > 3 && day() < 10){                               //turning on LEDs for each day in first week
      for(int x = 0; x<=day()-4; x++){                         //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
      }
    }
    else if(day() > 10 && day() < 17){
      for(int x = 0; x<=day()-5; x++){ //because program starts on Monday 4.11. We need to ignore first 3 days. But if i put day()-3 then on the 4.11. we have day() = 4; day()-3 = 1. So it will ignore first pin in array( x[0]) and it will turn one LED more.  
        digitalWrite(ledPins[x],HIGH);
      }
    }   
    else if(day() >17 && day() < 26){
      for(int x = 0; x<=day() - 6;x++){
        digitalWrite(ledPins[x],HIGH);
        if (led_pattern1 == 1){
          ledPattern1(x);
          }
        else if (led_pattern2 == 1){
          ledPattern2(x);
        }
      }
    }
  }
  }
  else{
    for(int x = 0; x<18; x++){
      digitalWrite(ledPins[x],LOW);
    }
  }
  
}

//PATTERNS

void ledPattern1(int x){
  digitalWrite(ledPins[x],LOW);
  delay(100);
  digitalWrite(ledPins[x],HIGH);
  delay(100);
}

void ledPattern2(int x){
  int random_led = random(ledPins[x]);
  digitalWrite(random_led,LOW);
  delay(100);
  digitalWrite(random_led,HIGH);
  delay(100);
}

//Fuktion for RTC
void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
//Funktion for RTC
void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Good progress! I see that your worked out how to make your function definitions and calls in this iteration.

You are still over running an array bound in this code as 25 -6 = 19, and there is no ledPins[19].

else if(day() >17 && day() < 26){
      for(int x = 0; x<=day() - 6;x++){
        digitalWrite(ledPins[x],HIGH);
        if (led_pattern1 == 1){
          ledPattern1(x);
          }
        else if (led_pattern2 == 1){
          ledPattern2(x);
        }
      }
    }

I do not understand why the ledPatterns are only part of the day() >17 && day() < 26 block. Is this what you want?

ledPattern2 does not look correct. Did you test it?

void ledPattern2(int x) {
  int random_led = random(ledPins[x]);
  digitalWrite(random_led, LOW);
  delay(100);
  digitalWrite(random_led, HIGH);
  delay(100);
}
random(ledPins[x])

is not likely to give you an actual ledPin with an led connected. For example ledPin[2] == 26 and a random number from 0 to 25 is not likely to fall on any of your pins. random() - Arduino Reference

In general, you do not appear to have a strong sense of how to write Arduino code, and and you keep jumping ahead rather than getting pieces working one at a time. Write it one piece at a time, outputting debug information to serial so you can make sure that the code is doing what you think; then, when that piece works, then add another piece. The less experience you have with writing arduino code, the smaller the chunks of code you write between testing your program should be.

I saw it that every 5 sec Arduino start a loop (so long does it take for my LEDs to turn off and on - pattern 1). In the meantime the Arduino is not "available". I mean I need to wait before pattern is that and then I can switch to second one. Is there a way to add a interupt or some thing like that?

You need to remove all use of delay() in order to make the code responsive to inputs.

First, you have this

delay(1000);

It probably is there to control the Serial printing of the time to every second, and it also gives you time to get your fingers off of the buttons. You were advised earlier to used button state change as the way to read buttons and were using that approach a few posts ago, but you changed that, and have carried forward with reading button state. Why did you make that change?

Then as you recognize, the patterns use delay, and also block the code from responding to anything else.

There are many tutorials on how to write non blocking code, and they all start with the "blink without delay" example of the ide. Also look at Using millis() for timing A beginners guide and Demonstration code for several things at the same time

When you have had a chance to review, we can see about removing the blocking delays from your code.

Keklja:
Good progress! I see that your worked out how to make your function definitions and calls in this iteration.

You are still over running an array bound in this code as 25 -6 = 19, and there is no ledPins[19].

else if(day() >17 && day() < 26){

for(int x = 0; x<=day() - 6;x++){
        digitalWrite(ledPins[x],HIGH);
        if (led_pattern1 == 1){
          ledPattern1(x);
          }
        else if (led_pattern2 == 1){
          ledPattern2(x);
        }
      }
    }




I do not understand why the ledPatterns are only part of the day() >17 && day() < 26 block. Is this what you want?

The patters will be in all 3 if statments. I only used this one because of the date.

Keklja:
ledPattern2 does not look correct. Did you test it?

void ledPattern2(int x) {

int random_led = random(ledPins[x]);
  digitalWrite(random_led, LOW);
  delay(100);
  digitalWrite(random_led, HIGH);
  delay(100);
}






random(ledPins[x])



is not likely to give you an actual ledPin with an led connected. For example ledPin[2] == 26 and a random number from 0 to 25 is not likely to fall on any of your pins. https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

As you saw it, I used for loop to turn on LEDs. On 7.Dec only 6 LED will be on and I would like to have a pattern where this 6 will blinked randomly. I tought random(ledPins[x]) will turn on/off first 6 LEDs but it doesnt work like I imagened.

Keklja:
In general, you do not appear to have a strong sense of how to write Arduino code, and and you keep jumping ahead rather than getting pieces working one at a time. Write it one piece at a time, outputting debug information to serial so you can make sure that the code is doing what you think; then, when that piece works, then add another piece. The less experience you have with writing arduino code, the smaller the chunks of code you write between testing your program should be.

This is my first Arduino project so evrything that you sad is correct. Because it needs to be done till Thuersday, I dont really have the time to go like you sad but you are right. That should be the right way.

Keklja:
You need to remove all use of delay() in order to make the code responsive to inputs.

First, you have this

delay(1000);

It probably is there to control the Serial printing of the time to every second, and it also gives you time to get your fingers off of the buttons. You were advised earlier to used button state change as the way to read buttons and were using that approach a few posts ago, but you changed that, and have carried forward with reading button state. Why did you make that change?

I found in saved examples and used it to avoid debaunce. Because final version will not have buttons I didnt put a lot of atention to that.

This is my first Arduino project so evrything that you sad is correct. Because it needs to be done till Thuersday, I dont really have the time to go like you sad but you are right.

You will probably find that doing it the way Cattledog described is actually faster. Big bang testing is no fun.