Page 1 of 1

Light Sensor Sound Play

Posted: Sat Apr 09, 2016 8:37 pm
by moran1804
Hi everyone,

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()
with this code it works but when it plays the looped MP3 the code stops checking to see the state change on the light sensor.

any help much appreciated.

Thanks
Mike M

Re: Light Sensor Sound Play

Posted: Mon Apr 11, 2016 2:33 am
by Davies
Hi, im fairly new to python also and think i understand what your trying to do.
i could be wrong with this and havent tested but id be tempted to run the mp3 in a threaded while and introduce some variables to make sure the loop doesnt get started repeatedly

Code: Select all

import os
import RPi.GPIO as GPIO
import time
import threading

run = 0
play = 0
def sound():
    global play
    while 1:
        if run == 1 and play == 0:
            os.system('sudo mpg123 -q --loop -1 /home/pi/RetroPie/Test.mp3')
            play = 1
        if run == 0 and play == 1:
            os.system('sudo pkill mpg123')
            play = 0
        time.sleep(1)


def my_back():
    global run
    while 1:
        if GPIO.input(4) and run == 0:     # if port 18 == 1
            run = 1
            print "Light is Dark"
            GPIO.output(18, True)
        if not GPIO.input(4) and run == 1                  # if port 18 != 1
            print "Light is Light"
            GPIO.output(18, False)
            run = 0
        time.sleep(1)
        
t = threading.Thread(target=sound)
t.daemon = True
t.start()
my_back()
or something like that...

if you want it to run more than once you might just need to change the end part

Code: Select all

while 1:
    GPIO.add_event_detect(4, GPIO.BOTH, callback=my_callback)
    time.sleep(60)
im not sure how event_detect works but if its continuous monitoring you might just need to remove

Code: Select all

time.sleep(60)
GPIO.cleanup()

Re: Light Sensor Sound Play

Posted: Mon Apr 11, 2016 7:46 am
by elParaguayo
Removing the sleep line will cause the script to exit instantly.

You've also posted the same question twice and have an answer on the other thread.

www.raspberrypi.org/forums/viewtopic.php?f=32&t=143896

Re: Light Sensor Sound Play

Posted: Mon Apr 11, 2016 11:59 am
by moran1804
Hi All,

sorry posted twice, pretty much resolved in the other Topic,

thank you so much for everyones help.

Thanks
Mike M