Turning on and off the OLED

Hi everyone. I am looking for a way to program OLED monochrome to switch on only when I am using it, and turn off 5 min after using it?

My screen is interfaced with cap1188 for touch buttons
I will appreciate any help. thanks!

void sleepDisplay(Adafruit_SSD1305 *display){
  display->command(SSD1305_DISPLAYOFF);
}
void wakeDisplay(Adafruit_SSD1305 *display){
  display->command(SSD1305_DISPLAYON);
}

I am not too sure how I can develop a countdown that I can use to call sleepDisplay() after the Oled has been used

You could use software timers (documented) which you’d just reset every time the touch was used.

Alternatively, Millis timing: https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing

Thank you for getting back to me.
I have tried millis timing already but just doesn’t work for me because the timer start counting when the Oled is on and i am using it. meaning the Oled can be off even when is in use.

static uint8_t lastTouch = 0;
uint8_t touched = cap.touched();

if(touched == 0){   // No touch ditected

	if (millis() - lastTouch >= 3000){
		sleepDisplay(&display);
		lastTouch = millis();
        }
}

but as for software timers i am just not too sure how I can make it reset every time the button is touched

That is also how you would use the millis timer. You need to make sure you update the lastTouch value every time the button is touched, with the current timestamp. You’re constantly comparing the time to the last time the button was touched, so as long as you keep that ‘lasttouch’ time updated, it shouldn’t ever turn off the screen until the time has passed.

1 Like

I’m wondering if you need to reset the timer after every call to display? I assume when you say “using it” you mean that you are changing programmatically what is shown on the screen.

When I say “using” it I mean as I continue to press the touch buttons (going from one menu to another) the UI should be on during that time and then 5 min after i am done pressing the touch buttons the UI should turn off.

Sort of screen saver

I get that. I’m just not too sure how I can approach that method really. I will appreciate it if you could show me with a small code for just a hint please :pray:.

We’ll trade. Show me a bit more of your code, and I’ll see what I can come up with ;)? Easier to do with a bit of context.

1 Like

If this variable is not global, make it global and just set it whenever you do what you said

That’s what this means

1 Like