Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

How to change any value in python with an input

Tue Jun 11, 2019 8:51 am

Hello...
Can anyone help me with this?
I want to change (or replace) specific values and strings using a button input from the raspberry pi's GPIO pins. How can I get the desired results in python? Is it simple code? Or a library? I am confused.
Thank you!

pcmanbob
Posts: 7264
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to change any value in python with an input

Tue Jun 11, 2019 8:58 am

From your posted question its not 100% clear as to what you want to do,

So do you just want to press a button and that will set a variable value to a specific value, so you would have several buttons each with a value assigned,

or do you want to have buttons that allow you to increase or decrease a variable value.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Tue Jun 11, 2019 12:57 pm

Hello!
I am playing audio using pygame and when I press a button it plays the audio for me. That part is fine for me.
What I what to do is use another button to change the audio.
For example-

Code: Select all

Audio1 = ('/home/pi/Drum.wav')
I want to change the value of Audio1, say-

Code: Select all

Audio1 = ('/home/pi/AnotherSound.wav')

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:05 pm

So there is a dedicated button for each possible audio track? The sort of thing you would need if you were building a drum kit?

This documentation for the gpiozero python library seems to have an example of just what you need. See Section 2.14: https://gpiozero.readthedocs.io/en/stab ... -music-box

User avatar
rpiMike
Posts: 980
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:06 pm

It might help if you post the rest of your code.

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:10 pm

B. Goode, I want to have a single button to change the audio directories of 8 channels I have in pygame.

Code: Select all

import time                               #importimg the time library
import os                                   #Importing the os Library for Control Over Operating System
import RPi.GPIO as GPIO      #Importing the GPIO library
GPIO.setmode(GPIO.BOARD)   #Defining the Setmode for setup of GPIO pins
GPIO.setup(3,GPIO.IN)           #Determining each pin's purpose
GPIO.setup(16,GPIO.OUT)
GPIO.setup(29,GPIO.IN)
GPIO.setup(31,GPIO.IN)
GPIO.setup(32,GPIO.IN)
GPIO.setup(33,GPIO.IN)
GPIO.setup(35,GPIO.IN)
GPIO.setup(36,GPIO.IN)
GPIO.setup(37,GPIO.IN)
GPIO.setup(38,GPIO.IN)

GPIO.output(16,False)

for x in range(1, 10):
    GPIO.output(16,False)
    time.sleep(0.25)
    GPIO.output(16,True)
    time.sleep(0.25)

import pygame                           #Importing Pygame
pygame.mixer.init(44100,-16,8,2048)      #initialising the mixer

Drum1 = ('/home/pi/Drum1.wav')         #Defining Sounds
Drum2 = ('/home/pi/Drum2.wav')
Drum3 = ('/home/pi/DrumSp.wav')
BassDrum = ('/home/pi/BassDrum.wav')
Cym1 = ('/home/pi/Cymbal1.wav')
Cym2 = ('/home/pi/Cymbal2.wav')
Cym3 = ('/home/pi/Cymbal3.wav')
Cym4 = ('/home/pi/Cymbal4.wav')

ch1 = pygame.mixer.Channel(0)        #Making Channel Objects for playback of Multiple Sounds
ch2 = pygame.mixer.Channel(1)
ch3 = pygame.mixer.Channel(2)
ch4 = pygame.mixer.Channel(3)
ch5 = pygame.mixer.Channel(4)
ch6 = pygame.mixer.Channel(5)
ch7 = pygame.mixer.Channel(6)
ch8 = pygame.mixer.Channel(7)

print "Please Start" #LOL

try:
    while True:      #The outermost loop
        while True:  #The inner loop
            if GPIO.input(29) ==1:      #Checks to see if GPIO pin level is high
                ch1.play(pygame.mixer.Sound(Drum1))     #If GPIO level is HIGH or 1, Plays a Sound ON channel 1
                time.sleep(0.02)      #Sleeps for 0.02 Seconds
                break      #The breaks the loop and starts again to avoid multiple Sounds playing at Once

            if GPIO.input(31) ==1:
                ch2.play(pygame.mixer.Sound(Drum2))
                time.sleep(0.05)
                break

            if GPIO.input(32) ==1:
                ch3.play(pygame.mixer.Sound(Drum3))
                time.sleep(0.05)
                break

            if GPIO.input(33) ==1:
                ch4.play(pygame.mixer.Sound(BassDrum))
                time.sleep(0.02)
                break

            if GPIO.input(35) ==1:
                ch5.play(pygame.mixer.Sound(Cym1))
                time.sleep(0.02)
                break

            if GPIO.input(36) ==1:
                ch6.play(pygame.mixer.Sound(Cym2))
                time.sleep(0.02)
                break

            if GPIO.input(37) ==1:
                ch7.play(pygame.mixer.Sound(Cym3))
                time.sleep(0.02)
                break

            if GPIO.input(38) ==1:
                ch8.play(pygame.mixer.Sound(Cym4))
                time.sleep(0.02)
                break

            if GPIO.input(3) ==1:
                GPIO.output(16,False) #X in range wasn't working here, so I used the manual technique
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.output(16,False)
                time.sleep(0.25)
                GPIO.output(16,True)
                time.sleep(0.25)
                GPIO.cleanup()
                os.system ('sudo shutdown now')
                break
