Cycling LCD display without using delay() in code? - SOLVED

Hi,

I’ve tried to adapt the blinkwithout delay example for displaying two sets of information on a LCD screen.
The code is posted below, but does not work as expected.
It keeps cycling through the displays so quickly that only a faint haze of one of the items to be printed can be seen on the screen. if I uncomment the //delay (2000); the LCD display changes every 2 seconds, displaying both sets of info normally.

Is there any way to cycle the display without using delays?

#include <LiquidCrystal.h>


//create object to control an LCD.  
LiquidCrystal lcd(9, 8, 7, 10, 14, 2);  // this was 12,11,5,3,2 but 5 is taken, 
long previousLCDMillis = 0;    // for LCD screen update
long lcdInterval = 4000;


void setup()
{
  
  // start serial port
  Serial.begin(9600);
  Serial.println("Sensor is Starting Up");
  
  //Welcome Message
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);          // set the cursor to column 0, line 0
  lcd.print("Sensor");
  lcd.setCursor(0, 1);          // set the cursor to column 0, line 1
  lcd.print("initialising...");      
  
  // delay(2000);   // to show message on screen}


}


void loop()
{ 


  // (note: line 1 is the second row, since counting begins with 0):
  // Print a message to the LCD.
    
   unsigned long currentLCDMillis = millis();
   
   if(currentLCDMillis - previousLCDMillis > lcdInterval) {             // save the last time you changed the display 
      previousLCDMillis = currentLCDMillis; 
  
       
  lcd.clear();
  lcd.setCursor(0, 0);          // set the cursor to column 0, line 0
  lcd.print("Temperature");
  lcd.setCursor(0, 1);          // set the cursor to column 0, line 1
  lcd.print("temperature");      //send the data to the computer
  lcd.print((char)223);         // degree symbol
  lcd.print("C");

 }
 
  else {
 
  lcd.clear();
  lcd.setCursor(0, 0);          // set the cursor to column 0, line 0
  lcd.print("Humidity");
  lcd.setCursor(0, 1);          // set the cursor to column 0, line 1
  lcd.print("humidity");
  lcd.print("%");
   }
}
  1. use functions, they give overview in the main loop()
  2. after the interval you want to change the content of the display, define an id for the ‘screen’ to show ==> int screen ; + some extras

modded your code => something like this? (not tested if it compiles)

#include <LiquidCrystal.h>

//create object to control an LCD.  
LiquidCrystal lcd(9, 8, 7, 10, 14, 2);  // this was 12,11,5,3,2 but 5 is taken, 
long previousLCDMillis = 0;    // for LCD screen update
long lcdInterval = 4000;

// screen to show 
int screen = 0;    
int screenMax = 2;
bool screenChanged = true;   // initially we have a new screen,  by definition 
// defines of the screens to show
#define TEMPERATURE 0
#define HUMIDITY       1
#define TIME              2

//////////////////////////////////////
//
// display functions
//
void showWelcome()
{
  lcd.clear();
  lcd.setCursor(0, 0);           
  lcd.print("Sensor");
  lcd.setCursor(0, 1);          
  lcd.print("initialising...");      
}

void showTemperature(int T)
{
  lcd.clear();
  lcd.setCursor(0, 0);           
  lcd.print("Temperature");
  lcd.setCursor(0, 1);           
  lcd.print(T);       
  lcd.print((char)223);          
  lcd.print("C");
}

void showHumidity(int H)
{
  lcd.clear();
  lcd.setCursor(0, 0);           
  lcd.print("Humidity");
  lcd.setCursor(0, 1);          
  lcd.print(H);
  lcd.print("%");
}

void showTime()
{
  lcd.clear();
  lcd.setCursor(0, 0);           
  lcd.print("Time in seconds");
  lcd.setCursor(0, 1);          
  lcd.print(millis() / 1000);
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Sensor is Starting Up");
  
  lcd.begin(16, 2);
  showWelcome();
  
  delay(2000);   // to show message on screen}
}


void loop()
{ 
  unsigned long currentLCDMillis = millis();

  // MUST WE SWITCH SCREEN?
  if(currentLCDMillis - previousLCDMillis > lcdInterval)              // save the last time you changed the display 
  {
    previousLCDMillis = currentLCDMillis; 
    screen++;
    if (screen > screenMax) screen = 0;  // all screens done? => start over
    screenChanged = true; 
  }

  // debug Serial.println(screen);


  // DISPLAY CURRENT SCREEN
  if (screenChanged)   -- only update the screen if the screen is changed.
  {
    screenChanged = false; // reset for next iteration
    switch(screen)
    {
    case TEMPERATURE: 
      showTemperature(40); 
      break;
    case HUMIDITY: 
      showHumidity(50);
      break;
    case TIME:
      showTime();
      break;
    default:
      // cannot happen -> showError() ?
      break;
    }
  }

}

As you can see the int screen holds which screen has to be shown. In the code the variable screenChanged takes care that the LCD is not written to if not necessary. This free processor time to do something usefull

Hopes this helpes

Dear robtillaart,

Thank you very much for your kind reply. It works perfectly and does exactly what I was looking for!

Thank you!