Hello is there a alternative to time.sleep? Because I want to let my LEDs blink in the exact amount of Hz what is not able because to call time.sleep needs time too, so the blinking needs more time than expected.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from threading import Thread
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.IN)
def blink(port, hz):
GPIO.setup(port, GPIO.OUT)
while True:
if GPIO.input(32) == 1: //lever activated?
GPIO.output(port, GPIO.HIGH)
time.sleep(0.5/hz)
GPIO.output(port, GPIO.LOW)
time.sleep(0.5/hz)
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, 4)
start(18, 6)
start(22, 12)
start(29, 24)