im fairly new to python and GPIO, i am using a light sensor and trying to write a script that plays an MP3 when it is dark and the moment the light comes up turns it off, i have tried using IF statements and ended up with it working however it waits till the end of the MP3 when the light comes off before stopping. i want it to be instant the moment the light comes on the sound to stop and start again the moment the light goes dark. so i tried the below.
here is the last code ive started to use:
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)
GPIO.setup(18,GPIO.OUT)
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()any help much appreciated.
Thanks
Mike M