Astro-Text

Astro-Text is a game that I made on using an Atmegea328p microcontroller, a 16×2 character liquid crystal display and a speaker. Before getting into a detailed explanation of how the game was constructed, It would be helpful to have an understanding of what the game looks like. So, here is a short demo video of Astro-Text (38 seconds):

The Arduino UNO R3 was used as the development board used for this project. The Atmege328p onboard the UNO was programmed in C using Atmel’s AVR library. There project has many complex components, however, I will be focusing on how the projects audio was implemented.

The audio portion of Astro-Text was the most difficult and time-consuming portion of the game. It is also responsible for at least half of the user experience. Without sound the game would seem very dull. Try watching the demo video again without sound and you will see.

To make the game play sound, the voltage level of a single output pin on the microcontroller is varied 20,000 times a second. Since the output pins are digital, they can only be set high or low. To get around this, the game uses pulse width modulation or PWM for short. PWM is the act of varying the duty cycle or pulse width of a square wave to achieve an average value that is somewhere between the high and low output levels. By then passing the output pin through a low-pass filter, the square-wave signal is smoothed out leaving behind the underlying audio signal.

Getting the system to play audio simultaneously while displaying graphics was by far the most challenging aspect of the project. To make things even more difficult, the sound being played was sampled audio, which ran at 20 kHz. The difficulty was that if too much time elapsed between audio samples there would be a noticeable pause in the sound. It would sound like a record that was skipping. To avoid this, a timer interrupt system was used to set the audio output. 20,000 times a second, the microprocessor stops what its doing, changes the audio output, and then goes back to what it was doing. Each time it changes the audio, only an infinitesimal amount of time passes, so it is imperceptible to the user.