How to properly idle without sleep?

I wanted to set up a snippet into my existing code that if it has been running without button press for a while, it dims the LED display.

2 buttons are attached via interrupts and I do need to maintain the LED display or the 4 pins output so those needs to remain active. I get the impression sleep mode would turn those outputs off? If so, is this a safe idle loop to use?

void displayidle() {
  for (int x = LEDdefault; x <= 0; x--); {
    sevseg.refreshDisplay();
    sevseg.setBrightness(LEDdefault - x);
    delay(50);
    if (x < 1) x = 1;
  }

displayidle loop is triggered when buttonidle has reached 10,000 (one increment per main loop or a few hundreds per second). If the button was pressed (either of them), it resets buttonidle to 0. When it reaches 10,000, it should jump to displayidle loop.

There, it loops until X reaches 1 and stays at 1 (minimum display brightness) and never reaches zero so it remains in permanent loop. The interrupt of the button should work to end this loop? Or am I making a mountain out of a molehill? The entire code is a bit over 5k (only idle bit is shown) and I have plenty of spare flash space on ATMega328 for any more coding to make for optional idle loop.

delay 50 should mean the display would go from preset brightness (1-100 permitted in sevseg library, I have it at 70) to completely dim in about 3.5 seconds.

I get the impression sleep mode would turn those outputs off?

From where does that impression originate?

Google search but I probably didn't find a good explanation on how sleep worked or how it affected the outputs while in sleep.

Delta_G:
I would just take a count from millis at each button press. In the loop, look at the current value of millis. If it has been longer than 10 seconds or 100 seconds or whatever you want since the last button press, then dim the screen. In the button press routines you could add code to make it bright again if it was dim.

With what you have the screen will be always dim. Once the code reaches that for loop it can never escape. An interrupt may run, but will return to the middle of that for loop that it interrupted. None of the other code outside of your interrupts or that for loop will ever run again until you reset the board.

Didn't realize the loop would become permanently stuck. Changed it to be inline, increment a variable each time the main loop runs until it's been x times. There's an if time > xx then turn off display else update display.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.

...R

:wink:

for (int x = LEDdefault; x <= 0; x--) ; {