smithjames0
Posts: 42
Joined: Tue Oct 04, 2016 10:04 pm

Timer problem

Fri Nov 11, 2016 10:16 pm

Hi

I have a problem with a timer script, the timer function is triggered from another function. The problem I have is that the original function stops working for the length of time the timer is set for. I have googling but I am not quite sure what I need to looking for.

Code: Select all

    def insidelight():
        GPIO.output(Coop.PIN_INSIDE_LIGHT, GPIO.HIGH)
        time.sleep(20)
        GPIO.output(Coop.PIN_INSIDE_LIGHT, GPIO.LOW)

    def checkTriggers(self):
        while True:
            (top, bottom) = self.currentTriggerStatus()
            if (self.direction == Coop.UP and top == Coop.TRIGGERED):
                logger.info("Top sensor triggered")
                GPIO.output(Coop.PIN_LED_R, GPIO.HIGH) 
                GPIO.output(Coop.PIN_LED_G, GPIO.LOW) 
                self.stopDoor(0)
            if (self.direction == Coop.DOWN and bottom == Coop.TRIGGERED):
                logger.info("Bottom sensor triggered")
                GPIO.output(Coop.PIN_LED_R, GPIO.LOW)
                GPIO.output(Coop.PIN_LED_G, GPIO.HIGH) 
                self.stopDoor(1)
                self.insidelight()
Can anyone point me in the right direction? Thanks James

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Timer problem

Fri Nov 11, 2016 10:30 pm

Single threaded, i.e. sequential operation.
You need to investigate threading or multiprocessing so that your timed code can run separately.

smithjames0
Posts: 42
Joined: Tue Oct 04, 2016 10:04 pm

Re: Timer problem

Mon Nov 14, 2016 10:02 pm

Thanks I have had a play, this is my code

Code: Select all

        t5 = threading.Time(20.0, insidelight)
        t5.setDaemon(True)
........

  def insidelight(self):
             logger.info("Inside light on")
             GPIO.output(Coop.PIN_INSIDE_LIGHT, GPIO.LOW)
             time.sleep(20)
             GPIO.output(Coop.PIN_INSIDE_LIGHT, GPIO.HIGH)
             logger.info("Inside light off")
             t5.stop()
............

   def checkTriggers(self):
        while True:
            (top, bottom) = self.currentTriggerStatus()
            if (self.direction == Coop.UP and top == Coop.TRIGGERED):
                logger.info("Top sensor triggered")
                GPIO.output(Coop.PIN_LED_R, GPIO.HIGH)  
                GPIO.output(Coop.PIN_LED_G, GPIO.LOW)  
                self.stopDoor(0)
            if (self.direction == Coop.DOWN and bottom == Coop.TRIGGERED):
                logger.info("Bottom sensor triggered")
                GPIO.output(Coop.PIN_LED_R, GPIO.LOW) 
                GPIO.output(Coop.PIN_LED_G, GPIO.HIGH)  
                self.stopDoor(1)
                t5.start()

but I get t5 = threading.Time(20.0, insidelight) NameError: global name 'threading' is not defined

can anyone suggest where I am going wrong?

Thanks James

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Timer problem

Mon Nov 14, 2016 11:27 pm

Did you do a search for your error message. First hit on google seems to fix it.

I'm not trying to be mean, but python has pretty helpful error messages which should help you fix a lot of your problems. Learning to debug your own scripts is a key skill.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”