On my Raspberry Pi I have this code to let LEDS blink in different hertz frequencies. Everything works fine, but I can´t stop it with ctrl+C.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from threading import Thread
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.IN)
#LED Blinking Function
def blink(port, hz):
GPIO.setup(port, GPIO.OUT)
pulse = 0.5/hz
dtm = time.time()
while True:
dtm+= pulse
time.sleep(dtm - time.time())
if GPIO.input(32) == 1:
GPIO.output(port, not GPIO.input(port))
else:
GPIO.output(port, GPIO.LOW)
#to make it easier to add new LED
def start(port, hz):
Thread(target=blink, args=(port, hz)).start()
#to add LED insert start(GPIOport, Hz)
start(15, 2)
start(16, 2)
start(18, 2)
start(22, 2)
start(29, 2)
I have tried so much, but I can´t handle it. Do you have any ideas to solve this problem?