H00PD0GG
Posts: 6
Joined: Wed Apr 10, 2013 1:00 pm

Help needed with programming

Tue Apr 16, 2013 11:14 am

Hello and thankyou for taking the time to look at this topic, sorry but here comes an essay about everything ive done and want to do.
I am struggling to understand and program with python to control my project. I have a Raspberry Pi Rev 2. I also have a board i have designed and made consisting of a MPR121 Capacitive touch sensor and a MCP23017 https://www.dropbox.com/s/0d399qhg5l6o7 ... %20HDR.jpgImage
I am a total noob to linux and programming and cannot seem to understand how i can get what i want (not for lack of trying).
Ive managed to get everything working but to write a code for exactly what i want ???? Im lost
I can understand some basic functions like print(" "), if x >=1 , if x is equal to or greater than 1 then do.... else do....but how to implicate this into controlling the things i want is making my head feel like its about to pop.
My goal is to have a project where i can put an electrode from the capacitive touch sensor onto say a tree which when touched triggers the sound of say an owl, ive looked at the beetbox and sampled the code from this, this sort of worked, i can see 'pin x was just touched' ect but the audio, this didnt work as i didnt appear to have pygame (however i didnt get any errors from python?), so anyway off i went to get pygame installed, eventually after days of searching on the net and advice from many sources i managed to get sounds i downloaded to play using mpg321-filename.mp3 which i then typed into the code here and there, i then ran into the problem of the nasty clicking/popping which apparently is a 2.5v dc shift which is a known bug on the Pi's, so then i have tried to use the hdmi sound output, somehow i managed to get it working once very briefly, but no more, i really do not know how i got it to work or why.
But wait, that's not all i would like to do, i would also like to trigger a relay as the sound is playing. eg if i placed an electrode on say a tree, then when that said tree is touched the audio is triggered as well as my relay, hence the MCP23017. Now i have played with the chip and some sample code from skpang (ledchaser.py) i have leds working and flashing, ive now connected my new board (mcp23017 and the mpr121) to the 8ch 5v relay card. And now have the problem that every channel is on, i believe this is because when the code starts all channels are set as outputs which tells them all to be gnd, this is causing my 5v0 regulator to get very hot. How would i possibly go about telling the mcp23017 to make its GPI0 a/b 0-7 gnd when the mpr121 is triggered, and then clear itself after 0.x seconds?
Again im sorry for coming on here and blah blah blah but could anybody please help me?
I would like to know how i can get my touch sensors to trigger sound(with no pops) and at the same time trigger my relays, Ive searched online loads and cannot find any code for either chip i can break down to see how its working and then compile to make something to suit my application.
I know youve probably looked at this and are saying you've bitten off far more than you can chew but hey, you have to learn somewhere and this is easy compared to some of the future projects i have in mind.
Thankyou in advance
Chris

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Help needed with programming

Tue Apr 16, 2013 5:08 pm

H00PD0GG wrote:I know youve probably looked at this and are saying you've bitten off far more than you can chew but hey, you have to learn somewhere and this is easy compared to some of the future projects i have in mind.
Bitten off more than you can chew is a understatement!
You really need to take it one step at a time and understand what is happening and why, especially when it goes wrong.
Get your capacitance switch working just by printing the output to screen and make sure it works the same every time.
When it does you can work on the function to play your sample and get that working the same every time.
Then you can concentrate on the next thing and get that working the same every time (see a pattern forming here?).

It's tempting to go for it all at once but near impossible to get it right first time. On top of that your code will probably be a nightmare to debug.

Work on it one piece at a time and not only will you get better results but you'll be building up a library of code snippets that you'll be able to use in future projects.

Regards & good luck :D
Dave.
Apple say... Monkey do !!

H00PD0GG
Posts: 6
Joined: Wed Apr 10, 2013 1:00 pm

Re: Help needed with programming

Tue Apr 16, 2013 8:37 pm

