Seeking for an idea or solution...

Hi there,

I have 5 buttons on the device and an LCD screen, I wish to make some sort of idle function like for example:

If no buttons are pressed in the past 5 minutes the backlight on the LCD will go OFF and if any of these 5 buttons where pressed the LCD backlight will go back ON.

Thank you for your suggestions, ideas and time,

Kind regards,
Viktor

When a button is pressed...

last_pressed_time = millis();
// turn back light on

Then repeatedly in loop...

now = millis();
if (now - last_pressed_time > timeout)
{
  //turn backlight off
}

Read the blink without delay tutorial/video if you are unsure. There's a link to it in the tutorials sticky.

pcbbc:
When a button is pressed…

last_pressed_time = millis();

// turn back light on




Then repeatedly in loop...


now = millis();
if (now - last_pressed_time > timeout)
{
  //turn backlight off
}




Read the [blink without delay](https://www.youtube.com/watch?v=3VZgaJBrZD8) tutorial/video if you are unsure. There's a link to it in the tutorials sticky.

So, to every button to add this line to reset the timer or?

last_pressed_time = millis();

I can't help feeling that there is more to the question than meets the eye. More details please

As to reading the buttons, it sounds like it would be better if you put the button pins in an arrays and iterated through them reading their state. Are you doing a digitalRead() or analogRead() on each of them ?

beic:
So, to every button to add this line to reset the timer or?

Yes, every time any button is pressed reset the timer.
Then when no button has been pressed for the timeout period the "backlight off" if statement will execute.

Of course this code does rely on you coding our sketch according to best practices. i.e. with no long delays or long executing loops.
But as you haven't posted any example code we can only surmise...

UKHeliBob:
I can't help feeling that there is more to the question than meets the eye. More details please

As to reading the buttons, it sounds like it would be better if you put the button pins in an arrays and iterated through them reading their state. Are you doing a digitalRead() or analogRead() on each of them ?

Not a Digital or Analog reads here, all buttons are working over i2c IO expander (like matrix style).

But, pcbbc's suggestion is very interesting and easy to implement.

pcbbc:
Yes, every time any button is pressed reset the timer.
Then when no button has been pressed for the timeout period the "backlight off" if statement will execute.

That's what I need

pcbbc:
Of course this code does rely on you coding our sketch according to best practices. i.e. with no long delays or long executing loops.

Exactly, I'm not using any delay(); only millis()

pcbbc:
But as you haven't posted any example code we can only surmise...

The code is very long, the main sketch is like 4x A4 pages long and there is 7x tabs more (using ESP8266).

Not a Digital or Analog reads here, all buttons are working over i2c IO expander (like matrix style).

Whilst the principle described by pcbbc is valid for any form of button input it would have been nice to know the details from the start of the thread in case it affected the answer