0

I'm trying to setup a switch on a breakout board which, upon flicking the switch, will allow me to play a short mp3, then loop a second mp3 until I switch it off. When I do so, I want it to play a third mp3 (this is basically a siren starting up looping then shutting down). I can play mp3s using a push button but don't know how to use the switch, any help?

This is the current python script I'm using

import pygame.mixer
import RPi.GPIO as GPIO
from time import sleep
from sys import exit

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)

pygame.mixer.init(48000, -16, 1, 1024)

sndA = pygame.mixer.Sound("buzzer.wav")
sndB = pygame.mixer.Sound("clap.wav")
sndC = pygame.mixer.Sound("laugh.wav")

soundChannelA = pygame.mixer.Channel(1)
soundChannelB = pygame.mixer.Channel(2)
soundChannelC = pygame.mixer.Channel(3)

print "Soundboard Ready."

while True:
    try:
        if (GPIO.input(23) == True):
            soundChannelA.play(sndA)
        if (GPIO.input(24) == True):
            soundChannelB.play(sndB)
        if (GPIO.input(25) == True):
            soundChannelC.play(sndC)
        sleep(.01)
    except KeyboardInterrupt:
        exit()
2

I'm assuming the code you posted is for the push buttons?

Switches work very similar to push buttons. When you switch it on, voltage on your gpio port goes high until you switch it off. This is the same as if you were to hold a push button down for that same period of time. So lets modify this to your new requirements. You want:

  1. On switch play sound a
  2. Then loop sound b while switch is on
  3. Finally play sound c

So try using this for your infinite loop:

while True:
    try:
        if (GPIO.input(23)):               #When switched high
            soundChannelA.queue(sndA)      #Play sound a
            while (GPIO.input(23))         #While switch remains high
                soundChannelA.queue(sndb)  #Play sound b
            soundChannelA.queue(sndc)      #Play sound c
        sleep(.01)
    except KeyboardInterrupt:
        exit()

Keep in mind I haven't run this code, nor do I know anything about pygame.mixer so if the play function isn't blocking (doesn't wait for the sound to complete before continuing the program) its going to sound like a jumbled mess. To fix that you'll need to delay after each play call. If you do this however you wont detect switch changes until after each sound has completed.

  • brilliant this does what I wanted, however as soon as you switch, it plays the looping track over the top of the first sound and the same when you switch off, it plays over the last sound, I have tried queue instead of play, that didn't help, any ideas? – Ian Solway Jul 14 '14 at 0:49
  • I sorted this using the same channel and using queue instead of play – Ian Solway Jul 14 '14 at 9:23
  • That seems reasonable. As I mentioned, I don't know the details of pygame.mixer so I'm glad you could get it figured out. – Kevin Brey Jul 14 '14 at 15:39
  • I edited my answer to match the changes you mentioned – Kevin Brey Jul 14 '14 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.