Go Down

Topic: 8-bit gun sound effects without extra hardware? (Read 3223 times) previous topic - next topic

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.

Grumpy_Mike

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.

cr0sh


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...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

sciguy

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.
arduino sequencer

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.
Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

#4
Jul 05, 2011, 04:47 pm Last Edit: Jul 05, 2011, 04:52 pm by polishvendetta Reason: 1
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 :)

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.

Cylindric

Current Projects: Rodentometer - Hamster Endurance Moni

sciguy

#6
Jul 05, 2011, 05:03 pm Last Edit: Jul 05, 2011, 05:05 pm by sciguy Reason: 1
I haven't tested this, but this should make a half second long burst of noise on pin 3.
Code: [Select]

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!
Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

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?

Grumpy_Mike

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.

sciguy

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.
Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

so the latest updates,

I took Sciguy's code and made it a little more generic for my purposes.
Code: [Select]
//================================================================================//
// 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.

sciguy

Cool, there's one reason why you always try the simpler solution, in case it works.
Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o

Go Up