0

This is the code:

#!/usr/bin/python3

from time import sleep
from gpiozero import LED, Button
from threading import Thread
from signal import pause, signal, SIGTERM

LEDs = [5, 17, 22, 19, 26]
LIGHTS = [LED(LEDs[0]), LED(LEDs[1]), LED(LEDs[2]), LED(LEDs[3]), LED(LEDs[4])]
pushButton = Button(21)
DELAY = 0.5

def cleanup(signum, frame):
    exit(2)


# TODO: Create a button callback function which will change blinking speed
def buttonLEDs():
    global DELAY
    while True:
        if pushButton.is_pressed:
            print("Click")
            DELAY += 0.1


def LEDseq():
    while True:
        for LED in LIGHTS:
            LED.on()
            sleep(DELAY)
            LED.off()
        for LED in reversed(LIGHTS):
            LED.on()
            sleep(DELAY)
            LED.off()


try:
    signal(SIGTERM, cleanup)
    # pushButton = buttonControl
    # pushButton.when_pressed = LEDseq
    LEDseq()
    # buttonControl(LEDseq)
    pause()

except KeyboardInterrupt:
    exit(1)

I'm trying to make a callback function for a button that would change the sleep interval of LEDseq when the button is pressed so I created this function:

but it doesn't seem to work. Any idea why?

  • You need to add your full code. Add a print statement inside your 'if .. is_pressed' – CoderMike Apr 5 at 12:47
  • @CoderMike I added the print inside the if ... is_pressed as I also updated my question with a full. Check it out. And thank you for your response – MrYosuko Apr 5 at 13:32
  • That should be print('Click'). Does not look as though your code will even run - there is no flashLED()? – CoderMike Apr 5 at 13:38
  • @CoderMike I've edited the code and still, the button won't work when I press it the LEDs still at the same sequence speed. And sorry for flashLED() that was supposed to be LEDseq that I've forgotten to rename – MrYosuko Apr 5 at 13:49
  • @CoderMike I also updated the code in the thread with the correction that you mentioned – MrYosuko Apr 5 at 13:50
2

Your code will always be stuck inside the LEDseq() while loop.

Try the following, using the button when_pressed callback:

#!/usr/bin/python3

from time import sleep
from gpiozero import LED, Button
from threading import Thread
from signal import pause, signal, SIGTERM

LEDs = [5, 17, 22, 19, 26]
LIGHTS = [LED(LEDs[0]), LED(LEDs[1]), LED(LEDs[2]), LED(LEDs[3]), LED(LEDs[4])]
pushButton = Button(21)
DELAY = 0.5

def cleanup(signum, frame):
    exit(2)

def incrementDelay():
    global DELAY
    print("Click")
    DELAY += 0.1

def LEDseq():
    while True:
        for LED in LIGHTS:
            LED.on()
            sleep(DELAY)
            LED.off()
        for LED in reversed(LIGHTS):
            LED.on()
            sleep(DELAY)
            LED.off()


try:
    signal(SIGTERM, cleanup)
    pushButton.when_pressed = incrementDelay
    LEDseq()
    pause()

except KeyboardInterrupt:
    exit(1)
| improve this answer | |

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.