Adding millis to LCD instead of delay, help?

Hello guys,
I'm trying to use millis instead of delay to shift the pages of a 2x16 LCD with a pause of 7 seconds. I want to make it show the first 2 rows for 7 seconds, and then the second 2 rows for 7 seconds. But looks like my code is no bueno, it just stays on the second page.

Does anyone know how can I do it?

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define DS18B20 26
#define LCD_milis_1 7000
#define LCD_milis_2 7000

unsigned long lastExecutedMillis_1 = 0;
unsigned long lastExecutedMillis_2 = 0;

OneWire oneWire(DS18B20);
DallasTemperature Sensor(&oneWire);
float temperature;

hd44780_I2Cexp lcd;

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);
}

void loop() {
  unsigned long currentMillis = millis();
  Sensor.requestTemperatures();
  temperature = Sensor.getTempCByIndex(0);
  Serial.println(temperature);

  if (currentMillis - lastExecutedMillis_1 >= LCD_milis_1) {
    lastExecutedMillis_1 = currentMillis;
    lcd.backlight();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("FirstLine  ");
    lcd.print("14");
    lcd.setCursor(0, 1);
    lcd.print("SecondLine ");
    lcd.print("85");
    lcd.print("%  ");
  }

    if (currentMillis - lastExecutedMillis_2 >= LCD_milis_2) {
      lastExecutedMillis_2 = currentMillis;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Page2Up: ");
      lcd.print("21");
      lcd.print((char)223);
      lcd.setCursor(0, 1);
      lcd.print("Page2Down: ");
      lcd.print(temperature, 1);
      lcd.print((char)223);
    }
  }

There is one task to do, so I would use just one millis-timer with one previousMillis variable.

You could count each time the millis-timer gets activated, and do different things for each number of the counter, perhaps also turning off the millis-timer.

You could combine a Finite State Machine with millis. To me that seems over-the-top for your project.

Is it 7 seconds one page and 7 seconds the other page continuous in a loop forever ?
Then you only need a millis-timer of 7 seconds and a counter that is 0 and 1.
Let's not call them first rows and second two rows, but let's stick to "pages".

int page = 0;      // start with page 0
unsigned long previousMillis;
const unsigned long interval = 7000UL;

...

if (currentMillis - previousMillis >= interval) 
{
  previousMillis = currentMillis;

  if( page == 0)
  {
    // show page 0
  }
  else if( page == 1)
  {
    // show page 1
  }

  page++;          // next page
  if( page > 1)    
    page = 0;      // back to page 0
}

To avoid your display flickering from constant lcd.clear() and redraw, only do that when the timer expires

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define DS18B20 26
const unsigned long LCD_milis = 7000;

unsigned long lastExecutedMillis;
bool isFirstPage = true;

OneWire oneWire(DS18B20);
DallasTemperature Sensor(&oneWire);

hd44780_I2Cexp lcd;

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);
}

void loop() {
  unsigned long currentMillis = millis();
  Sensor.requestTemperatures();
  float temperature = Sensor.getTempCByIndex(0);
  Serial.println(temperature);

  if (currentMillis - lastExecutedMillis >= LCD_milis) {
    lastExecutedMillis = currentMillis;
    isFirstPage = !isFirstPage; // toggle page to display
    lcd.clear();
  }
  if ( isFirstPage ) {
    lcd.backlight();
    lcd.print("FirstLine  ");
    lcd.print("14");
    lcd.setCursor(0, 1);
    lcd.print("SecondLine ");
    lcd.print("85");
    lcd.print("%  ");
  }
  else {
    lcd.print("Page2Up: ");
    lcd.print("21");
    lcd.print((char)223);
    lcd.setCursor(0, 1);
    lcd.print("Page2Down: ");
    lcd.print(temperature, 1);
    lcd.print((char)223);
  }
}

You can extend that idea to draw all the static text when the page changes and then just update the temperature portion each time through loop()

1 Like

Thank you blh64, works like a charm!