sorry I'm fairly new to python.
i have installed a light sensor on the GPIO pins and what i want to happen is when there is light no MP3 to play.
when the room goes dark id like it to play the MP3 Looped.
i have managed to do just this with IF statements however whilst going through the loop it waits till the end of the MP3 to detect light and then kill the MP3. i have then tried to use GPIO.add_event_detect and that works however when it goes dark it starts looping the MP3 and does not shut off when light is detected.
what is the best way to program this so the minute it detects light it kills the MP3?
here is my latest script:
Code: Select all
#1 = Dark
#0 = Light
import os
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN) #Light Sensor on GPIO 4
GPIO.setup(18,GPIO.OUT) #LED im using to work along with the sound
def my_callback(channel):
if GPIO.input(4): # if port 18 == 1
os.system('sudo mpg123 -q --loop -1 /home/pi/RetroPie/Test.mp3')
print "Light is Dark"
GPIO.output(18, True)
else: # if port 18 != 1
print "Light is Light"
GPIO.output(18, False)
os.system('sudo pkill mpg123')
GPIO.add_event_detect(4, GPIO.BOTH, callback=my_callback)
time.sleep(60)
GPIO.cleanup()