Problems with programming RTC

Hello, I have problems with the programming of the DS1307, it is for the watch to run and update the time on the LCD screen must be running several times, otherwise only the time is displayed and is statically on the LCD, this means that the time and date are not updated.

#include <RTClib.h>
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal.h>

 
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); //pines del arduino al LCD (RS E D4 D5 D6 D7) 


//Definición de filas y columnas
const byte Filas = 4; //Cuatro filas
const byte Cols = 4; //Cuatro columnas

//Definición de los pines
byte Pins_Filas[] = {3, 2, 1, 0}; //Pines Arduino para las filas
byte Pins_Cols[] =  {4, 5, 6, 7}; // Pines Arduino para las columnas


//Definición de las teclas
char Teclas [ Filas ][ Cols ] =
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

//Se crea una instancia llamada Teclado1 y el cual asignara las teclas que tenemos en el arreglo "Teclas" y le decimos
//que se han conectados los pines de las filas y columnas

Keypad kpd = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);

byte second = 0;

RTC_DS1307 RTC;
 
void setup()
{
Wire.begin(); //configura el bus I2C estableciendo arduino como MASTER
RTC.begin();


RTC.adjust(DateTime(__DATE__, __TIME__)); //esta funcion establecera en el modulo la fecha de creación del archivo .hex generado al compilar el sketch.
 

lcd.begin(16, 4); //Configura el LCD con el numero de columas y filas. Para un LCD 16x4: 16 columnas y 4 filas.
lcd.display(); //Enciende el display
lcd.clear();

  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW);

}
 
void loop()
{


while(1){
mostrarRTCLCD();
}
 

lcd.setCursor(0,2); lcd.print ("1. LedOn");
lcd.setCursor(0,3); lcd.print ("2. LedOff");
  
int kp = Teclado ();
switch(kp){
    case '1':
    lcd.clear();
    lcd.setCursor(0,2); lcd.print ("LedOn");
    digitalWrite(A0, HIGH);
    delay(50);
    break;

    case '2':
    lcd.clear();
    lcd.setCursor(0,2); lcd.print ("ledoff");
    digitalWrite(A0, LOW);
    delay(50);
    break;
}


}


 
void mostrarRTCLCD() //Funcion que lee los datos de modulo RTC y despues los imprime en el display
{

   
DateTime now = RTC.now(); //obtiene datos del modulo RTC

lcd.setCursor(0,0);
if(now.day() < 10){lcd.print("0");} lcd.print(now.day(), DEC); //imprime dia
lcd.print('/');
if(now.month() < 10){lcd.print("0");} lcd.print(now.month(), DEC); //imprime mes
lcd.print('/');
lcd.print(now.year(), DEC);  //imprime el año


lcd.setCursor(0,1);
if(now.hour() < 10){lcd.print("0");} lcd.print(now.hour(), DEC); //imprime hora
lcd.print(':');
if(now.minute() < 10){lcd.print("0");} lcd.print(now.minute(), DEC); //imprime minutos
lcd.print(':');
if(now.second() < 10){lcd.print("0");}
lcd.print(now.second(), DEC); //imprime segundos


}



char Teclado ()
{
  do{
    char Tecla = kpd.getKey();
   
    if (Tecla != NO_KEY)
    {
      return Tecla;
    }   
  } while (1);
}

The program works perfectly if you only left void and function, example

void loop(){
mostrarRTCLCD();
}

but adding other functions to the program fails to update the time, example.

void loop(){

mostrarRTCLCD();

//Function turn on / turn off led

//

}

When the program runs the mostrarRTCLCD() function; shows the time and date on the LCD but arriving at a function (this could be any) led turn on or it fails to update, and only restatement at the time that I finish running the program.

As solution use “while” infinite

void loop{

while(1){
mostrarRTCLCD();
}


}

The problem is that if there is a function after the infinite while, won’t ever run

void loop{

while(1){
mostrarRTCLCD();
}

//Function turn on / turn off led

}

And if I put the function (any) turn on led inside the infinite “while”, we were in the same problem as when not using infinite while.

void loop{

while(1){
mostrarRTCLCD();

//Function turn on / turn off led

}


}

The display will run until it finishes executing the function turn on led

Someone who has already worked with RTC that voluntarily help me please

Many thanks, in advance

Whatever the problem is you do not need the while (1). The loop() function does what it says and loops back to the beginning. Use that to you advantage by checking whether it is time to update the LCD each time loop() starts. In order to do this any code in loop() or any function called from it must not block free execution of the code.

Please post an example of your attempt to update the LCD and flash an LED as I can't see it in your original post. I suspect that the code that flashes the LED is the problem.

UKHeliBob:
Whatever the problem is you do not need the while (1). The loop() function does what it says and loops back to the beginning. Use that to you advantage by checking whether it is time to update the LCD each time loop() starts. In order to do this any code in loop() or any function called from it must not block free execution of the code.

Please post an example of your attempt to update the LCD and flash an LED as I can’t see it in your original post. I suspect that the code that flashes the LED is the problem.

#include <RTClib.h>
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal.h>

 
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); //pines del arduino al LCD (RS E D4 D5 D6 D7) 


//Definición de filas y columnas
const byte Filas = 4; //Cuatro filas
const byte Cols = 4; //Cuatro columnas

//Definición de los pines
byte Pins_Filas[] = {3, 2, 1, 0}; //Pines Arduino para las filas
byte Pins_Cols[] =  {4, 5, 6, 7}; // Pines Arduino para las columnas


