#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# creepy_voice.py
# Measure distance using an ultrasonic module
# in a loop. Plays Mp3 at certain distance detection
#
# Author : Matt Hawkins
# Date   : 28/01/2013
# Revised: Nick Puente
# Date   : 3/09/2016

# -----------------------
# Import required Python libraries
# -----------------------
import time
import RPi.GPIO as GPIO
import os
import random

# -----------------------
# Define some functions
# -----------------------

def measure():
  # This function measures a distance
  GPIO.output(GPIO_TRIGGER, True)
  time.sleep(0.00001)
  GPIO.output(GPIO_TRIGGER, False)
  start = time.time()

  while GPIO.input(GPIO_ECHO)==0:
    start = time.time()

  while GPIO.input(GPIO_ECHO)==1:
    stop = time.time()

  elapsed = stop-start
  distance = (elapsed * 34300)/2

  return distance

def measure_average():
  # This function takes 3 measurements and
  # returns the average.
  distance1=measure()
  time.sleep(0.1)
  distance2=measure()
  time.sleep(0.1)
  distance3=measure()
  distance = distance1 + distance2 + distance3
  distance = distance / 3
  return distance

# -----------------------
# Main Script
# -----------------------

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
# Make sure to change this to your own
# pins if they are different
GPIO_TRIGGER = 18
GPIO_ECHO    = 24

print ("Ultrasonic Measurement")

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Wrap main content in a try block so we can
# catch the user pressing CTRL-C and run the
# GPIO cleanup function. This will also prevent
# the user seeing lots of unnecessary error
# messages.
try:

  while True:

    distance = measure_average()

    # In ordger to play a random clip each time
    # this variable will change with every loop 
    # iteration could also be placed in the if block
    randomNumber = random.randint(1,12)
    
    print ("Distance : %.1f" % distance)

    # Choose a distance that works for you 
    # the 0 accounts for an echo that was never
    # returned this could be constantly 0 if not 
    # placed in front of an object/wall
    if(distance == 0 or distance < 124):
        #play mp3
        if(randomNumber ==1):
            os.system('mpg321 /home/pi/halloween/candy.mp3')
        
        if(randomNumber ==2):
            os.system('mpg321 /home/pi/halloween/gameover.mp3')

        if(randomNumber ==3):
            os.system('mpg321 /home/pi/halloween/ill_get_u_my_pretty.mp3')

        if(randomNumber ==4):
            os.system('mpg321 /home/pi/halloween/Jaws.mp3')
        
        if(randomNumber ==5):
            os.system('mpg321 /home/pi/halloween/screem.mp3')

        if(randomNumber ==6):
            os.system('mpg321 /home/pi/halloween/theyre_here.mp3')

        if(randomNumber ==7):
            os.system('mpg321 /home/pi/halloween/wolf_growl.mp3')

        if(randomNumber ==8):
            os.system('mpg321 /home/pi/halloween/witch_laugh.mp3')

        if(randomNumber ==9):
            os.system('mpg321 /home/pi/halloween/sicky.mp3')

        if(randomNumber ==10):
            os.system('mpg321 /home/pi/halloween/evil_laugh.mp3')
        
        if(randomNumber ==11):
            os.system('mpg321 /home/pi/halloween/squeek_Door.mp3')

        if(randomNumber ==12):
            os.system('mpg321 /home/pi/halloween/scarecrow.mp3')

    # time between loop iterations
    time.sleep(.25)

except KeyboardInterrupt:
  # User pressed CTRL-C
  # Reset GPIO settings
  GPIO.cleanup()
