rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Halloween, pir sensor, scary video

Sun Oct 01, 2017 12:19 am

Hi All,

I'm looking to create a scare for this coming Halloween using and PI 3, pir sensor, and old monitor that will randomly play a short video from a small selection (3-5) of video's. I've found one page that shows how to do one video, http://blogs.arcsoftwareconsultancy.com ... halloween/ but I'm not sure how to have it randomly choose a video. This is my first attempt at anything with a Pi and coding but am not a computer noob. Thanks!

Ron

Hoagie
Posts: 119
Joined: Sun Sep 23, 2012 7:19 pm

Re: Halloween, pir sensor, scary video

Tue Oct 03, 2017 8:44 pm

I'm don't really know python but here's my best guess.

Change this line in the second file
scareFile = "3secondScare.mp4"
to something like this
scareFile = ["3secondScare1.mp4","3secondScare2.mp4","3secondScare3.mp4","3secondScare4.mp4","3secondScare5.mp4"]
Where the filenames are the names of your scary files.

Then you'll need a random integer with something like this
x = random.randint(0, 4)

Finally, change video[1] = scareFile to video[1] = scareFile[x]

This will choose a random filename from an array of 5 filenames. I'm not too sure on the random number part though.

rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Re: Halloween, pir sensor, scary video

Wed Oct 04, 2017 12:35 pm

Thanks Hoagie. I'll give it a shot and report back with the results.

AJB2K3
Posts: 215
Joined: Sun Aug 11, 2013 3:25 pm

Re: Halloween, pir sensor, scary video

Thu Oct 05, 2017 7:21 pm

Ahh, someone else who wants to set up an atmosfx screen powered by a pi
Back in the old days, we were smarter then the technology.
Nowadays technology makes us look like villages fools in front of the children!

rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Re: Halloween, pir sensor, scary video

Thu Oct 05, 2017 10:18 pm

Thanks Hoagie, the code you gave me didn't work but it did send me down the right path. After a few hours of searching and trial and error (first time coding and using Pi) I was able to get it to randomly play a video. Then some more time to get it to play without showing the desktop, it helps if you read what options are available :lol: I wanted to get a black screen to show between motion triggers but was mentally exhausted so I went K.I.S.S. and went with a black background, hid the taskbar and put the mouse in the lower corner. I'll post the code later. Thanks again.

AJB2K3 - yeah, I wasn't spending upwards of $250 on a brightsign player when I knew it could be done cheaper.

berrymix
Posts: 16
Joined: Thu May 25, 2017 2:03 am
Location: Florida

Re: Halloween, pir sensor, scary video

Fri Oct 06, 2017 7:49 am

This is perfect, few more days before Halloween. :lol:

rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Re: Halloween, pir sensor, scary video

Tue Oct 10, 2017 1:20 am

Hey all,

Sorry for pasting the code in the message as it's not letting me add attachments.
Here's the code I used:

pirDetect.py:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time


class detector(object):
        def __init__(self, sensor):
                self.callBacks = []
                self.sensor = sensor
                self.currState = False
                self.prevState = False

                GPIO.setmode(GPIO.BOARD)
                GPIO.setup(self.sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

        def read(self):
                self.prevState = self.currState
                self.currState = GPIO.input(self.sensor)

        def printState(self):
                print ("GPIO pin {0} is {1}").format(self.sensor, "HIGH" if self.currState else "LOW")

        def subscribe(self, callBack):
                self.callBacks.append(callBack)

        def callBack(self, state):
                for fn in self.callBacks:
                        fn(state)

        def start(self):
                self.read()
                self.printState()
                while True:
                        self.read()
                        if self.currState != self.prevState:
                                self.printState()
                                self.callBack(self.currState)
                        time.sleep(.1)
scarynew2.py:

Code: Select all

#!/usr/bin/python
import subprocess as sp
import time
from pirDetect import *
import random
import os

directory = ("/home/pi")

video = ["omxplayer", "filename", "-b"]
scareFile = ["1.mp4", "2.mp4", "3.mp4", "4.mp4", "5.mp4", "6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4", "11.mp4", "12.mp4", "13.mp4", "14.mp4", "15.mp4", "16.mp4", "17.mp4", "18.mp4", "19.mp4", "20.mp4", "21.mp4", "22.mp4", "23.mp4"]

def onMotion(currState):
        if currState:
		scarefile = ["1.mp4", "2.mp4", "3.mp4", "4.mp4", "5.mp4", "6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4", "11.mp4", "12.mp4", "13.mp4", "14.mp4", "15.mp4", "16.mp4", "17.mp4", "18.mp4", "19.mp4", "20.mp4", "21.mp4", "22.mp4", "23.mp4"]
                video[1] = (random.choice(scarefile))
                subVideo = sp.Popen(video)
                while subVideo.poll() is None:
                        time.sleep(.1)

objDetect = detector(7)
objDetect.subscribe(onMotion)
objDetect.start()
Last edited by rld612 on Sat Oct 21, 2017 12:50 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Halloween, pir sensor, scary video

Fri Oct 13, 2017 7:12 pm

Can you edit your post and wrap some [CODE] ... your code here ... [/CODE] tags round your python program.

That makes the difference between

if code_tags <> "used":
print "Code is unreadble, whitespace is lost"
else:
print "Code is nicely formatted"

and

Code: Select all

if code_tags <> "used":
  print "Code is unreadble, whitespace is lost"
else:
  print "Code is nicely formatted"
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Re: Halloween, pir sensor, scary video

Sat Oct 21, 2017 12:52 am

Edited. Thanks for the tip. Guess the email update doesn't work since I didn't get a notification that someone replied. Just happened to check if someone replied.

awl74
Posts: 3
Joined: Wed Oct 25, 2017 5:41 pm

Re: Halloween, pir sensor, scary video

Wed Oct 25, 2017 5:52 pm

Hi Thanks for everyones input! Halloween and Pi what a great combo... I am brand new to Pi and Python and in the processs of setting up my halloween haunt. Everything is working great, but I would like to add a little more functionality. I would like the trick or treaters to decide via radio button what kind of experience they want.

Press button 1 = launches kid friendly experience (1.mp4 - 4.mp4)
Press Buttton 2 = scary experience . (5.mp4 - 9.mp4)
Press button 3 = Terrifying! (10.mp4 - 14.mp4)

I was thinking that a button press could trigger a different playlist

Any Ideas how I could implement this function? Please excuse my noobness! Any help would be appreciated! This is what i am currently using.

Code: Select all

#scary.py

#!/usr/bin/python
import subprocess as sp
import time
from pirDetect import *
import random
import os

directory = ("/home/pi")

video = ["omxplayer", "filename", "-b"]
scareFile = ["6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4"]

def onMotion(currState):
        if currState:
		scarefile = ["6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4"]
                video[1] = (random.choice(scarefile))
                subVideo = sp.Popen(video)
                while subVideo.poll() is None:
                        time.sleep(.1)

objDetect = detector(7)
objDetect.subscribe(onMotion)
objDetect.start()



#PIR DETECTOR

#!/usr/bin/python
import RPi.GPIO as GPIO
import time


class detector(object):
def __init__(self, sensor):
self.callBacks = []
self.sensor = sensor
self.currState = False
self.prevState = False

GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def read(self):
self.prevState = self.currState
self.currState = GPIO.input(self.sensor)

def printState(self):
print ("GPIO pin {0} is {1}").format(self.sensor, "HIGH" if self.currState else "LOW")

def subscribe(self, callBack):
self.callBacks.append(callBack)

def callBack(self, state):
for fn in self.callBacks:
fn(state)

def start(self):
self.read()
self.printState()
while True:
self.read()
if self.currState != self.prevState:
self.printState()
self.callBack(self.currState)
time.sleep(.1)

awl74
Posts: 3
Joined: Wed Oct 25, 2017 5:41 pm

Re: Halloween, pir sensor, scary video

Wed Oct 25, 2017 5:53 pm

Hi Thanks for everyones input! Halloween and Pi what a great combo... I am brand new to Pi and Python and in the processs of setting up my halloween haunt. Everything is working great, but I would like to add a little more functionality. I would like the trick or treaters to decide via radio button what kind of experience they want.

Press button 1 = launches kid friendly experience (1.mp4 - 4.mp4)
Press Buttton 2 = scary experience . (5.mp4 - 9.mp4)
Press button 3 = Terrifying! (10.mp4 - 14.mp4)

I was thinking that a button press could trigger a different playlist

Any Ideas how I could implement this function? Please excuse my noobness! Any help would be appreciated! This is what i am currently using.

Code: Select all

#scary.py

#!/usr/bin/python
import subprocess as sp
import time
from pirDetect import *
import random
import os

directory = ("/home/pi")

video = ["omxplayer", "filename", "-b"]
scareFile = ["6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4"]

def onMotion(currState):
        if currState:
		scarefile = ["6.mp4", "7.mp4", "8.mp4", "9.mp4", "10.mp4"]
                video[1] = (random.choice(scarefile))
                subVideo = sp.Popen(video)
                while subVideo.poll() is None:
                        time.sleep(.1)

objDetect = detector(7)
objDetect.subscribe(onMotion)
objDetect.start()


Code: Select all


#PIR DETECTOR

#!/usr/bin/python
import RPi.GPIO as GPIO
import time


class detector(object):
        def __init__(self, sensor):
                self.callBacks = []
                self.sensor = sensor
                self.currState = False
                self.prevState = False

                GPIO.setmode(GPIO.BOARD)
                GPIO.setup(self.sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

        def read(self):
                self.prevState = self.currState
                self.currState = GPIO.input(self.sensor)

        def printState(self):
                print ("GPIO pin {0} is {1}").format(self.sensor, "HIGH" if self.currState else "LOW")

        def subscribe(self, callBack):
                self.callBacks.append(callBack)

        def callBack(self, state):
                for fn in self.callBacks:
                        fn(state)

        def start(self):
                self.read()
                self.printState()
                while True:
                        self.read()
                        if self.currState != self.prevState:
                                self.printState()
                                self.callBack(self.currState)
                        time.sleep(.1)
Btw here is where I found the original code used: http://blogs.arcsoftwareconsultancy.com ... mment-2198

awl74
Posts: 3
Joined: Wed Oct 25, 2017 5:41 pm

Re: Halloween, pir sensor, scary video

Sun Oct 29, 2017 6:49 am

Okay this is what i was able to cobble together, it seems to be working pretty well..

Code: Select all

# # buttonlauncher this allows the user to select their experience kid friendly, scary 

#!/usr/bin/env python

from time import sleep  # Allows us to call the sleep function to slow down our loop
import RPi.GPIO as GPIO # Allows us to call our GPIO pins and names it just GPIO
import subprocess
 
GPIO.setmode(GPIO.BCM)  # Set's GPIO pins to BCM GPIO numbering
BUTTON_1 = 17           # Sets our input pins
BUTTON_2 = 26           # Sets our input pins
BUTTON_3 = 25           # Sets our input pins
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Set our input pin to be an input, with internal pullup resistor on
GPIO.setup(BUTTON_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Set our input pin to be an input, with internal pullup resistor on
GPIO.setup(BUTTON_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Set our input pin to be an input, with internal pullup resistor on

# Create functions to run when the buttons are pressed
print 'Select your scare'

def Input_1(channel):
	print 'Kids'
	subprocess.call(['python', 'kidscare.py']); # Put whatever Button 1 does in here
    
    
def Input_2(channel):
	print 'Scary'
	subprocess.call(['python', 'scary.py']); # Put whatever Button 1 does in here;
    

# Wait for Button 1 to be pressed, run the function in "callback" when it does, also software debounce for 300 ms to avoid triggering it multiple times a second
GPIO.add_event_detect(BUTTON_1, GPIO.BOTH, callback=Input_1, bouncetime=300) 
GPIO.add_event_detect(BUTTON_2, GPIO.BOTH, callback=Input_2, bouncetime=300) # Wait for Button 2 to be pressed
GPIO.add_event_detect(BUTTON_3, GPIO.BOTH, callback=Input_3, bouncetime=300) # Wait for Button 3 to be pressed

# Start a loop that never ends
while True:
    # Put anything you want to loop normally in here
    
    sleep(60);           # Sleep for a full minute, any interrupt will break this so we are just saving cpu cycles.
    
The following will play a kid friendly experience based on a mp4 playlist played at random

Code: Select all

#kidscare.py

#!/usr/bin/python
import subprocess as sp
import time
from pirDetect import *
import random
import os

directory = ("/home/pi")

video = ["omxplayer", "filename", "-b"]
scareFile = ["15.mp4", "16.mp4", "17.mp4"]

def onMotion(currState):
        if currState:
		scarefile = ["15.mp4", "16.mp4", "17.mp4"]
                video[1] = (random.choice(scarefile))
                subVideo = sp.Popen(video)
                while subVideo.poll() is None:
                        time.sleep(60)
                        sp.call(['python', 'buttonlaunch.py']); # Put whatever Button 1 does in here
                        # time.sleep(.2)
                        
objDetect = detector(7)
objDetect.subscribe(onMotion)
objDetect.start()
Hope this helps someone.. it was fun to experiment ! Happy Hollween

rld612
Posts: 6
Joined: Sun Oct 01, 2017 12:08 am

Re: Halloween, pir sensor, scary video

Sun Oct 29, 2017 6:02 pm

Cool. I wish I could have helped you but I am also new to Pi. Happy Haunting!!

User avatar
StevenP
Posts: 57
Joined: Tue Aug 14, 2012 7:55 pm
Location: Germany / UK

Re: Halloween, pir sensor, scary video

Mon Oct 30, 2017 11:27 am

Great project, I made a similar device to play scary Halloween mp3 clips - a random choice of 25 clips. My code was also similar, but there was one small improvment I used to avoid hard coding the filenames into the program. Instead of:

Code: Select all

scarefile = ["15.mp4", "16.mp4", "17.mp4"]
For your example, it would be:

Code: Select all

scarefile = os.listdir('/home/pi/videos') # all videos must be in folder /home/pi/videos 
This creates the list of videos automatically and so lets you easily add more videos or songs by FTP to the RPi without changing the program itself, and saved me a lot of time while adding and deleting mp3's remotely from the RPi.
(Note you also need:)

Code: Select all

import os
Just an idea! Happy Halloween! :o
"The time has come," the Walrus said,
"To talk of many things..."

grimmer
Posts: 1
Joined: Thu Oct 25, 2018 8:21 pm

Re: Halloween, pir sensor, scary video

Thu Oct 25, 2018 9:21 pm

Did you ever get this to work. Im working on something similiar using atmosfx scary portrait. Im a noob to pythom and need help

Return to “Automation, sensing and robotics”