fusebox
Posts: 3
Joined: Thu Apr 25, 2019 11:53 pm

PIR Sensor Multiple Actions

Thu Oct 31, 2019 7:24 am

Hello,

I am attempting to create a sensor that will play sound files one at a time after sensing movement. (PIR sensor senses movement, sound_file_1 plays, wait for a set amount of "cool down time", wait for PIR sensor to detect movement, play sound_file_2, wait for set "cool down", wait for PIR sensor to detect movement, play sound_file_3...ect)

My current code allows for one sound file to be triggered effectively, but I am not sure how to separate the playbacks.

Code: Select all

import os
import Rpi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

pirPin=26
GPIO.setup(pirPin,GPIO.IN)
cooldown = 60

def MOTION_one(pirPin):
	print("motion 1 detected")
	os.system("aplay SOUND1.wav")
	
	time.sleep(cooldown)
	print("all clear")
	
def MOTION_two(pirPin):
	print("motion2 detected")
	os.system("aplay SOUND2.wav")
	
	time.sleep(cooldown)
	print("all clear")

def MOTION_three(pirPin):
	print("motion3 detected")
	os.system("aplay SOUND3.wav")
	
	time.sleep(cooldown)
	print("all clear")	

print("motion sensor test")
time.sleep(2)
print("ready")

try:
	GPIO.add_event_detect(pirPin, GPIO.RISING, bouncetime=200)
	GPIO.add_event_callback(pirPin, MOTION_one)
	GPIO.add_event_callback(pirPin, MOTION_two)
	GPIO.add_event_callback(pirPin, MOTION_three)
	while1:
		time.sleep(1)
		
Any help is greatly appreciated
Last edited by fusebox on Thu Oct 31, 2019 3:29 pm, edited 1 time in total.

pcmanbob
Posts: 7925
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR Sensor Multiple Actions *Time Sensitive*

Thu Oct 31, 2019 10:20 am

Hi.

Try this code

Code: Select all

import os
import Rpi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

pirPin=26
GPIO.setup(pirPin,GPIO.IN)
cooldown = 60

def MOTION_one():
	print("motion 1 detected")
	os.system("aplay SOUND1.wav")
	
	time.sleep(cooldown)
	print("all clear")
	
def MOTION_two():
	print("motion2 detected")
	os.system("aplay SOUND2.wav")
	
	time.sleep(cooldown)
	print("all clear")

def MOTION_three():
	print("motion3 detected")
	os.system("aplay SOUND3.wav")
	
	time.sleep(cooldown)
	print("all clear")	

print("motion sensor test")
time.sleep(2)
print("ready")

sound = 1
while true:

    while GPIO.input(pirPin) == 0:
            time.sleep(0.2)
            
    if sound == 1:
        MOTION_one()
        
    if sound == 2:
        MOTION_two()   

    if sound == 3:
        MOTION_three()    
        
    sound = sound + 1
    if sound == 4:
        sound = 1
        
        
untested so expect errors
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 57
Joined: Sat Jul 06, 2019 2:57 am

Re: PIR Sensor Multiple Actions *Time Sensitive*

Thu Oct 31, 2019 12:23 pm

I would write only one def and use the counter to build the name of the sound file. This way you can easily add of subtract sound files as needed with out having to write or delete def. Something like this:

Code: Select all

x=1

def MOTION(x):
	sound_file = ('aplay SOUND' + x + '.wav')
	print('motion ' x ' detected')
	os.system(sound_file)
	
	time.sleep(cooldown)
	print("all clear")
	
try
	while x in range [1,3]:
		while GPIO.input(pirPin) == 0:
           		 time.sleep(0.2)
		MOTION(x)
		x = x + 1
		if x ==4:
			x=1
		time.sleep(1)		
	
There are probably better ways to handle that counter but I am just not thinking of it right now. The syntax may not all be correct so there might be a little troubleshooting. I think this will give you a cleaner code and all you have to do is change the range of x to add or subtract sound files.

fusebox
Posts: 3
Joined: Thu Apr 25, 2019 11:53 pm

Re: PIR Sensor Multiple Actions *Time Sensitive*

Thu Oct 31, 2019 2:52 pm

pcmanbob wrote:
Thu Oct 31, 2019 10:20 am
Hi.

Try this code

Code: Select all

import os
import Rpi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

pirPin=26
GPIO.setup(pirPin,GPIO.IN)
cooldown = 60

def MOTION_one():
	print("motion 1 detected")
	os.system("aplay SOUND1.wav")
	
	time.sleep(cooldown)
	print("all clear")
	
def MOTION_two():
	print("motion2 detected")
	os.system("aplay SOUND2.wav")
	
	time.sleep(cooldown)
	print("all clear")

def MOTION_three():
	print("motion3 detected")
	os.system("aplay SOUND3.wav")
	
	time.sleep(cooldown)
	print("all clear")	

print("motion sensor test")
time.sleep(2)
print("ready")

sound = 1
while true:

    while GPIO.input(pirPin) == 0:
            time.sleep(0.2)
            
    if sound == 1:
        MOTION_one()
        
    if sound == 2:
        MOTION_two()   

    if sound == 3:
        MOTION_three()    
        
    sound = sound + 1
    if sound == 4:
        sound = 1
        
        
untested so expect errors
PCMAN Bob, this is exactly what my sleep-deprived brain couldn't quite get right.
It's now working perfectly. I will share working code shortly.
Thank You!

fusebox
Posts: 3
Joined: Thu Apr 25, 2019 11:53 pm

Re: PIR Sensor Multiple Actions

Thu Oct 31, 2019 3:40 pm

! Working Code !

Code: Select all

import os
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

pirPin =26
GPIO.setup(pirPin,GPIO.IN)
cooldown(60)

def MOTION_one():
	print("motion detected 1")
	os.system("aplay Sound1.wav")
	time.sleep(cooldown)
	print("all clear")
	
def MOTION_two():
	print("motion detected 2")
	os.system("aplay Sound2.wav")
	time.sleep(cooldown)
	print("all clear")
	
def MOTION_three():
	print("motion detected 3")
	os.system("aplay Sound3.wav")
	time.sleep(cooldown)
	print("all clear")
	
print("PIR Sensor Initializing")
time.sleep(0.2)
print("Ready to Sense")

sound =1

while True:
	while GPIO.input(pirPin) == 1:
		time.sleep(0.2)
		
		if sound ==1:
			MOTION_one()
			
		if sound ==2:
			MOTION_two()
			
		if sound ==3:
			MOTION_three()
			
		sound = sound +1
		if sound ==4:
			sound = 1
			
				
Thank you to all who responded and helped make this happen so expediently!

Sean_Creatif
Posts: 6
Joined: Mon Jul 01, 2019 7:26 pm

Re: PIR Sensor Multiple Actions

Fri Jan 24, 2020 9:32 pm

Can you post a photo of where you have your pins in place? I am trying to wire a pir motion sensor to a raspberry pi zero w, w/out using a breadboard.
Based on this image
Image
I would assume I just need to replace your pin(26) with pin(7)?

I would love to know how to get it to just play one sound file too?

pcmanbob
Posts: 7925
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR Sensor Multiple Actions

Sat Jan 25, 2020 11:33 am

Sean_Creatif wrote:
Fri Jan 24, 2020 9:32 pm
Can you post a photo of where you have your pins in place? I am trying to wire a pir motion sensor to a raspberry pi zero w, w/out using a breadboard.
If you look at this thread https://www.raspberrypi.org/forums/view ... 2&t=262882

i have just posted connection diagram for connecting and using a PIR without a breadboard, there is also basic test code there too.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”