Why don't my LEDS stay on when the guizero buttons are pressed?

226 views Asked by At

I wrote a simple python dashboard, using guizero and gpiozero, to turn LEDS on and off. I only have a problem with the LEDS, they only turn on for 1 second before they turn off. I tried to use time.sleep() to see if the LEDS would stay on, but unfortunately that was not the case and that's why I'm asking this question. (I'm new to Python + I changed some lines for my privacy)

This is my code:

#!/usr/bin/env python
"""
When the "ok" buttons are pressed, the lights will turn on.
"""


from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from guizero import App, PushButton, Box, Text


__author__ = "Senne De Winter"
__email__ = "senne.dewinter@...."
__status__ = "Development"


IP = PiGPIOFactory(host='192.....')
LED1 = LED(21, pin_factory=IP)
LED2 = LED(20, pin_factory=IP)
LED3 = LED(16, pin_factory=IP)


def turn_led1_on():
    if LAMP1.bg == "white":
        LAMP1.tk.configure(bg="red")
        LED1.on()

    elif LAMP1.bg == "red":
        LAMP1.tk.configure(bg="white")
        LED1.off()


def turn_led2_on():
    if LAMP2.bg == "white":
        LAMP2.tk.configure(bg="red")
        LED2.on()

    elif LAMP2.bg == "red":
        LAMP2.tk.configure(bg="white")
        LED2.off()


def turn_led3_on():
    if LAMP3.bg == "white":
        LAMP3.tk.configure(bg="red")
        LED3.on()

    elif LAMP3.bg == "red":
        LAMP3.tk.configure(bg="white")
        LED3.off()


app = App(title="Dashboard", layout="grid", width=400)

invis_1 = Box(app, grid=[0, 0], width=80)
invis_2 = Box(app, grid=[4, 0], width=20)
invis_3 = Box(app, grid=[8, 0], width=20)

invis_4 = Box(app, grid=[0, 1], width=80, height=10)
invis_5 = Box(app, grid=[4, 1], width=20, height=10)
invis_6 = Box(app, grid=[8, 1], width=20, height=10)

invis_7 = Box(app, grid=[0, 2], width=80)
invis_8 = Box(app, grid=[4, 2], width=20)
invis_9 = Box(app, grid=[8, 2], width=20)

lamp1 = Box(app, border=True, grid=[2,0])
lamp2 = Box(app, border=True, grid=[6,0])
lamp3 = Box(app, border=True, grid=[12,0])

LAMP1 = Text(lamp1, text="LAMP 1")
LAMP1.tk.configure(background="white")

LAMP2 = Text(lamp2, text="LAMP 2", bg="white")
LAMP2.tk.configure(background="white")

LAMP3 = Text(lamp3, text="LAMP 3", bg="white")
LAMP3.tk.configure(background="white")

button1 = PushButton(app, turn_led1_on, text="OK 1", grid=[2,2])
button2 = PushButton(app, turn_led2_on, text="OK 2", grid=[6,2])
button3 = PushButton(app, turn_led3_on, text="OK 3", grid=[12,2])

app.display()

The invis boxes are invisible boxes for the layout of the dashboard.

1

There are 1 answers

1
calypsorrr On

Based on what I can see you need to switch your LED1.on() and your LED1.off() with LED1.toggle() with this you can push a button on your dashboard to turn them on and off.

Here is a example

def exit_app():
sys.exit()

def toggle_led():
    led_01.toggle()
    led_02.toggle()
    led_03.toggle()
    led_04.toggle()
    if led_01.is_lit:
        ledButton.text = "LED OFF"
    else:
        ledButton.text = "LED ON"

def led_blink():
    for i in range(10):
        led_01.on()
        led_02.on()
        led_03.on()
        led_04.on()
        time.sleep(1)
        led_01.off()
        led_02.off()
        led_03.off()
        led_04.off()

# App
app = App(title="Project house", height=400, width=400)

# Boxes
displayBox = Box(app, border= True, height=35, width=400)
buttonBox = Box(app, layout="grid", align="top")

# Title
title = Text(displayBox, text="Push a button")
title.text_size = 20

# Buttons
ledButton = PushButton(buttonBox, toggle_led, text="LED ON", grid=[0,0])
ledButton.text_size = 25
blinkButton = PushButton(buttonBox, led_blink, text="Push to party", grid=[9,0])
blinkButton.text_size= 25
exitButton = PushButton(app, exit_app, text="EXIT", align="bottom", width=15 , 
height=2)
exitButton.text_size = 32

app.display()