Advertisement
Guest User

Light_trigger.py

a guest
Feb 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import os
  3. import time
  4. GPIO.setmode(GPIO.BOARD)
  5. GPIO.setwarnings(False)
  6. GPIO.setup(7, GPIO.OUT)#Light
  7. GPIO.setup(26, GPIO.IN)#Motion Sensor
  8. GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Switch
  9. GPIO.setup(11, GPIO.OUT)#Switch LED
  10.  
  11. countdown = 300 #Countdown Time in seconds until light is deactivated
  12. x = 0
  13. auto_disabled = False
  14.  
  15. def cls():
  16.     os.system("clear")
  17.  
  18. def light_toggle():
  19.  if GPIO.input(7):
  20.     GPIO.output(7,False)#LIGHT OFF
  21.     GPIO.output(11,True)#BUTTON LIGHT ON
  22.     print "Turning Light Off"
  23.  else:
  24.     GPIO.output(7,True)#LIGHT ON
  25.     GPIO.output(11,False)#BUTTON LIGHT OFF
  26.     print "Turning Light On"
  27. n = 0
  28. while(n != 1)#Loops infinitely
  29.  
  30.     if (GPIO.input(18)==0):
  31.         light_toggle()
  32.         print "Light switching on/off"
  33.         time.sleep(1)
  34.         x = 0
  35.        
  36.  
  37.     elif (GPIO.input(18)==1):
  38.      print("Waiting for button press.")
  39.      x = 0
  40.      cls()
  41.    
  42.      if (GPIO.input(7)==0):
  43.       lighton = False
  44.      elif (GPIO.input(7)==1):
  45.       lighton = True
  46.        
  47.     print ("Light on: %ls" %lighton)
  48.  
  49.     while (GPIO.input(7)==1 and GPIO.input(26)==0 and GPIO.input(18)==1 and x != countdown): #Loops while Light is on, no movement is detected and switch isn't pressed
  50.      shutdown_time = countdown - x
  51.      mins = shutdown_time / 60
  52.      secs = shutdown_time % 60
  53.      if x == int(x):
  54.       print ("Turning light off in %d secs. (%d Minute(s) and %d Second(s).)" %(shutdown_time, mins, secs))
  55.      time.sleep(0.25)
  56.      x = x + 0.25
  57.      if (x == countdown):
  58.       light_toggle()
  59.       print ("Light turned off due to inactivity.")
  60.       auto_disabled = True
  61.  
  62.     while (GPIO.input(7)==0 and GPIO.input(26)==1 and auto_disabled == True):
  63.      light_toggle()
  64.      print("Movement detected, re-enabling lighting.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement