Pedro Lopes
Published © GPL3+

Raspberry Pi Zero W Scary Halloween Ghost

It has a ultrasonic sensor to detect people presence. When it detects people, it jiggles, makes a random scary sound, and flashes a RGB LED.

IntermediateFull instructions provided10 hours1,152
Raspberry Pi Zero W Scary Halloween Ghost

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
Can use any small speaker, for example, from a old toy
×1
Maxbotix ultrasonic sensor
×1
RGB LED
×1
Servo Module (Generic)
×1
LM386
×1

Story

Read more

Schematics

Sounds

Schematic

Code

ghost.py

Python
Code used
import RPi.GPIO as GPIO
import time
import sys
import threading
from subprocess import call
import random
import wave
import contextlib

GPIO.setmode(GPIO.BOARD)

servo = 37
sensor = 18
red = 11
green = 13
blue = 15

sensorCount = 0
sound = 0
soundLen = 0.0

GPIO.setup(servo, GPIO.OUT)
GPIO.setup(sensor, GPIO.IN)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)

p = GPIO.PWM(servo, 50)
p.start(0)

call(["gpio_alt", "-p", "18", "-f", "5"])


def readSensorDistance():
	pulse_end = 0
	pulse_start = 0
	
	while GPIO.input(sensor) == 0:
		pulse_start = time.time()
	
	while GPIO.input(sensor) == 1:
		pulse_end = time.time() 
	
	return round(((pulse_end - pulse_start) * 6500.0) * 2.54, 2)


def doMotorMovements():
	global soundLen
	elapsedTime = 0.0
	startTime = 0.0
	endTime = 0.0
	while elapsedTime < soundLen:
		startTime = time.time()
		p.ChangeDutyCycle(9.5)
		time.sleep(0.1)
		p.ChangeDutyCycle(12.5)
		time.sleep(0.1)
		endTime = time.time()
		elapsedTime = elapsedTime + (endTime - startTime)
	p.ChangeDutyCycle(12.5)


def playSound(i):
	auxStr = "/home/pi/ghost/" + i + ".wav"
	print auxStr
	call(["aplay", auxStr])


def allColorOff():
	GPIO.output(red, 0)
	GPIO.output(green, 0)
	GPIO.output(blue, 0)
	
	
def doColor():
	global soundLen
	tOn = 0.05
	tOff = 0.05
	elapsedTime = 0.0
	startTime = 0.0
	endTime = 0.0
	while elapsedTime < soundLen:
		startTime = time.time()
		
		GPIO.output(red, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(green, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(blue, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(red, 1)
		GPIO.output(green, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(red, 1)
		GPIO.output(blue, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(green, 1)
		GPIO.output(blue, 1)
		time.sleep(tOn)
		allColorOff()
		time.sleep(tOff)
		
		GPIO.output(red, 1)
		GPIO.output(green, 1)
		GPIO.output(blue, 1)
		time.sleep(tOn)
		allColorOff()
		
		endTime = time.time()
		elapsedTime = elapsedTime + (endTime - startTime)

	
def getSoundLen(i):
	auxStr = "/home/pi/ghost/" + i + ".wav"
	with contextlib.closing(wave.open(auxStr, 'r')) as f:
		return f.getnframes() / float(f.getframerate())
	

while True:
	try:
		#print readSensorDistance()
		if readSensorDistance() < 130.0:
			sensorCount += 1
		else:
			sensorCount = 0
		#print sensorCount
		if sensorCount >= 3:
			#print "doing scary stuff"
			sound = random.randint(1, 9)
			soundLen = getSoundLen(str(sound))
			threading.Timer(0, doColor).start()
			threading.Timer(0, doMotorMovements).start()
			playSound(str(sound))
	except KeyboardInterrupt:
		GPIO.cleanup()
		sys.exit(0)
	except:
		print sys.exc_info()
	
 

Credits

Pedro Lopes

Pedro Lopes

2 projects • 15 followers

Comments