Creating Arcade Game Sounds on a Microcontroller

5.1K170

Intro: Creating Arcade Game Sounds on a Microcontroller

Hello!

Some of you may have seen the Color Invaders project that I made the other day that used a Space Invaders-like game style on a strip of WS2812 LEDs. But I found that playing the game silently isn't terribly fun, so I decided to look into creating my own arcade game sounds that I could easily play on a small piezoelectric speaker.

In this I'ble, I'll explain the general principle behind creating sound in general on a piezoelectric speaker as well as creating some simple sounds like charging up and firing a missile.

STEP 1: Piezoelectric Speakers

A piezoelectric speaker, or at least the one that I am using, is a type of speaker that has a small piezo element inside of it. A piezo element is a type of material that produces a voltage when it is squeezed or bent. Conversely, you can also cause the element to bend itself when you apply voltage across the material.

With this in mind, we can send digital signals in the form of a square wave at a set frequency to cause our material to vibrate at that frequency and produce a sound wave that we can hear.

Using this technique, we can create a musical array (pun intended) of sounds by playing frequencies at known frequencies with a 50% duty cycle.

An example code function (in an "arduino" format) that plays a provided frequency for a set amount of time in this style is provided below:

void playFreq(double freqHz, int durationMs){
//Calculate the period in microseconds
int periodMicro = int((1/freqHz)*1000000);
int halfPeriod = periodMicro/2;

//store start time
int startTime = millis();

//(millis() - startTime) is elapsed play time
while((millis() - startTime) < durationMs){
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(halfPeriod);
} //end of while loop
}

STEP 2: Creating Your Own Sounds

But let's get to the point of this Instructable: creating your own custom sounds. Please note that this all from my own personal experience after an afternoon of experimenting, and is by no means the standard by which other people also create their own sounds.

There are three main things that I personally try to keep aware of when creating my own "arcade" sound:

  1. Keep the sounds short and sweet -- unless you are going to put in some extra work to implement some "non-blocking functions" or not be concerned about it, playing a sound on a microcontroller will effectively pause/stop your game for as long as it is playing. This can rapidly become very obvious (and annoying) if you have a sound for changing directions on a snake game or when you fire a missile.
  2. Change the frequency of the sound in steps -- this helps keep your sounds both short and sweet and more easily shows (well, sounds) like a progression in pitch has occurred. As long as the length of time spent on each pitch isn't too long, it will not sound too choppy, even if you are jumping 30-50 Hz at a time.
  3. Know what it is going to sound like -- I realize that this is both obvious and vague at the same time, but what I'm trying to get at is the idea of knowing what a stereotypical sound is supposed to sound like. If you are creating a sound for when you fire a shot of some sort, it is going to start off higher in pitch (faster frequency) and then quickly drop off in pitch to simulate the shot going farther away from the source. An explosion noise is typically a lot of "white noise" so a random set of frequencies (decided by the user or via a circuit) would suffice to give an appropriate 8-bit video game explosion.

STEP 3: Seeing It in Practice


You can check out the YouTube video of the Color Invaders game that I made with a WS2812 LED strip that uses some of these fun sounds. The code I used to run the game is available in a zip file and a text file at the bottom of this step; it's designed to be used with a chipKIT uC32. Code that just has the sound effects without the game is similarly available in files below this step.

Feel free to ask any questions that you might have in the comments below!