Need help with coding problem LCD I2c + RTC (Changing screen)

Hello, I am working on a big project so I'm taking it a step by step, for now I'm working on having a multiple LCD pages, first page displays continously updated input data, RTC data ( data, time, etc..), second page displays a simple standard text, I am trying to combine my LCD RTC display code with another code i found online which changes the LCD screen ( autoscroll) every some seconds.
I am using LCD I2C 16x2 and DS3231. I'm posting the combined code, I'd appreciate it if you could highlight where my mistakes are at and maybe how to solve it.
One more thing, is that when void updateLCD() is used it is continously updating the LCD based on the RTC data, doesnt matter if on page 1 or page 2, It is like I am displaying 2 different thing at the same time on the LCD.
I'm going to put the 2 codes combined ( I know case 1 in incorrect but i tried to write there void updateLCD() and i was getting an error message ( expected initializer before '}' token ). Please help me out.

Code:

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68



//Counter to change positions of pages

int page_counter=1 ;       //To move beetwen pages

//Variables for auto scroll
unsigned long previousMillis = 0;
unsigned long interval = 5000; //Desired wait time 5 seconds
//-------Pins-----//
int up = 8;               //Up button
int down = 10;           //Down button              
//---------Storage debounce function-----//
boolean current_up = LOW;          
boolean last_up=LOW;            
boolean last_down = LOW;
boolean current_down = LOW;
        


/*
   function to update RTC time using user input
*/
void updateRTC()
{
  
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}


/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position

  
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}
//STANDARD FUNCTIONS: SETUP() AND LOOP()
//The last phase in completing our code for an Arduino Calendar Clock is to add the standard Arduino functions setup() and loop().

//Inside setup(), we will initialize the serial interface, the lcd and the rtc objects. To initialize the serial with a baud rate of 9600 bps, we will use the code Serial.begin(9600);. For the LCD, we need to initialize the LCD object and switch-on the backlight of the display. This is achieved by the codes lcd.init(); and lcd.backlight();. And finally, we add the code rtc.begin(); to initialize the rtc object.

void setup()
{
  Serial.begin(9600); // initialize serial

  lcd.init();       // initialize lcd
  lcd.backlight();  // switch-on lcd backlight

  rtc.begin();       // initialize rtc

  
}
   //---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}


//For the loop() function, we will update the text displayed on the LCD by calling updateLCD();. We will also add the capability to accept user input to update the RTC’s internal clock. If the user sends the char ‘u’ via the serial monitor, it means the user wants to modify the set time and date of the rtc. If this is the case, then we call the function updateRTC(); to handle user input and update the RTC internal clock.

void loop()
{
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }
current_up = debounce(last_up, up);         //Debounce for Up button
current_down = debounce(last_down, down);   //Debounce for Down button

//----Page counter function to move pages----//

//Page Up
    if (last_up== LOW && current_up == HIGH){ 
      lcd.clear();                     //When page is changed, lcd clear to print new page   
      if(page_counter <2){              //Page counter never higher than 2(total of pages)
      page_counter= page_counter +1;   //Page up
      
      }
      else{
      page_counter= 1;                //return to page 1
      }
  }
  
    last_up = current_up;

//Page Down
    if (last_down== LOW && current_down == HIGH){
      lcd.clear();                     //When page is changed, lcd clear to print new page    
      if(page_counter >1){              //Page counter never lower than 1 (total of pages)
      page_counter= page_counter -1;   //Page down
      
      }
      else{
      page_counter= 2;              //return to page 2
      }
  }
    
    last_down = current_down;
//------- Switch function to write and show what you want---// 
  switch (page_counter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(5,0);
      lcd.print("This is");
      lcd.setCursor(3,1);
      lcd.print(" Home Page");
    }
    break;

    case 2: { //Design of page 2 
     lcd.setCursor(5,0);
     lcd.print("This is");
     lcd.setCursor(4,1);
     lcd.print("Page 2");
    
       
    }
    break;
 }//switch end
  
//-----------Auto scroll function---------------//

     unsigned long currentMillis = millis();            //call current millis
     lcd.setCursor(0,1);                               // Show millis counter status
     lcd.print((currentMillis-previousMillis)/1000);
     
     if (currentMillis - previousMillis > interval) {  //If interval is reached, scroll page
     previousMillis = currentMillis;                   //replace previous millis with current millis as new start point
     lcd.clear();                                      //lcd clear if page is changed.
     if (page_counter <2){                             //Page counter never higher than 2 (total of pages)
     page_counter = page_counter +1;                   //Go to next page
     }
     else{
      page_counter=1;                                  //if counter higher than 2 (last page) return to page 1
     }
     } 
      
if (digitalRead(8) == HIGH || digitalRead(10)==HIGH){ // Reset millis counter If any button is pressed
  previousMillis = currentMillis;
}

     
}

Don't use the String class on AVR Arduinos! It fragments the memory too fast and sooner or later you get improper behavior.

1 Like
int hh = rtcTime.twelveHour();
if (rtcTime.isPM())

Your posted code does not compile. These do not appear to be RTClib.h functions.

One more thing, is that when void updateLCD() is used it is continously updating the LCD based on the RTC data, doesnt matter if on page 1 or page 2, It is like I am displaying 2 different thing at the same time on the LCD.
I'm going to put the 2 codes combined ( I know case 1 in incorrect but i tried to write there void updateLCD() and i was getting an error message ( expected initializer before '}' token ). Please help me out.

Yes, updateLCD() needs to be called from one of the cases. For example

switch (page_counter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(5,0);
      lcd.print("This is");
      lcd.setCursor(3,1);
      lcd.print(" Home Page");
      updateLCD();
    }
    break;

But called from one of the cases how?

Thanks for the tip, but it still doesn't solve my update lcd problem.

But called from one of the cases how?

Like I showed you in my previous post. What did you not understand?

Basic tutorial on writing and calling a function
https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/#:~:text=Calling%20a%20Function&text=To%20call%20a%20function%2C%20use,then%20open%20the%20terminal%20window.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.