Hiya Dave thank you for your reply, I know exactly what you are saying and I have gone back to the start, I know my touch sensors are working well every time channels 1-7 (not sure why the other 5 are not but 7 should be more than enough for now) I am stuck on the audio side of things, I have been playing all day and am not much further than when I started. I have now used ,sudo amixer cset numid=2 to force hdmi output. I have also downloaded some sample wav files to my pi, however I can run the python script and trigger all my touch sensors, every now and then I get a buzz/tone played, sometimes a different tone. I cannot for the life of me figure out how to just make the audio work, as I've said I've searched online lots and just don't know what it could be, this Is basically the beatbox code but with the audio files changed for ones I have downloaded, I've tried changing the buffer and frequency, size, channels but nothing makes the audio play like I want it to.

Code: Select all

import pygame

import RPi.GPIO as GPIO
import mpr121

# Use GPIO Interrupt Pin

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)

# Use mpr121 class for everything else

mpr121.TOU_THRESH = 0x30
mpr121.REL_THRESH = 0x33
mpr121.setup(0x5a)

# User pygame for sounds

pygame.mixer.pre_init(44100, -16, 12, 512)
pygame.init()

kick = pygame.mixer.Sound('wow.wav')
kick.set_volume(.65);
snare = pygame.mixer.Sound('yikes.wav')
snare.set_volume(.65);
openhh = pygame.mixer.Sound('voice.wav')
openhh.set_volume(.65);
closedhh = pygame.mixer.Sound('electric.wav')
closedhh.set_volume(.65);
clap = pygame.mixer.Sound('bark.wav')
clap.set_volume(.65);
cymbal = pygame.mixer.Sound('ding.wav')
cymbal.set_volume(.65);

# Track touches

touches = [0,0,0,0,0,0];

while True:

	if (GPIO.input(7)): # Interupt pin is high
		pass
	else: # Interupt pin is low

		touchData = mpr121.readData(0x5a)

		for i in range(12):
			if (touchData & (1<<i)):

				if (touches[i] == 0):

					print( 'Pin ' + str(i) + ' was just touched')

					if (i == 0):
						kick.play()
					elif (i == 1):
						snare.play()
					elif (i == 2):
						openhh.play()
					elif (i == 3):
						closedhh.play()
					elif (i == 4):
						clap.play()
					elif (i == 5):
						cymbal.play()
					elif (i == 6):
						snare.play()
					elif (i == 7):
						openhh.play()
					elif (i == 8):
						closedhh.play()
					elif (i == 9):
						clap.play()
					elif (i == 10):
						cymbal.play()
					elif (i == 11):
						cymbal.play()
				touches[i] = 1;
			else:
				if (touches[i] == 1):
					print( 'Pin ' + str(i) + ' was just released')
				touches[i] = 0;
The original code refers to, samples/kick.wav among others which I cannot seem to find on my pi, I've used, cd pygame, ls, however I cannot find a samples folder, this also is confusing me as the original beetbox.py code doesn't return any errors stating it cannot find that file? Why is this?
Again thank you for your reply and I hope you or somebody can help.
Chris

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Help needed with programming

Wed Apr 17, 2013 5:27 pm

To be honest I've not used sound much on the Pi but I do know that some monitors/TVs won't play sound unless it's stereo so that could be an issue.
Another problem could be with ALSA. Try running your script as root, if that works you may need to add yourself to the audio group.

Try this for some info.. http://www.debianhelp.co.uk/sound.htm

Let us know how you go.
Dave.
Apple say... Monkey do !!

H00PD0GG
Posts: 6
Joined: Wed Apr 10, 2013 1:00 pm

Re: Help needed with programming

Sat Apr 20, 2013 9:21 pm

Hi, i thought i would quickly give an update just to say ive cracked it, not really sure how i got the sound working but it is, i now have touch sensors that when touched play a sound via the audio jack and also triggers a relay. I still have a loud pop as the python script starts and stops but no popping during the program (if sombody has an idea on how i can stop the pop or even dampen it please let me know.
Here is a short video https://www.dropbox.com/s/vcod1dcropz84 ... .53.54.mov I had a problem with all of the relay outputs turning on when the code was started (when the GPIO's are set to be outputs and made gnd, then when the output is triggered the GPIO outputs +5 so turns off the relay, to correct this i have used an array of transistors to switch the ground with the +5v signal, in the video you can see the transistors on a small board ive quickly knocked up which i think i can replace with a darlington array. Thankyou again Dave for your help, now going to make a box and then its time for the next pi project :-)

Return to “Python”