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

Changing values in a defined sequence

Fri Jun 21, 2019 9:15 am

Hello!
Is there any code that can help me with this?
I want to change an audio file whenever a button is pressed in a defined order. The sound files are SOUND1, SOUND2, SOUND3, etc...
I want the sounds to change this way-
When I press the button once, the sound changes to SOUND1
When I press the button again, it changes to SOUND2
When I press the button once more, it changes to SOUND3

Any help would be really appreciated...

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

Re: Changing values in a defined sequence

Fri Jun 21, 2019 9:30 am

You could use some thing as simple as this.

Code: Select all


import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
count = 0
while True:
	
	while GPIO.input(7) == 1:
		time.sleep(0.5)
        
    count = count + 1
    print (count)
    
    # here you could have your sound playing code
    #example
    # file = "home/pi/sound" + str(count) + ".wav"
    # os("aplay file")
    
 	while GPIO.input(7) == 0:
		time.sleep(0.5)   
		
un-tested code so expect errors

there are probably lots more ways to do it , it just depends on how complex you want to make it.
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: Changing values in a defined sequence

Wed Jun 26, 2019 1:34 pm

Hello & sorry for the late reply...
I looked at your code and I think it's fantastic...

Just a few clarifications...
According to your code, it keeps on adding 1 to "count" this would be correct for three times when there are only three sounds, until it goes up to 4, and gives an error about that sound not existing.

I would want this to go back to SOUND1 after SOUND3....

I am creating a drum kit with my pi and as you know, an electronic drum kit contains a set of sounds, known as patches.

So when you select 1, patch 1 will open having its own sounds for each of the 8 trigger pads...

Initially, I wanted to use buttons as patch buttons but then figured out that I would need an unknown number of patches for the future. So i decided to use +/- buttons... (Of course 2 buttons ate cleaner than 10 or more...)

That is why I would like to change 8 sounds collectively. Also isn't there anything that can help me with the sequence? I am also going to use an oled display which is going to display patch numbers...

What I want to do is-
>When I start up the pi, it would want it to be on patch 1...

>When button "+" is pressed, I want it to change to patch 2 and change the sounds. If the "+" button is pressed again, the patch should change to 3 and the sounds should change also...

>At any given point, if the "-" button is pressed, the patch should reduce by 1 and the sounds should also change. For example...
If I am at patch 12, and press the " - " button, the patch should change to 11...

(I am 12 years old so this may take some time to get into me....😅 )

Thank you!

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

Re: Changing values in a defined sequence

Wed Jun 26, 2019 2:14 pm

So I changed my code so now it will allow you to cycle through the 3 sound options.

Code: Select all


import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
count = 0
while True:
	
	while GPIO.input(7) == 1:
		time.sleep(0.5)
        
    count = count + 1
    if count == 4:
        count = 3
    print (count)
    
    # here you could have your sound playing code
    #example
    # file = "home/pi/sound" + str(count) + ".wav"
    # os("aplay file")
    
 	while GPIO.input(7) == 0:
		time.sleep(0.5)  
		
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Changing values in a defined sequence

Wed Jun 26, 2019 2:46 pm

So I have read through your post several times and I think this is what you wanted to do .

2 buttons , one to move up a list of patches , one move down a list of patches , which ever way you move you just cycle through the list.

in this example you have a list of 8

Code: Select all


import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)

count = 1

GPIO.add_event_detect(7, GPIO.FALLING, bouncetime=200)  
GPIO.add_event_detect(8, GPIO.FALLING, bouncetime=200)  



while True:
	
    if GPIO.event_detected(7):
        print('+ Button pressed')
        count = count + 1
        if count == 9:
            count = 1

    if GPIO.event_detected(8):
        print('- Button pressed')
        count = count - 1
        if count == 0:
            count = 8
    
    
        
    print (count)
    
    # here you could have your code to change the patches
    
    time.sleep(1)
    
    

tested using python 3

I suggest you have a read of this web site to get an explanation of how the gpio inputs are done.

https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/

again this is just one way of doing it.
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: Changing values in a defined sequence

Wed Jun 26, 2019 4:29 pm

Thank you...
I understand most of your code, but the part where the sounds are being changed are not clear...
After print (count), You mean I need to something like...

Code: Select all

If count == 1:
Drum1 = the path to my sound file?
Also, Will I be able to change the NUMBER of patches by editing these lines?

Code: Select all

if GPIO.event_detected(7):
        print('+ Button pressed')
        count = count + 1
        if count == 9:
            count = 1

    if GPIO.event_detected(8):
        print('- Button pressed')
        count = count - 1
        if count == 0:
            count = 8
Thank you...

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

Re: Changing values in a defined sequence

Wed Jun 26, 2019 7:01 pm

So to answer your second question yes those parts of the code control the number of patches.

You could have worked that out by changing the numbers and trying it for yourself, you are going to have to get used to trying different things when learning to code, so starting to experiment and trying things for yourself now would be a good idea.

So to answer your first question yes you would need to add what ever code you choose to select which drum sounds are used for each selected patch.

So I have add some more code to my example

Code: Select all


import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(8, GPIO.IN, pull_up_down=GPIO.PUD_UP)

nopatch = 4
count = 1

GPIO.add_event_detect(7, GPIO.FALLING, bouncetime=200)  
GPIO.add_event_detect(8, GPIO.FALLING, bouncetime=200)  

patchmatrix = [("drum 1","drum 2","drum 3","drum 4","drum 5","drum 6"),
              ("drum 7","drum 8","drum 9","drum 10","drum 11","drum 12"),
              ("drum 13","drum 14","drum 15","drum 16","drum 16","drum 17"),
              ("drum 18","drum 19","drum 20","drum 21","drum 22","drum 23")]

while True:
	
    if GPIO.event_detected(7):
        #print('+ Button pressed')
        count = count + 1
        if count == (nopatch + 1):
            count = 1

    if GPIO.event_detected(8):
        #print('- Button pressed')
        count = count - 1
        if count == 0:
            count = nopatch
    
    
        
    print ("patch number ",count)
    pcount = count - 1
    for item in range(0, 6):
        print (patchmatrix[pcount][item])
    
    
    
    time.sleep(1)
    
So now we have just 4 patches each with 6 drum sounds, these drum sounds could be used to select which files are being used for your drums,

now I am not going to tell you how this works you will learn more if you work it out for yourself, try adding another patch that contains the next 6 drum numbers.

you may find these sites helpful when learning python.

https://www.programiz.com/python-programming/tutorial
https://docs.python.org/3/tutorial/
https://www.quackit.com/python/tutorial/

if you get stuck post your code and I will provide guidance, but don't forget to use code tags when posting code.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”