i am new to this forum, maybe the entry here is in the wrong place.
I'm programming my Raspberry PI with a reed contact. When the door is opened, an audio file should be played back. So far I managed to do that, see Script, but the OMXPLAYER starts the playback of the song several times. But I want it to be given only once.
I am still a beginner in programming with Python, maybe you can help me! Many thanks in advance!
Cheers
André
Code: Select all
import time # so we can use "sleep" to wait between actions
import RPi.GPIO as io
import subprocess
## set GPIO mode to BCM
## this takes GPIO number instead of pin number
io.setmode(io.BCM)
## enter the number of whatever GPIO pin you're using
door_pin = 23
## use the built-in pull-up resistor
io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp
## initialize door
door=0
## this loop will execute the if statement that is true
while True:
## if the switch is open
if io.input(door_pin):
print("Door is open")
myprocess = subprocess.Popen(['mpg321','/home/pi/Musik/song.mp3'])
#time.sleep(5) # wait 0.5 seconds before the next action
## if the switch is closed and door does not equal 1
if (io.input(door_pin)==False and door!=1):
print("Door is closed")