Pages: [1]   Go Down
Author Topic: 8-bit gun sound effects without extra hardware?  (Read 2838 times)
0 Members and 1 Guest are viewing this topic.
Cleveland, OH
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm completely new to programming in C, Arduino, and electronic circuits in general.

I'm working on a project to make a Laser Tag like game.  I have found lots of resources using the play tone function, however I was wondering if there was something that could make a gun sound effect without using any extra chips or shields.  I don't need anything real sounding but something better then a simple tone or melody would be awesome.  I'm working with a Nano 328 and trying to keep things as small as possible.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Without extra hardware all you get is a square wave. Do you not need hardware to drive your speaker?

However, a few cycles of random square waves sounds like a burst of noise and that is a bit like a shot. So try that.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5611
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Without extra hardware all you get is a square wave. Do you not need hardware to drive your speaker?

However, a few cycles of random square waves sounds like a burst of noise and that is a bit like a shot. So try that.

It sounds even better when you can vary the amplitude, from loud to soft, fairly quickly; I remember doing this on my TRS-80 Color Computer back in the day, which had a 6-bit DAC; there was a way in BASIC to get the PLAY command to output something akin to random noise, and while doing that while decrease the volume rapidly, you could get the effect of shots being fired, and even something like an explosion. I should hope something like this could be done on an Arduino, perhaps with only a modicum of external parts...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Nowhere
Offline Offline
God Member
*****
Karma: 3
Posts: 852
|-\ |\|\
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All the sounds in the following video were produced by the Arduino:
Only 2 square waves, one for the drums, the other for the melody.


So +1 for the square wave idea, including mike's suggestion for noise.  (that was how I made noise on the video)


It was a bit tricky to program, but only because I needed it to be non-blocking.  (I didn't know how to use timer interrupts yet, and I needed to continuously refresh the display.)
If you can use delay() in the sound making (meaning the arduino cannot do anything else during the sound effect), it would be really easy.
Logged

Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

Cleveland, OH
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for the help.  Can anyone put up some sample code to create noise?  Like I said im not very adept at coding in C (im a mainframer my self) but ive gotten pretty good at adapting existing code smiley

Locking up the board while a .5 second or less sound plays shouldnt be a big deal so for now delay() should work.  this would eliminate the need for a delay anyway to prevent someone from firing the tag gun too quickly.

I will likely need some kind of amplifier for the speaker as running straight from the arduino board the sound is very quiet.
« Last Edit: July 05, 2011, 09:52:27 am by polishvendetta » Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 168
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's some stuff in the Playground that might set you in the right direction.
Logged

Current Projects: Rodentometer - Hamster Endurance Moni

Nowhere
Offline Offline
God Member
*****
Karma: 3
Posts: 852
|-\ |\|\
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't tested this, but this should make a half second long burst of noise on pin 3.
Code:
void noise()  {
unsigned long time = millis();
while(millis() - time <= 500)  {  // change "500" for different durations in ms.
tone(3, random(100, 1000);   // change the parameters of random() for different sound
}

I would just copy and paste the code I used, but it would not really make sense to use it outside the sequencer program, it would be a pretty kludgey way to do it.


Ooh, just realized the forum has a ninja'd warning!
« Last Edit: July 05, 2011, 10:05:34 am by sciguy » Logged

Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

Cleveland, OH
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I actually read most of the articles on the playground regarding sound, but nothing talked about noise, which is what im finding most 8-bit games use for guns and explosions.

Thanks for the code, I'll give it a shot later and let you knwo how it works. 

is there anything i can do to control amplitude?  I've been hearing things about digital pots, is that what I would use or is there something in my code I can use?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is there anything i can do to control amplitude?
Not without external hardware. Digital pots are too slow for what you want, a few resistors to make a crude D/A would be best.
Logged

Nowhere
Offline Offline
God Member
*****
Karma: 3
Posts: 852
|-\ |\|\
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simplest hardware-wise solution to give variable amplitude would be to use PWM and a simple lowpass filter.
All you'd need is a resistor and a capacitor.
Unfortunately you'd also need to learn how to use the internal timers.

Probably not worth it for just making gun noises.  But learning to use internal timers for interrupts, etc could be very useful for future projects.

So take mike's suggestion, use a r2r resistor ladder or similar.
Logged

Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

Cleveland, OH
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so the latest updates,

I took Sciguy's code and made it a little more generic for my purposes.
Code:
//================================================================================//
// Noise                                                                          //
// This code generates random tones for DURATION between +300 and -300 of the     //
// FREQency specified.                                                            //
//================================================================================//
void noise(int freq, int duration)  {
int low = freq - 300;
int high = freq + 300;
unsigned long time = millis();
while(millis() - time <= duration)  {
tone(7, random(low, high));
}
noTone(7);
}
//================================================================================//

And this is working perfectly.  As for the amplitude problem, I'm not sure if its going to be an issue.  Once I started playing with this I was able to get the sound to trail off just buy lowering the frequency.  The small speaker I'm using I got from radio shack has a low end of 300hz, so setting the frequency lower then that less and less sound is played.

The speaker isn't loud and will be changed out in the future but for testing its working fine.
Logged

Nowhere
Offline Offline
God Member
*****
Karma: 3
Posts: 852
|-\ |\|\
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool, there's one reason why you always try the simpler solution, in case it works.
Logged

Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

Pages: [1]   Go Up
Jump to: