MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

GPIO Zero Cleanup Command?

Mon Dec 10, 2018 4:59 pm

I'm using the 3D Xmas tree with LEDs from the Pi Hut https://thepihut.com/products/3d-xmas-t ... spberry-pi with the following code:

Code: Select all

from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
tree = LEDBoard(*range(2,28),pwm=True)
for led in tree:
 led.source_delay = 0.1
 led.source = random_values()
pause()
When I use Control-C to stop the program, all LEDs don't turn off; they stop blinking, but some remain lighted.

What command should be used with GPIO Zero to turn off all the LEDs when I want to exit the program? Thanks for help!
Last edited by MacsandaPi on Tue Dec 11, 2018 4:43 pm, edited 1 time in total.

Andyroo
Posts: 4180
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: GPIO Zero Cleanup Command?

Mon Dec 10, 2018 9:38 pm

I think you link is a bit odd - is this the one https://thepihut.com/products/3d-xmas-t ... spberry-pi - if so it beats the heck out of the USB one I have :lol: :lol: :lol: as all the Pi will do is turn it on.

Have you seen the documents for the commands at all https://gpiozero.readthedocs.io/en/stab ... oards.html :D

You have two possible ways:

1) Loop through all the lights setting each to zero using the

Code: Select all

for led in tree:
    led.source = 0
(assuming '0' is the off...)

2) Set them all off in one go

Code: Select all

tree.off()
For myself, I would go for a couple of mods:

a) Light up all the lights as the loop ends
b) Slowly turn them out randomly
Need Pi spray - these things are breeding in my house...

MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

Re: GPIO Zero Cleanup Command?

Tue Dec 11, 2018 5:01 pm

I think you link is a bit odd

Hi Andyroo--Thanks for pointing out that malformed link; I've corrected it in my original post.
Have you seen the documents for the commands at all https://gpiozero.readthedocs.io/en/stab ... oards.html

I did look at that site before I posted my question. The information there is largely beyond my comprehension. My skill level with GPIO Zero is really no more than being able to specify an LED by GPIO number and then turn it on or off with "led.on(), led.off()." :?

Many thanks for your suggestions. Your help is appreciated.

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: GPIO Zero Cleanup Command?

Wed Dec 12, 2018 4:56 am

To stop a program with CTRL-C and have it tidyup requires a little extra code.

Code: Select all

try:
  while True:
    # animation loop
    
except KeyboardInterrupt:
  pass
  
# tidy up here

MacsandaPi
Posts: 93
Joined: Tue Dec 30, 2014 7:44 pm

Re: GPIO Zero Cleanup Command?

Wed Dec 12, 2018 11:50 pm

Thanks, rhubarbdog, for your suggestion. I've used that "while True/except KeyboardInterrupt:" combination with the RPI.GPIO library before, but haven't been able to get it to work yet with the gpiozero library. I'll keep trying.

Return to “Beginners”