I am looking for some help with my Christmas lights project. I have decided to start playing with Python and GPIO and thought as it's christmas it would be an interesting learning experience.
So far I have got a working circuit and can flash two sets of LED christmas lights using sequences I have set in Python. I have a button on the board too, this is also confirmed working I can use it to start a sequence.
What I am looking to accomplish is having a sequence start when the program starts and use the button to change the sequence.
I am sure I need to use threading and interrupts but I am not getting the results I was hoping for. The button running in it's own thread works fine and I can get it to print to screen on input but when controlling the lights thread it waits until the thread is complete to receive another input.
I assume what I need is:
Interrupt (from button)
Stop lights thread
Pick next in sequence
Start Lights Thread
Any help would be great, I have been going over it all night and am lost!
Thanks
Code: Select all
import RPi.GPIO as GPIO
import time
import threading
from threading import Thread
GPIO.setmode(GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(16,GPIO.IN)
red = 26
blue = 18
def reset():
GPIO.output(26, True)
GPIO.output(26, False)
GPIO.output(18, True)
GPIO.output(18, False)
def blink(repeat, colour, speed):
for n in range(repeat):
GPIO.output(colour, True)
time.sleep(speed)
GPIO.output(colour, False)
time.sleep(speed)
def end():
GPIO.cleanup()
raise SystemExit
return
def setone():
reset()
blink(2, red, .5)
blink(4, blue, .5)
blink(6, red, .025)
blink(6, blue, .05)
blink(7, red, .1)
return
def settwo():
reset()
blink(6, red, .025)
blink(6, blue, .025)
blink(3, red, .1)
blink(3, blue, .1)
for n in range(64):
blink(1, red, .025)
blink(1, blue, .025)
return
def setthree():
reset()
blink(4, blue, .5)
blink(4, red, .5)
blink(2, blue, .025)
blink(2, red, .025)
for n in range(128):
blink(1, blue, .02)
blink(1, red, .02)
return
def button():
GPIO.add_event_detect(16, GPIO.RISING, callback=alert, bouncetime=150)
def lights(func):
reset()
func()
def alert(channel):
seq = [setone, settwo, setthree]
reset()
print "Detected"
print str(seq[0])
sq = seq[0]
Thread(target = lights(seq[0])).join()
if __name__ == '__main__':
Thread(target = button).start()
Thread(target = lights(setone)).join()