Soo here is the thing. I have a script with a function delayedRing(params), which "activates" 3 GPIOs at the same time (or with delay, which is set with parameters), like so:
LED1 ON -> delay -> LED2 ON -> delay -> LED3 -> duration -> LED1 OFF -> delay -> LED2 OFF -> delay -> LED3 OFF
Here is the code:
def delayedRing(interval, duration, bellCount, delay):
try:
msg = "rang at " + getCurrentDate() + ' ' + getCurrentTime() + '\n'
f = open('/home/pi/zvonci/log.txt', 'a')
f.write(msg.encode('utf-8'))
f.close()
except:
print("error writing to file")
offLeds = 0
currentMs = time.clock()
while offLeds < bellCount:
ms = time.clock() - currentMs
if ms < duration:
GPIO.output(V, GPIO.HIGH)
if ms >= interval:
GPIO.output(S, GPIO.HIGH)
if ms >= 2 * interval:
GPIO.output(M, GPIO.HIGH)
elif offLeds == 0:
offLeds += 1
GPIO.output(V, GPIO.LOW)
if offLeds == 1 and ms >= float(duration + interval):
offLeds += 1
GPIO.output(S, GPIO.LOW)
if offLeds == 2 and ms >= float(duration + 2 * interval):
offLeds += 1
GPIO.output(M, GPIO.LOW)
It is working perfectly, but when the code is executing, the processor is under very high load (100% load for the whole duration), which is logical, since it is "activating" GPIOs everytime it loops over the code.
The question is, should I optimize my code, so it sends the signal only twice (when it is turned on and off) or should I let it be as it is?
This piece of code is run once or twice a day for ~4 minutes so it is not a lot.