

### PIR sensor

# change program name to sanitize.py
### Change background music
# Make new audio tracks
#Make new folder /home/pi/sanitize
#Change autostart to autoexec.sanitize ###


import RPi.GPIO as GPIO                           #Import GPIO library
import time                                       #Import time library
import os
import threading
import datetime

gain = str(-1200)    #sets the gain of the audio output

GPIO.setmode(GPIO.BOARD)                          #Set GPIO pin numbering
stop_pir = 26                                       #Associate pin 26 to pir
go_pir = 24
red_light = 23
green_light = 21
GPIO.setup(stop_pir, GPIO.IN)                          #Set pin as GPIO in
GPIO.setup(go_pir, GPIO.IN)
GPIO.setup(red_light, GPIO.OUT)
GPIO.setup(green_light,GPIO.OUT)
tr =.25   # red flashing
tg = .5   # green flashing

for x in range(0,3):    ## Check lights on bootup
    red_light = 1
    time.sleep(.5)
    red_light = 0
    green_light =1
    time.sleep(.5)   # green light is on
    
    

#######################  threading Plays the background music file eerie_sounds_7Hr.mp3

def play_clip():
    playing_clip = 1
    zzz = 'omxplayer ' + ' -o both --vol -1800 --loop ' +'/home/pi/distance/distance_audio_clips/eerie_sounds_7Hr.mp3'
    print zzz
    os.system(zzz)

threading.Thread(target=play_clip).start() #Plays background music
###########################
def flashing_red():
    green_light = 0
    for x in range(1,40):  ### Flashes for ten seconds
        red_light =1
        time.sleep(ts)
        red_light = 0
        time.sleep(ts)
        if go_pir:
            x=40
    green_light = 1
#############################

def flash_green():
    for x in range(1,3):
        green_light =1
        time.sleep(.5)
        green_light = 0
        time.sleep(.5)
    green_light = 1
    

#########################

j=1     #### Sound file index number Gets incemented after each playback
i=1     ### dummy variable for loop   

while 1==1:             ## 
    
    
    print "Waiting for sensor to settle ",j
    time.sleep(2)                                     #Waiting 2 seconds for the sensor to initiate
    print "Detecting motion"
     
    while True:
       
       filename = str(j)+'.mp3'             # sets the file name to be played
       if GPIO.input(stop_pir):                            #Check whether pir is HIGH
          ttime = datetime.datetime.now()          # saves the detection time
          green_light = 0
          for x in range(1,40):
              print x
              red_light = 1
              time.sleep(tr)
              red_light = 0
              time.sleep(tr)
              if go_pir:
                  x=40
          green_light = 1
          for x in range(1,4):
              ####
              print x
              time.sleep(tg)
              green_light = 0
              time.sleep(tg)
              green_light = 1  # green light is on
            
            
              



              
          
          
          print "Motion Detected!   ", filename,'  ',str(ttime)[0:19]  #
          
          xxx='omxplayer -o both --vol '+gain+ ' /home/pi/distance/distance_audio_clips/'+filename 
          os.system(xxx)                            ## Play audio clip
          if j == 25:               # 
              j=0
          j=j+1
          print "Detecting motion"
          file = open ('/home/pi/ir_sensor/logfile.txt','a')
          file.write(filename + '   '+str(ttime)[0:19]+'\n')
          file.close()
        

          
          time.sleep(2)                               #D1- Delay to avoid multiple detection
       time.sleep(0.1)                                #While loop delay should be less than detection(hardware) delay
      