finally:       #At the Last
    GPIO.cleanup()#Cleanup GPIO pins to avoid confusion
I am using individual buttons for pins 29, etc
Pins 16 and 3 are input for button to shutdown and output for an led.
Thank you
Last edited by Shaurya on Tue Jun 11, 2019 1:19 pm, edited 1 time in total.

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:13 pm

Try this...

May not be exactly what you want but should give you some ideas.

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

import pygame
pygame.mixer.init(44100,-16,8,2048)
ch1 = pygame.mixer.Channel(0)

Drums = ['Drum1','Drum2','DrumSp','BassDrum','Cymbal1','Cymbal2','Cymbal3','Cymbal4']

x = 0
try:
    while True:
        if GPIO.input(29) == 1:
            Drum = '/home/pi/' + Drums[x] +'.wav'
            ch1.play(pygame.mixer.Sound(Drum))
            x +=1
            if x > 7:
                x = 0
            time.sleep(0.25) # switch debounce
finally:
    GPIO.cleanup()

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:34 pm

No, I can't actually understand your code.
I am a 13 yr old boy trying to learn electronics.
Please help me with that. Although I think read something about changing some values collectively somewhere, I am still confused...

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to change any value in python with an input

Tue Jun 11, 2019 1:48 pm

Code: Select all

import time # import time function
import RPi.GPIO as GPIO # import gpio function
GPIO.setmode(GPIO.BOARD) # set gpio to use BOARD numbers
GPIO.setup(29,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # setup pin 29

import pygame # import pygame
pygame.mixer.init(44100,-16,8,2048)
ch1 = pygame.mixer.Channel(0)

Drums = ['Drum1','Drum2','DrumSp','BassDrum','Cymbal1','Cymbal2','Cymbal3','Cymbal4'] # define list of the drum wavs that can be played

x = 0 # set counter to 0, initially use the first drum wav, Drum1,  note the first is 0 not 1
try:
    while True: # run forever
        if GPIO.input(29) == 1: # check the input switch , 1 = pressed
            Drum = '/home/pi/' + Drums[x] +'.wav' # build the file name string to be played, Drums[x] selects the name from the Drums list, eg Drums[1] = 'Drum2'
            ch1.play(pygame.mixer.Sound(Drum)) # play the required sound
            x +=1 # increment counter 'x'  to point at next wav file
            if x > 7: # there are 8 wavs available, in the list above, so if counter is greater than 7 (remember first is 0) then start at 0 again
                x = 0
            time.sleep(0.25) # switch debounce, switches are 'noisy' and you will get multiple triggers so wait a while for these to stop
finally:
    GPIO.cleanup()
Last edited by gordon77 on Wed Jun 12, 2019 9:09 am, edited 4 times in total.

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Tue Jun 11, 2019 2:01 pm

Thank you...
Now I understand...

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Wed Jun 12, 2019 6:04 am

But then how will I change the value of x with inputs???????

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to change any value in python with an input

Wed Jun 12, 2019 7:22 am

Shaurya wrote:
Wed Jun 12, 2019 6:04 am
But then how will I change the value of x with inputs???????
If you look at gordon77's code example, the value of x is increased every time the GPIO.Input(29) is true. This is done by the line "x += 1" which means add 1 to the current value of x.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
rpiMike
Posts: 980
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: How to change any value in python with an input

Wed Jun 12, 2019 8:46 am

Shaurya wrote:
Wed Jun 12, 2019 6:04 am
But then how will I change the value of x with inputs???????

Code: Select all

if GPIO.input(n) == 1:
	x += 1
or regarding your original post

Code: Select all

if GPIO.input(n) == 1:
	Audio1 = '/home/pi/AnotherSound.wav'

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: How to change any value in python with an input

Thu Jun 13, 2019 4:59 am

Thank you Scotty101 and rpimike, just one last question... what does this line do?

Code: Select all

Drums = ['Drum1','Drum2','DrumSp','BassDrum','Cymbal1','Cymbal2','Cymbal3','Cymbal4']
How can we define so many sounds to a single object?

User avatar
rpdom
Posts: 15580
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to change any value in python with an input

Thu Jun 13, 2019 5:28 am

Shaurya wrote:
Thu Jun 13, 2019 4:59 am
Thank you Scotty101 and rpimike, just one last question... what does this line do?

Code: Select all

Drums = ['Drum1','Drum2','DrumSp','BassDrum','Cymbal1','Cymbal2','Cymbal3','Cymbal4']
How can we define so many sounds to a single object?
The object is a list. See https://www.w3schools.com/python/python_lists.asp

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to change any value in python with an input

Thu Jun 13, 2019 7:43 am

Shaurya wrote:
Thu Jun 13, 2019 4:59 am
Thank you Scotty101 and rpimike, just one last question... what does this line do?

Code: Select all

Drums = ['Drum1','Drum2','DrumSp','BassDrum','Cymbal1','Cymbal2','Cymbal3','Cymbal4']
How can we define so many sounds to a single object?
This is a ' list' where we can define various elements which we can later refer to. To refer to them we use Drums[x] where x is the location in the list, remember the first entry is 0, so Drums[0] is 'Drum1', Drums[1] is 'Drum2' etc

Return to “Python”