Page 1 of 1

Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 7:34 am
by saulton7
Hey all,

I am new to Pi's I have used Arduino before but frankly my coding abilities are lacking/very rusty.

I am trying to make a Pi zero that is powered from and outside power supply of 5V and does three things

-Play a sound file played on startup
-Have a safe shutdown sequence activated by button
-Have a sound file played at the press of a button

I already figured out how to get the sound to play on startup by adding the command in /etc/rc.local but I am having trouble getting the shutdown script to run as well. I was curious if there was a way to write a single script that could do all of these things, if so that would be awesome. Any and all help is much appreciated. Thanks!

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 8:01 am
by saulton7

Code: Select all

import RPi.GPIO as GPIO

import time

import os



GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN)

GPIO.setup(11, GPIO.IN)



aplay /etc/sound/Start.wav


while True:
   
	if (GPIO.input(7)):
        
		os.system("sudo shutdown -h now")

	if (GPIO.input(11)) == False):
      
		aplay /home/pi/Music/Sounds/Fire1.wav
        


        time.sleep(0.1);
This is what I got so far

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 10:49 am
by elParaguayo
You could certainly have a script that plays a sound when it starts and then listens for button presses.

The best way to learn (in my own view) is to:
  1. try some code
  2. change it
  3. inevitably cause an error
  4. search for the error
  5. see if I can fix, if I can, move on until I hit #3, if I can't, then I ask for help.
  6. repeat until everything works!
So, you've got some code already which is a great start (much better than just asking someone to write it). So what happens when you run it? What error messages do you get?

I'll give you a clue for starters, this line:

Code: Select all

aplay /etc/sound/Start.wav
will cause an error. That may work in a shell script (and I'm guessing you added it straight to /etc/rc.local) but it's not a python command so you'll need to have a see how to run shell commands in python (hint - you've done it elsewhere in your code).

As for the buttons, you may want to take a look at gpiozero which simplifies some of this.

Good luck and please post more again if you get stuck.

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 11:20 am
by saulton7

Code: Select all

import os

from time import sleep


import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BOARD)

GPIO.setup(7, GPIO.IN)

GPIO.setup(11, GPIO.IN)


os.system('mpg123 -q start1.mp3 &')


while True:

    if (GPIO.input(7)):

        os.system('sudo shutdown -h now &')

    if (GPIO.input(11)):

        os.system('mpg123 -q Fire1.mp3 &')

	break

	time.sleep(0.1)
	

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 11:24 am
by saulton7
elParaguayo wrote:You could certainly have a script that plays a sound when it starts and then listens for button presses.

The best way to learn (in my own view) is to:
  1. try some code
  2. change it
  3. inevitably cause an error
  4. search for the error
  5. see if I can fix, if I can, move on until I hit #3, if I can't, then I ask for help.
  6. repeat until everything works!
So, you've got some code already which is a great start (much better than just asking someone to write it). So what happens when you run it? What error messages do you get?

I'll give you a clue for starters, this line:

Code: Select all

aplay /etc/sound/Start.wav
will cause an error. That may work in a shell script (and I'm guessing you added it straight to /etc/rc.local) but it's not a python command so you'll need to have a see how to run shell commands in python (hint - you've done it elsewhere in your code).

As for the buttons, you may want to take a look at gpiozero which simplifies some of this.

Good luck and please post more again if you get stuck.
Thanks for the help, ive worked at it some more and this batch of code sort of works. I get the startup sound to play. and I can get one of the two conditions to work. As in I press one of them and then the script ends and I would have to restart it. Is there a way to keep the script working even after i press one of the buttons? I am specifically using this code to have a raspberry pi zero play sounds in a Prop laser rifle. where upon powering up there is a played sound, followed by the rifle making a firing noise whenever the trigger is pressed. With the shutdown button being used for safe shutdown of the Pi before main power being cut off. So how would I keep the script running while also taking into account the need for debouncing of the inputs?

Thanks again

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 12:53 pm
by elParaguayo
Take the "break" line out (it exits your loop).

You may also want to use a print statement or something else instead of the shutdown command while you're testing your script!

Re: Code for a Pi Zero Sound Player

Posted: Fri Nov 11, 2016 8:58 pm
by saulton7
Removing the break produces the same result. Any thoughts?

Re: Code for a Pi Zero Sound Player

Posted: Sat Nov 12, 2016 7:13 am
by elParaguayo
Is it exiting cleanly, or is there an error message?

Re: Code for a Pi Zero Sound Player

Posted: Tue Nov 15, 2016 6:07 am
by saulton7
When I remove the break, there is sort of an error message as in the screen show multiple prints of "shutdown" and the sound for the laser rifle starts to fire erratically. I suspect it has something to do with the inputs and the bouncing effect.

Re: Code for a Pi Zero Sound Player

Posted: Tue Nov 15, 2016 6:39 am
by elParaguayo
Indeed. There are plenty of examples on the forum of debouncing.

I suspect the gpiozero module has this built in.

Re: Code for a Pi Zero Sound Player

Posted: Tue Nov 15, 2016 7:23 am
by saulton7

Code: Select all

import os 
from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
#check_call(['sudo', 'poweroff'])
print ('SHUT')

def laser(): 
# os.system(mpg123 -q Fire1.mp3 &')
print ('BANG')

shutdown_btn = Button(17, hold_time=2)
shutdown_btn.when_held = shutdown

laser_btn = Button(4, bounce_time=200)
laser_btn.when_pressed = laser

pause() 

Re: Code for a Pi Zero Sound Player

Posted: Tue Nov 15, 2016 7:25 am
by saulton7
This is my try at using GPIOzero. It has been working a little bit better. As the code runs now, I can press the shutdown button and produce the SHUT action as many times as I want, but the BANG action will only work once and then no more. Any help appreciated!

I figure that I need to create a loop of sorts, but my knowledge and skill is very limited. Where should I start?

edit : I am very stuck i clearly know nothing about loops

Re: Code for a Pi Zero Sound Player

Posted: Tue Nov 15, 2016 12:09 pm
by elParaguayo
Are you using an old version of the software?

A similar issue was reported here: http://raspberrypi.stackexchange.com/qu ... -only-once

Re: Code for a Pi Zero Sound Player

Posted: Thu Nov 17, 2016 6:09 am
by saulton7
I updated everything and the same events are occurring. Can anyone replicate this on their own Pi's? I'd like to solve this soon please and thanks :D