Use Blinkt to show door open or closed


#1

How would I code Blinkt to show red if a door’s closed, and green if it’s open?

I can do the magnetic switch OK, but can’t figure the code to get Blinkt to respond to changes in GPIO status.

Thank you.


#2

Anyone? Must be possible…


#3

You just want a loop continually reading the GPIO status and re-acting accordingly:

import RPi.GPIO as GPIO
from blinkt import *
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

while True:
    switch_open = GPIO.input(4)
    if switch_open:
        for i in range(8):
            set_pixel(i , 0, 255, 0)
    else:
        for i in range(8):
            set_pixel(i , 255, 0, 0)

    show()

#4

was about to post an example too (using gpiozero and a button but same principle).

Note that you can use the set_all() function if you want all pixels to be the same colour.


#5

Brilliant! Thank you, I’ll look at those over the next few days.


#6

No worries, thought there might be a set_all type function, but the blinkt example I grabbed didn’t have an example of it :)

Just be mindful of whether your reed-switch (or equivalent) is wired up as pull-up or pull-down as it will affect the logic in my example.


#7

That works just fine, thank you.

The set_all function is documented here: https://github.com/pimoroni/blinkt/blob/master/documentation/REFERENCE.md

How do you make it switch the LEDs off when you kill the script?


#8

Try something like this:

… note there’s a dedicated doc site for a number of Pimoroni libs, with more added as time permits:

https://docs.pimoroni.com