I2C LCD millis

hello guys, im trying to make a ds3231 based clock with 16x2 display, both connected via i2c
after powering on, lcd is dark as intented but after pressing touch plate backlight turns on and stays on until i press it again.
i want it to turn on for 5 seconds after touching and then go black again
i want to use millis() instead of delay() because when delay is used, after i press touch plate the backlight turns on but time isnt changing for entire period of backlight activity
what am I doing wrong? please help me understand this

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

#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x3F,16,2); 

#define touchPin 2 

int period = 5000;
unsigned long time_now = 0;
   
DS3231 clock;
RTCDateTime dt;

void setup()
{
  lcd.init();
  lcd.noBacklight();
  pinMode(touchPin, INPUT);
  clock.begin();

  // clock.setDateTime(__DATE__, __TIME__);
  lcd.clear();
}



void loop()
{
  dt = clock.getDateTime();                //get time and date from rtc
  int touchValue = digitalRead(touchPin);

  lcd.setCursor(3,0);                          //display all values
  lcd.print(dt.day);   
  lcd.setCursor(5,0);
  lcd.print("/");
  lcd.setCursor(6,0);
  lcd.print(dt.month);
  lcd.setCursor(8,0);
  lcd.print("/");
  lcd.setCursor(9,0);
  lcd.print(dt.year); 
  lcd.setCursor(4,1);
  lcd.print(dt.hour);
  lcd.setCursor(6,1);
  lcd.print(":");
  lcd.setCursor(7,1);
  lcd.print(dt.minute);
  lcd.setCursor(9,1);
  lcd.print(":");
  lcd.setCursor(10,1);
  lcd.print(dt.second); 
  
  if (touchValue == HIGH){                   //turn backlight on for 5 seconds after touching sensor
    lcd.backlight();
    if(millis() > time_now + period){
    time_now = millis();
    {
    lcd.noBacklight();                           //turn off after 5 seconds
    }
    }
  
   delay(1000);
}
  }

I’m on my mobile so I can’t post code. Do you press the button and it turns on immediately after booting (< 5s)? Does the light come on if you press the button after 5s?

I would remove the if statement for the time from the statement for the button press. As is, you will never ask if the interval has passed if you are not pressing the button.

Also remove the curly brackets around nobacklight, why are they there?

gredzikk:
what am I doing wrong? please help me understand this

Still using delay :wink:

I would not update the display every time you go through loop; it usually results in flickering. Only update when the time changes.

void loop()
{
  // remember the second that the display was updated last
  static int lastDisplayUpdate=0
  
  dt = clock.getDateTime();                //get time and date from rtc
  int touchValue = digitalRead(touchPin);

  if(dt.second != lastDisplayUpdate)
  {
    lastSecond = dt.second;

    // update display here
    ...
    ...
  }

  ...
  ...
}

To get your 5 second timing working, you will need to set time_now when the button becomes pressed. The simplest is probably to use an additional variable to remember that the backlight timing is in progress.

Add the following variable to the beginning of loop()

void loop()
{
  // remember backlight in progress timing
  static bool backlightInProgress = false;

  ...
  ...
}

Next replace your touchValue code at the end of loop() by the below

  // only process 'button' press if backlight timing is not in progress
  if (touchValue == HIGH && backlightInProgress == false)
  {
    // backlight on
    lcd.backlight();
    // remember the time that we switched the backlight on
    time_now = millis();
    // indicate that backlight timing is in progress
    backlightInProgress = true;
  }

  // if backlight timing in progress
  if (backlightInProgress == true)
  {
    // check the time
    if (millis - time_now >= period)
    {
      // backlight off
      lcd.noBacklight();
      // indicate that backlight timing is no longer in progress
      backlightInProgress = false;
    }
  }

Please note that both code parts make use of a static variable to remember things.

So far the principles; question is why you want to update the display when the backlight is off?

Note:
can’t compile (and obviously test) as I don’t have the library.

thanks fo replies.
chuckyx, i left this clock running overnight and in the morning touch plate was still working like a toggle
also, in first 5s after booting it works as toggle too

sterretje thanks for advices, i will try your solution later today, thanks in advance

so your point is that when backlight is active, the clock updates, and when it goes off clock stops too?

i wanted to update it constantly because in daylight time and date is visible without backlight

gredzikk:
i wanted to update it constantly because in daylight time and date is visible without backlight

Ok, did not understand that; you can ignore that part of the reply.