1

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.

  • The code is hardly War and Peace. You should edit it into your question. – joan May 11 '16 at 20:27
  • What do you mean by it hardly being a War and Peace? Will do! – Rok Dolinar May 11 '16 at 22:32
  • 1
    War and Peace is a very long book! – joan May 11 '16 at 22:33
  • Ohhhh haha, it struck me the moment I added the comment. The code is now in my question :) – Rok Dolinar May 11 '16 at 22:35
2

If you add a time.sleep(0.01) into the while loop, it will use a lot less time.

I modified the code slightly for my convenience but it seems to work.

#!/usr/bin/env python

import time
import pigpio

V=23
S=24
M=25

def delayedRing(interval, duration, bellCount, delay):

    offLeds = 0
    currentMs = time.time()

    while offLeds < bellCount:
        ms = time.time() - currentMs

        #print(offLeds, bellCount, ms, duration)

        if ms < duration:
            pi.write(V, pigpio.HIGH)
            if ms >= interval:
                pi.write(S, pigpio.HIGH)
                if ms >= 2 * interval:
                     pi.write(M, pigpio.HIGH)

        elif offLeds == 0:
            offLeds += 1
            pi.write(V, pigpio.LOW)
        if offLeds == 1 and ms >= float(duration + interval):
            offLeds += 1
            pi.write(S, pigpio.LOW)
        if offLeds == 2 and ms >= float(duration + 2 * interval):
            offLeds += 1
            pi.write(M, pigpio.LOW)

        time.sleep(delay)

pi = pigpio.pi()
if not pi.connected:
   exit(0)

delayedRing(2, 10, 3, 0.1)

pi.stop()

I guess it must depend on the values you are using for interval, duration, bell count, and delay.

As a matter of interest why not just use

LED1 ON
time.sleep(delay)
if bellcount > 1
   LED2 ON
   time.sleep(delay)
   if bellcount > 2
      LED3 ON
time.sleep(duration)
LED1 OFF
time.sleep(delay)
LED2 OFF
time.sleep(delay)
LED3 OFF
  • Ohh of course, 0.1 would be good to probably? – Rok Dolinar May 11 '16 at 22:31
  • @RokDolinar I wasn't sure how the loop logic would be affected by a longer delay so thought a hundredth a second was a reasonable start point. – joan May 11 '16 at 22:36
  • It probably could, I will test with different values and see what is the best. – Rok Dolinar May 11 '16 at 22:39
  • Welp, tried with both, but nothing works. The program freezes at time.sleep(value). If I terminate the program with ctrl + c, it shows that the line, where program stopped is time.sleep(). Everytime. – Rok Dolinar May 12 '16 at 10:21
  • 1
    @RokDolinar You probably should be using time.time() rather than time.clock(). I have added to the answer. – joan May 12 '16 at 12:24

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.