Trouble looping real time clock

I am using the arduino uno with the display shield. I created a menu system that you can go through and pick different options like seeing the time or atmospheric pressure. I am having trouble on getting the real time clock to loop without being in void loop though. I want to be able to go into the clock section in the menu and have it count up each second. But when i put the code for the clock in the void loop it puts it over top of everything. So I need to like reference it in void loop without the code being actually in void loop i think. I have no idea how to do it and any help would be great.

#include <Wire.h>
#include <RTClib.h> //Clock
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
// 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();
   rtc.begin();
   
   //Set the characters and column numbers.
   lcd.begin(16, 2);
   //Print default title.
   clearPrintTitle();

   if (! rtc.isrunning())
  {
   rtc.adjust(DateTime((__DATE__),(__TIME__)));

  }
}
 
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) {
     switch (x) {
      case 1:
        clearPrintTitle();
        lcd.print ("-> Date and Time");
        break;
      case 2:
        clearPrintTitle();
        lcd.print ("-> Atmo.Pressure");
        break;
       case 3:
        clearPrintTitle();
        lcd.print ("-> Light Intens.");
        break;
      case 4:
        clearPrintTitle();
        lcd.print ("-> Menu Option 4");
        break;
    }
}
 
//Print a basic header on Row 1.
void clearPrintTitle() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" Hello! ");
  lcd.setCursor(0,1); 
}
 
//Show the selection on Screen.
void selectMenu(int x) {
   switch (x) {
      case 1:
        {DateTime now = rtc.now();
        
        lcd.setCursor(0,0);
        lcd.print(now.month(), DEC);
        lcd.print('/');
        lcd.print(now.day(), DEC);
        lcd.print('/');
        lcd.print(now.year(), DEC);
        
        lcd.setCursor(0,1);
        lcd.print((now.hour() < 10 ? "0" : "") + String(now.hour()) + ":" +
              (now.minute() < 10 ? "0" : "") + now.minute() + ":" +
              (now.second() < 10 ? "0" : "") + now.second());
        delay(1000);
        }
        //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;
   }
}

display.ino (3.44 KB)

 delay(1000);

What’s that doing there?

The real time clock code was from another just basic clock code that I have. I just basically copy and pasted it so the delay(1000); might not need to be there. But I have taken it out and nothing changed.

If you don't know what it does, how do you know that it's safe to remove?

The delay(1000); was from another real time clock code where it just printed the time on the screen every second so the delay (1000) delayed the printing of the time on the screen for 1 second. So I left it in my code for the display shield because I thought it would be essentially the same thing but only on a lcd display. I know the code for the clock works when I put it in void loop but when it is in the place in the menus and not in void loop I do not know how to "reference" it in void loop so it will loop in its spot in the menu.

Right now you are only updating after a keypress.

You could add a variable lastKeyPress and save millis whenever you record a keypress.
Later in your code you check for a state change. If no state change look for if lastKeyPress is > 5000 (5 seconds, but you chose the time you want) and call displayMenu. That will make display start to update 5 seconds after last keypress.

You could try to shove the date and time into one line of the display.
I wrote this code to do just that for you. But I haven't tested it, so it might have bugs.
NOTE: I found one possible bug, and I fixed it.

This part goes in your variable declarations, BEFORE setup().

  // set up a buffer for the message to be displayed
  char buf[21];

This part goes right after your lcd.begin statement:

  // define special characters for single cell numerals 10 through 12
  uint8_t singleCellTen[]    = { 18, 21, 21, 21, 21, 21, 18,  0 };
  uint8_t singleCellEleven[] = {  9, 27,  9,  9,  9,  9,  9,  0 };
  uint8_t singleCellTwelve[] = { 22, 21, 17, 18, 20, 20, 23,  0 };
  lcd.createChar(10, singleCellTen); 
  lcd.createChar(11, singleCellEleven); 
  lcd.createChar(12, singleCellTwelve);

This part does the actual work of displaying the date and time:

  // find out what time it is now
  DateTime now = rtc.now();
  // put date and time in the message buffer
  sprintf(buf, "%c/%02d/%02d %02d:%02d:%02d",
    ((char)((((now.month())+38)%48)+10)), (now.day()), ((now.year())%100),
    (now.hour()), (now.minute()), (now.second()));
  // move cursor to the bottom line
  lcd.setCursor(0,1);  
  // display the date and time    
  lcd.print(buf);

@bcox13, do not cross-post. Other thread removed.