Ammar Ashraf
Posts: 3
Joined: Mon Oct 01, 2018 6:05 pm

Interfacing Big SpaceBar Button with Raspberry Pi & perform certain task on pressing

Mon Oct 01, 2018 6:50 pm

Hi,
I am working on a project in which I have to play 5 mp3 songs in a sequence one after another. Each song has a duration of 10s. For that purpose, I am using pygame library. All is working fine but I have to add one for future in this application that is I want to start and pause these mp3 songs on pressing this BIG Button that is connected through one of the serial port of Raspberry Pi. I am a beginner level and don't know how to do this. Kindly! help me. Your help will be appreciated

Code: Select all

import time
import random
import RPi.GPIO as GPIO
import pygame
pygame.mixer.init()

# setting a current mode
GPIO.setmode(GPIO.BCM)
#removing the warings 
GPIO.setwarnings(False)
#creating a list (array) with the number of GPIO's that we use 
pins = [16,20,21] 

#setting the mode for all pins so all will be switched on 
GPIO.setup(pins, GPIO.OUT)
GPIO.output(16,  GPIO.HIGH)
GPIO.output(20,  GPIO.HIGH)
GPIO.output(21,  GPIO.HIGH)
temp = ''

def song():
    if (select == 'a'):
            #1............................................................
            pygame.mixer.music.load("Shinsuke-Nakamura.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(16,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(16,  GPIO.HIGH)
            temp = 'a'
            return temp
    if (select == 'b'):
            #2............................................................
            pygame.mixer.music.load("John-Cena.mp3")
            pygame.mixer.music.play() 
            time.sleep(0.2)
            GPIO.output(20,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(20,  GPIO.HIGH)
            temp = 'b'
            return temp
    if (select == 'c'):
            #3............................................................
            pygame.mixer.music.load("Brock-Lesnar.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(21,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(21,  GPIO.HIGH)
            temp = 'c'
            return temp
    if (select == 'd'):
            #4............................................................
            pygame.mixer.music.load("Gold-Berg.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(16,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(16,  GPIO.HIGH)
            temp = 'd'
            return temp
    if (select == 'e'): 
            #5............................................................
            pygame.mixer.music.load("Randy-Orton.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(20,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(20,  GPIO.HIGH)
            temp = 'e'
            return temp
    if (select == 'f'):
            #6............................................................
            pygame.mixer.music.load("Braun-Strowman.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(21,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(21,  GPIO.HIGH)
            temp = 'f'
            return temp
    if (select == 'g'):
            #7............................................................
            pygame.mixer.music.load("The-UnderTaker.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(16,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(16,  GPIO.HIGH)
            temp = 'g'
            return temp
    if (select == 'h'):  
            #8............................................................
            pygame.mixer.music.load("The-Kane.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(20,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(20,  GPIO.HIGH)
            temp = 'h'
            return temp
    if (select == 'i'):  
            #9............................................................
            pygame.mixer.music.load("Triple-H.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(21,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(21,  GPIO.HIGH)
            temp = 'i'
            return temp
    if (select == 'j'):  
            #10...........................................................
            pygame.mixer.music.load("The-Shield.mp3")
            pygame.mixer.music.play()
            time.sleep(0.2)
            GPIO.output(20,  GPIO.LOW)
            time.sleep(10)
            #pygame.mixer.music.stop()
            GPIO.output(20,  GPIO.HIGH)
            temp = 'j'
            return temp 
      
try:
    while 1:
        select = random.choice('abcdefghij')
        
        if (temp != select):
            temp = song() 
        else:
            print('repeat')

except KeyboardInterrupt:
 GPIO.cleanup()       # clean up GPIO on CTRL+C exit
 pygame.mixer.music.stop()
pygame.mixer.music.stop() 
GPIO.cleanup()           # clean up GPIO on normal exit 

Attachments
Big Button.JPG
Big Button.JPG (26.91 KiB) Viewed 575 times

Return to “Beginners”