Blinkt retropie auto stop script help

Hi everyone,

I am a bit new to this, but had a quick question. I am building a retropie setup with the blinkt. I have a push button that turns it on, the blinkt comes on perfectly fine, just when shuts down the blinkt is still on as well. Anyone know what i can add to the rainbow.py code to get it to shutoff as well

rainbow.py code

#!/usr/bin/env python

import colorsys
import time

from blinkt import set_clear_on_exit, set_brightness, set_pixel, show


spacing = 360.0 / 16.0
hue = 0

set_clear_on_exit()
set_brightness(0.1)

while True:
    hue = int(time.time() * 100) % 360
    for x in range(8):
        offset = x * spacing
        h = ((hue + offset) % 360) / 360.0
        r, g, b = [int(c*255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
        set_pixel(x,r,g,b)
    show()
    time.sleep(0.001)

my shutoff code

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess


GPIO.setmode(GPIO.BOARD)  


GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)  

oldButtonState1 = True

while True:
    #grab the current button state
    buttonState1 = GPIO.input(5)

    # check to see if button has been pushed
    if buttonState1 != oldButtonState1 and buttonState1 == False:
      subprocess.call("shutdown -h now", shell=True, 
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
      oldButtonState1 = buttonState1

    time.sleep(.1)

You should re-post your code using backticks, like so:

```
Your code here
```

So it’s formatted nicely and we can read it!

I suspect all you need is a dash of set_clear_on_exit which should automatically clear the Blinkt! when the Python script exits: http://docs.pimoroni.com/blinkt/#enable-disable-clear-on-exit

The Blinkt! pixels are “intelligent” and have memory, so they wont turn off unless disconnected from power or explicitly told to.

Thank you so much, I’ve been trying to find that document all day haha, exactly what i was looking for. Thanks again

hmm. yeah after reading the doc and messing with the code setting it to true. The blinkt still stays on after shutdown. If i manually start the script however with sudo python or ./, i can stop it and it turns off. I have it set to start in my /etc/rc.local startup through. Even if i kill the pid after startup , the blinkt will still be on while the pi is running or not. Any other ideas ?

I think you might have to delve into the complexity of signal handlers to properly clean up the Blinkt! in this case.

Possibly something like this at the top of your code:

import signal
import sys

def signal_handler(signal, frame):
    blinkt.clear()
    blinkt.show()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

This catches the SIGINT signal which is sent by kill and cleans up the Blinkt!. In theory anyway!