1

i am new with RPi.. can someone help me, i am working on a RPi project.

i am using lcd touch screen with guizero. i create 5 pushbuttons to turn on 5 different led. BUT i cant turn off the LED after 5 seconds. can someone help me?

below is my code


from guizero import App, Text, PushButton
from gpiozero import LED
from time import sleep

led1 = LED (26)
led2 = LED (19)
led3 = LED (13)
led4 = LED (6)
led5 = LED (5)

app = App(title="Smart Shelves", height=500, width=800, layout="grid")


button = PushButton (app, command=led1.on, text="Borang Faks", grid=[0,1])
sleep(5)

button = PushButton (app, command=led2.on, text="Borang Sijil Cuti Sakit", grid=[0,2])
sleep(5)

button = PushButton (app, command=led3.on, text="Senarai Kehadiran Mesyuarat", grid=[0,3])
sleep(5)

button = PushButton (app, command=led4.on, text="Borang Pemilihan WP", grid=[0,4])
sleep(5)

button = PushButton (app, command=led5.on, text="Borang Cuti Gantian / Tanpa Rekod", grid=[0,5])
sleep(5)

pause()
  • 1
    edit your question, select your code, click on {}, save edits ... then your code will be readable at least – Jaromanda X Mar 28 '19 at 13:15
2

First of all, remove all the sleep statements from your button declarations. All they will do is delay your code where you don't want it to be delayed. Second, create a function to activate the LEDs. Within that function, turn on the LED, then wait 5 seconds, then turn it off. Also, turn the leds into an list of leds instead of seperate variables. That way, you can activate a certain led based on it's index in the array.

Here's the code.

from guizero import App, Text, PushButton
from gpiozero import LED
from time import sleep

# List of LEDS:
leds = [LED (26), LED (19), LED (13), LED (6), LED (5)]

app = App(title="Smart Shelves", height=500, width=800, layout="grid")

def led_flash(led, duration=5):
    leds[led].on()
    sleep(duration)
    leds[led].off()

# Here, we set the command to a lambda function which calls the led_flash function with the proper led number.
button = PushButton (app, command=lambda: led_flash(0), text="Borang Faks", grid=[0,1])
# sleep(5) # remove these sleeps

button = PushButton (app, command=lambda: led_flash(1), text="Borang Sijil Cuti Sakit", grid=[0,2])

button = PushButton (app, command=lambda: led_flash(2), text="Senarai Kehadiran Mesyuarat", grid=[0,3])

button = PushButton (app, command=lambda: led_flash(3), text="Borang Pemilihan WP", grid=[0,4])

button = PushButton (app, command=lambda: led_flash(4), text="Borang Cuti Gantian / Tanpa Rekod", grid=[0,5])

pause()
| improve this answer | |
  • 1
    Hi scitronboy. Tq so much.your codes work perfectly. ๐Ÿ’๐Ÿ’๐Ÿ’ – iznurul ahmad radzali Mar 29 '19 at 1:45
  • @iznurulahmadradzali Great! Could you please check the grey/green checkmark to the left of my answer? That way, people will know that this question has been solved. – scitronboy Mar 29 '19 at 17:57
1

At the moment you have command=led1.on etc.

You would need to change that to a different command.

def my_command_1()
   led1.on()
   time.sleep(5)
   led1.off()

You would then replace command=led1.on with command=my_command_1

Personally I would make the led number a parameter so you only need one function rather than 5. But to do that I'd have to understand the overall objective and learn more about guizero.

| improve this answer | |
  • Would that be blocking? I'm not familiar with guizero. – scitronboy Mar 28 '19 at 19:37

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.