I am trying to write a program which will flicker LEDs (and eventually - hopefully - flicker a small strip of LEDs). It's meant to look like lightning - or flickering electricity (for an electric chair halloween prop) I've cobbled together the following from MANY sources. The script should have the bulb start off. When the button is pressed, it runs the loop and flickers the LED. This works, but when the button is let go of, it should switch the bulb off and exit that loop. But not the script. It should then just sit and wait for the button to be pressed again. Scratching my head here!! Thanks for any help. import RPi.GPIO as GPIO import time import random import math bulb = 18 button = 23 pwms = [] intensity = 1.0 def setupGPIO(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(bulb, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) # GPIO.add_event_detect(button, GPIO.BOTH, callback=handleButtonPress, bouncetime=300) def handleButtonPress(): p = GPIO.PWM(bulb, 300) p.start(0) while True: GPIO.wait_for_edge(button, GPIO.FALLING) p.ChangeDutyCycle(random.randint(1, 44) * math.pow(intensity, 2) if intensity > 0 else 0) rand_flicker_sleep() while GPIO.input(button) == GPIO.LOW: print("button is being pressed") def rand_flicker_sleep(): time.sleep(random.randint(3, 10) / 100.0) def flicker_it(_): global intensity intensity = min(0, 1.0) def main(): try: setupGPIO() handleButtonPress() except KeyboardInterrupt: pass finally: for p in pwms: p.stop() GPIO.cleanup() if __name__ == '__main__': main() It's on a Rpi 3b using python3 (if that makes a difference!) Continue reading...