How to set led timeout on LCD with buttons for Raspberry

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ltpitt
 
Posts: 75
Joined: Thu Jan 03, 2013 5:22 am

How to set led timeout on LCD with buttons for Raspberry

Post by ltpitt »

Hello there,

I just finished assembling and testing this beauty...

I would like to use a timeout of 10 seconds for its screen backlight.
The final result I have in mind would be:
if screen is off and user press any button turn it on, wait 10 seconds and turn it off again.
if screen is already on kill current counter and count 10 seconds again before turning the screen off.

I tried this but it works horribly and it uses a global variable which is, again, horrible.

Code: Select all

def turn_backlight_off():
    lcd.set_backlight(0.0)

def turn_backlight_on():
    global timer_backlight
    if timer_backlight:
        timer_backlight.cancel()
        timer_backlight = threading.Timer(10.0, turn_backlight_off).start()
    else:
        lcd.set_backlight(0.1)
        timer_backlight = threading.Timer(10.0, turn_backlight_off).start()

Any hint to tackle this problem?

User avatar
ltpitt
 
Posts: 75
Joined: Thu Jan 03, 2013 5:22 am

Re: How to set led timeout on LCD with buttons for Raspberry

Post by ltpitt »

I won!

In case anybody is fighting for this here's my example:

Code: Select all

period = datetime.timedelta(seconds=1)
next_time = datetime.datetime.now() + period
seconds = 0

while True:
        if next_time <= datetime.datetime.now():
            seconds += 1
            next_time += period
            if seconds == backlight_timeout:
                turn_backlight_off()
        # Loop through each button and check if it is pressed.
        for button in buttons:
                if lcd.is_pressed(button[0]):
                        seconds = 0
                        turn_backlight_on()

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”