Sbs-ula
Posts: 24
Joined: Mon Oct 23, 2017 11:22 am

light control with off delay

Mon Oct 23, 2017 5:56 pm

Hello everyone,
so my next project is a light control with PIR sensors.
I have one input to my raspberry and one output to the light relay.
The PIR sensor give me one 5 sec signal to my input, if he is detect any motion.

Now I want a python handling to turn the light on.
But not only light "on" - if a input form the sensor. I want also an off delay, if no more input, than hold on the light an turn it off after 30 sec.

So this is not my main problem, but if the PIR sensor make a new input signal in the off delay, then he doesn't extend the delay.

I found one diagramm that explain the plan:
Image

I hope you unterstand my situation and have a solution for this problem.

Currently I have only a script from github with minimal edit:

Code: Select all

#!/usr/bin/python
# -*- coding: utf8 -*-
 
import RPi.GPIO as GPIO
import time
import datetime
 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(35, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(12, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
 
GPIO_PIR = [16]
 
print "Ready"
 
#  GPIO Input
GPIO.setup(GPIO_PIR,GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
 
Current_State  = 0
Previous_State = 0

try:
 
 print "Ready"
 
 while GPIO.input(GPIO_PIR)==1:
   Current_State  = 0
 
 while True :
 
   Current_State = GPIO.input(GPIO_PIR)
 
	if Current_State==1 and Previous_State==0:
     GPIO.setup(40, GPIO.OUT)
     GPIO.output(40, GPIO.LOW)
     Previous_State=1
 
	elif Current_State==0 and Previous_State==1:

     Previous_State=0
     GPIO.setup(40, GPIO.OUT)
     time.sleep(80)
     GPIO.output(40, GPIO.HIGH)

	elif GPIO.input(12) == GPIO.HIGH:
		GPIO.setup(40, GPIO.OUT)
		GPIO.output(40, GPIO.HIGH)
		time.sleep(80)
		GPIO.output(40, GPIO.LOW)
	elif GPIO.input(35) == GPIO.HIGH:
		GPIO.setup(40, GPIO.OUT)
		GPIO.output(40, GPIO.HIGH)
		time.sleep(80)
		GPIO.output(40, GPIO.LOW	
 
except KeyboardInterrupt:
 print " -- End"
 GPIO.cleanup()
Thanks
Roman

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: light control with off delay

Mon Oct 23, 2017 7:56 pm

You will need to take the time when ever the pir detects motion and then be checking if the the current time minus the last dectime is greater than your wait ti e then turn off the light

Something like this but you will need to add all the needed other code

Code: Select all

import time

delay_time = 80 #time in seconds

while True:
  if GPIO.input(GPIO_PIR):
    start_time = time.time()
	#insert code to turn on light
  if time.time() - start time > delay_time :
    #insert code to turn off light

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: light control with off delay

Mon Oct 23, 2017 9:22 pm

Hi.

Well your posted code is a mess with multiple GPIO.setup commands through out the code so not even going to entertain editing it.

Most pir sensors output a high and low signal so there should be no need for any pull down on the setup unless yours is some how different.

So I started a fresh and this is how I would do what you want.

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.IN)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.LOW)

delay = 10 # set number of seconds delay before light turns off

while True:
    #wait for pir to trigger.
    print "waiting "
    while GPIO.input(18) == 0:
        time.sleep (0.5)

    print "turn light on here"
    GPIO.output(21, GPIO.HIGH)
    count = 0

    #start count down to turn off
    print "count down started "
    while count < delay:
        count = count + 1
        
    # here if the input goes high again we reset the counter to 0
        if GPIO.input(18) == 1:
            count = 0
        
        print "count down now ",  (delay - count)
        time.sleep(1)
        
    print "Turn light off"
    GPIO.output(21, GPIO.LOW)
    
So when the pir triggers the code it turns on the light then starts counting down, but while ever the pir input is high or goes high again the counter gets reset to zero, no matter how many times the pir input goes back to high.
Once the pir input stops going high the counter will count down to zero and switch of the light.

I have put in lots of print statements so you can see the code working these can be removed once you are happy with the code and its works as you require.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Sbs-ula
Posts: 24
Joined: Mon Oct 23, 2017 11:22 am

Re: light control with off delay

Wed Oct 25, 2017 5:18 am

With this codes work it fine.
Thank you very much!

Return to “Python”