//Definición de las teclas
char Teclas [ Filas ][ Cols ] =
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

//Se crea una instancia llamada Teclado1 y el cual asignara las teclas que tenemos en el arreglo "Teclas" y le decimos
//que se han conectados los pines de las filas y columnas

Keypad kpd = Keypad(makeKeymap(Teclas), Pins_Filas, Pins_Cols, Filas, Cols);

char kp;

RTC_DS1307 RTC;
 
void setup()
{
  Wire.begin(); //configura el bus I2C estableciendo arduino como MASTER
  RTC.begin();

  RTC.adjust(DateTime(__DATE__, __TIME__)); //esta funcion establecera en el modulo la fecha de creación del archivo .hex generado al compilar el sketch.
 

  lcd.begin(16, 4); //Configura el LCD con el numero de columas y filas. Para un LCD 16x4: 16 columnas y 4 filas.
  lcd.display(); //Enciende el display
  lcd.clear();

  pinMode(A0, OUTPUT);
  digitalWrite(A0, LOW);

}
 
void loop()
{

mostrarRTCLCD(); 

lcd.setCursor(0,3); lcd.print ("Press *");
kp = Teclado ();

if(kp=='*'){
  lcd.clear();
  lcd.setCursor(0,1); lcd.print ("Menu 1");
  lcd.setCursor(0,2); lcd.print ("Menu 2");
  kp = Teclado ();
  switch(kp){
    case '1':
            lcd.clear();
            lcd.setCursor(0,2); lcd.print ("1. LedOn");
            lcd.setCursor(0,3); lcd.print ("2. LedOff");
        
             kp = Teclado ();
                switch(kp){
                    case '1':
                          lcd.clear();
                          lcd.setCursor(0,2); lcd.print ("LedOn");
                          digitalWrite(A0, HIGH);
                          delay(50);
                          lcd.clear();
                    break;
                
                    case '2':
                          lcd.clear();
                          lcd.setCursor(0,2); lcd.print ("ledoff");
                          digitalWrite(A0, LOW);
                          delay(50);
                          lcd.clear();
                    break;
                }
    break;

    case '2':
          lcd.clear();
          lcd.setCursor(0,2); lcd.print ("Hola mundo");
          delay(100);
          lcd.clear();
    break;
  }

}

}


 
void mostrarRTCLCD() //Funcion que lee los datos de modulo RTC y despues los imprime en el display
{

   
DateTime now = RTC.now(); //obtiene datos del modulo RTC

lcd.setCursor(0,0);
if(now.day() < 10){lcd.print("0");} lcd.print(now.day(), DEC); //imprime dia
lcd.print('/');
if(now.month() < 10){lcd.print("0");} lcd.print(now.month(), DEC); //imprime mes
lcd.print('/');
lcd.print(now.year(), DEC);  //imprime el año


lcd.setCursor(0,1);
if(now.hour() < 10){lcd.print("0");} lcd.print(now.hour(), DEC); //imprime hora
lcd.print(':');
if(now.minute() < 10){lcd.print("0");} lcd.print(now.minute(), DEC); //imprime minutos
lcd.print(':');
if(now.second() < 10){lcd.print("0");}
lcd.print(now.second(), DEC); //imprime segundos


}



char Teclado ()
{
  do{
    char Tecla = kpd.getKey();
   
    if (Tecla != NO_KEY)
    {
      return Tecla;
    }   
  } while (1);
}

The time will be updated every time the program is run, please help

librery: RTClib/examples at master · adafruit/RTClib · GitHub

i2c.ino (3.41 KB)

I don't think you need this line.

  RTC.adjust(DateTime(__DATE__, __TIME__)); //esta funcion establecera en el modulo la fecha de creación del archivo .hex generado al compilar el sketch.

What are you trying to make? What is the keypad supposed to do?

You call the Teclado() function from loop(), but until a key is pressed the function will not return, so the program will spend most of its time waiting for a keypress. By all means check for a keypress in a function but return from the function with the value of the keypress, even if one has not been pressed, and deal with it in loop()

UKHeliBob:
You call the Teclado() function from loop(), but until a key is pressed the function will not return, so the program will spend most of its time waiting for a keypress. By all means check for a keypress in a function but return from the function with the value of the keypress, even if one has not been pressed, and deal with it in loop()

I can do for the program to run normally and time update automatically?

UKHeliBob:
By all means check for a keypress in a function but return from the function with the value of the keypress, even if one has not been pressed, and deal with it in loop()

Clearly English is not the OP’s first language. It might not be a good idea to use idioms such as “by all means”, or run-on sentences with too little punctuation.

odometer:
Clearly English is not the OP's first language. It might not be a good idea to use idioms such as "by all means", or run-on sentences with too little punctuation.

I forgot to say that I use google translator, sorry

odometer:
I don't think you need this line.

  RTC.adjust(DateTime(__DATE__, __TIME__)); //esta funcion establecera en el modulo la fecha de creación del archivo .hex generado al compilar el sketch.

What are you trying to make? What is the keypad supposed to do?

I want the time automatically updated without affecting my program, like cell phones, which runs the start time (without affecting the program) but when the user enters the menu can change the time

UKHeliBob:
You call the Teclado() function from loop(), but until a key is pressed the function will not return, so the program will spend most of its time waiting for a keypress. By all means check for a keypress in a function but return from the function with the value of the keypress, even if one has not been pressed, and deal with it in loop()

I solved it by putting a "if (key)"

Although you have not posted your solution it sounds like you are using the right technique.