yauzi
Posts: 1
Joined: Fri May 10, 2013 7:25 am

JukePi

Fri May 10, 2013 7:40 am

Hi,

I'm building a jukebox controlled by the rpi, and created some python code to control it (I'm a beginner here).
The way it's supposed to work is that when a button is pressed (there are 7 of them), the rpi will play an mp3 (using mpg321, but any other format is fine too) and a few leds will light up (that part is yet to be written). The thing is, once a song is played there shouldn't be an option to start another (i.e., the buttons doesn't work), but once a song has ended the user can pick any song to play next.

Would be glad to have any advice with this, here is the code I got so far (only allows one song to play once each time, no loop).

Thanks!

Code: Select all

#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# Buttons:
GPIO.setup(15, GPIO.IN)
GPIO.setup(17, GPIO.IN)
GPIO.setup(18, GPIO.IN)
GPIO.setup(21, GPIO.IN)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)


while True:
        if ( GPIO.input(15) == False ):
                os.system('mpg321 1.mp3 &')
				
		break

        elif ( GPIO.input(17) == False ):
                os.system('mpg321 2.mp3 &')
				
		break

        elif ( GPIO.input(18) == False ):
                os.system('mpg321 3.mp3 &')
				
		break

        elif ( GPIO.input(21) == False ):
                os.system('mpg321 4.mp3 &')
				
		break

        elif ( GPIO.input(22) == False ):
                os.system('mpg321 5.mp3 &')
				
		break

		elif ( GPIO.input(23) == False ):
                os.system('mpg321 6.mp3 &')
				
		break
		
        elif ( GPIO.input(24) == False ):
				os.system('mpg321 7.mp3 &')
				
		break

		sleep(0.1);

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: JukePi

Fri May 10, 2013 1:18 pm

Not quite sure what you want help with but I think your indentation on the 'break' statements is wrong. The way you have it at the moment, unless you're pressing button 1 when you run the script nothing will happen and you'll only ever have the option of playing song 1.
The break statements in each else/elif should be directly under the os.system('mpg121 etc') function, that way it will stay in the loop until a button is pressed, then play the relevant mp3, then 'break' out of the loop.

Hope that makes some sense (indentation is everything in Python).
Dave.
Apple say... Monkey do !!

Return to “Python”