Refresh LCD screen every second

Hello,

I got an arduino uno with 2 DTH22 temp sensors, 1 DS3231 rtc clock and an LCD screen.

I want to display the 2 temps and clock in a menu.
Only the clock does not refresh every second.

I tried delay, without succes.

I use this DS3231 lib:

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

#include "DHT.h"
DHT dht1, dht2;

#include <LiquidCrystal.h>
 
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
 
void setup() {
    Wire.begin();
        Clock.setSecond(50);//Set the second 
        Clock.setMinute(5);//Set the minute 
        Clock.setHour(23);  //Set the hour 
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(16);  //Set the date of the month
        Clock.setMonth(9);  //Set the month of the year
        Clock.setYear(16);  //Set the year (Last two digits of the year)
  dht1.setup(2); // data pin 2
  dht2.setup(3); // data pin 2
   //Set the characters and column numbers.
   lcd.begin(16, 2);
   //Print default title.
   clearPrintTitle();
      lcd.setCursor(0,0);
      lcd.print("Terrarium");
      lcd.setCursor(0,1); 
      lcd.print ("Instellingen");
}

void loop() {
  //Call the main menu.
  mainMenu();
}
 
void mainMenu() {
  //State = 0 every loop cycle.
  int state = 0;
  //Refresh the button pressed.
  int x = analogRead (0);
  //Set the Row 0, Col 0 position.
  lcd.setCursor(0,0);
 
  //Check analog values from LCD Keypad Shield
  if (x < 100) {
    //Right
  } else if (x < 200) {
   //Up
    state = 1;
  } else if (x < 400){
   //Down
    state = 2;
  } else if (x < 600){
    //Left
  } else if (x < 800){
    //Select
    state = 3;
  }
 
  //If we are out of bounds on th menu then reset it.
  if (currentMenuItem < 0 || currentMenuItem > 4) {
   currentMenuItem = 0; 
  }
 
   //If we have changed Index, saves re-draws.
   if (state != lastState) {
      if (state == 1) {
         //If Up
          currentMenuItem = currentMenuItem - 1; 
          displayMenu(currentMenuItem);
      } else if (state == 2) {
         //If Down
          currentMenuItem = currentMenuItem + 1;  
          displayMenu(currentMenuItem);
      } else if (state == 3) {
         //If Selected
         selectMenu(currentMenuItem); 
      }
      //Save the last State to compare.
      lastState = state;
   } 
   //Small delay
  delay(5);
}
 
//Display Menu Option based on Index.
void displayMenu(int x) {
  float humidity1 = dht1.getHumidity();
  float temperature1 = dht1.getTemperature();
  
  float humidity2 = dht1.getHumidity();
  float temperature2 = dht1.getTemperature();

    int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
     switch (x) {
      case 1:
        clearPrintTitle();
          lcd.setCursor(0,0);
          lcd.print("1. Temp ; Vocht");
          lcd.setCursor(0,1); 
          lcd.print (temperature1, 1);
          lcd.print(" ; ");
          lcd.print (humidity1, 1);
        break;
      case 2:
        clearPrintTitle();
        lcd.setCursor(0,0);
          lcd.print("2. Temp ; Vocht");
          lcd.setCursor(0,1); 
          lcd.print (temperature2, 1);
          lcd.print(" ; ");
          lcd.print (humidity2, 1);
        break;
       case 3:
         clearPrintTitle();
        lcd.setCursor(0,0);
          lcd.print("Tijd:");
          lcd.setCursor(0,1); 
          lcd.print(hour,DEC);
          lcd.print(":");
          lcd.print(minute,DEC);
          lcd.print(":");
          lcd.print(second,DEC);
        delay(500);
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("-> Menu Option 4");
        break;
    }
}

//Print a basic header on Row 1.
void clearPrintTitle() {
  lcd.clear();
}
 
//Show the selection on Screen.
void selectMenu(int x) {
   switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("Selected Opt 1");
        //Call the function that belongs to Option 1
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("Selected Opt 2");
        //Call the function that belongs to Option 2
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("Selected Opt 3");
        //Call the function that belongs to Option 3
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("Selected Opt 4");
        //Call the function that belongs to Option 4
        break;
    }
}

Does some got a solution for refreshing every sec?
Thanks in advance
M

Does some got a solution for refreshing every sec?

How often does it refresh?

How long does it take to read the temperature and humidity? Why do you the same sensor twice?

Why do you have the delay() in case 4? x is a lousy name for the state variable.

That delay was a try to get a refresh, without succes. Some else knows a solution?

What does your code actually do? How long does it take to refresh the screen display? It's hard to help you without sufficient details.

By pushing up or down you can go through a menu for 2 temp sensors and 1 rtc clock.
The rtc clock only goes when sec up when you go to another menu and back.

Temp 1 - 21 degrees
Temp 2 - 22 degrees
RTC - 15:15:30

The only way the 30 seconds go to 31 sec is to go back to Temp 2 and then go back to the RTC clock :frowning:

Your code only displays time when the menu is active. You need to completely re-write your code to display the menu OR the time and temp data. You need some method to change which is displayed.