Sounduino

Introduction

What would a video game be without sound? Not a very good one! So we are going to add a piezo to the circuit to generate game sounds and also add some processing code, so that when the Arduino UNO is disconnected, sound will play through your computer speakers.

After some digging on the web, I managed to find the actual frequency and duration for the sounds used in the original Pong game …

Sound Frequency Duration Listen
Ball hits wall 226 Hz 16 ms
Paddle hits ball 459 Hz 96 ms
Point scored 490 Hz 257 ms

… I have implemented these into the Arduino Pronguino Library Speaker class, and also created Waveform Audio files for each of the game sounds (which are also used in the table above) that are played through the Speaker class in the Processing code , these .wav files were generated using an online tone generator, there are quite a few to choose from, and will ask for some/all of the following parameters …

Ball hits wallPaddle hits ballPoint scored
Depth16-bit16-bit16-bit
WaveSquareSquareSquare
Frequency (Hertz)226 Hz459 Hz490 Hz
Duration (Milliseconds)16 ms96 ms257 ms
Sample Rate (Kilohertz)14.1 kHz14.1 kHz14.1 kHz
Amplitude/Volume(experiment)(experiment)(experiment)

Challenge: Find or create more modern versions of these game sounds, maybe tennis / table tennis sounds ?

Getting Started

Recommended

I recommend reading through the Servuino and subsequent posts, as we are building circuit on top of the one built in Servuino.

Components

All the components that you need listed here are in the Arduino Starter Kit, as mentioned above, this is being built on top of previously built circuit, see existing components list, as well as the following …

PartQuantity
Piezo1
5mm orange wire1
20m blue wire1

Note: I have been specific on the length in particular on the above wires, mainly for the smaller ones that are used just on the breadboard, so they are as flat as possible.

The Circuit

Assembly

These instruction steps are used to assemble the circuit as shown above.

StepInstruction
1Ensure you have unplugged from power source and at least one end of USB cable is removed from either the UNO / computer.
2The piezo will be slightly different to that shown in circuit above, the pins will be underneath. Gently place the two pins over sockets, making a note of the row column numbers on board, and then push piezo in.
3Insert one end of the 20mm blue wire into the digital pin D10, and the other end into nearest board socket on one of the rows noted above.
4Insert one end of the 5mm orange wire into a socket on the GND bus line, and the other end into the nearest board socket on the other row noted above.
* It is recommended not to use digital pins 0 and 1, due to them being used by USB serial communication.

The Code

Download

Arduino

With the introduction of the piezo, code as been added to the Component Library, also the concept of a “console”, which has the Speaker and Controllers “attached”, this has been encapsulated in the Pronguino Library GameConsole class.

Component Library
Pronguino Library
Pronguino Sketch
What’s New

There have been no new files added.

What’s Changed
FileDescription
component.hDeclaration of Piezo class added
component.cppImplementation of Piezo class added
Component Library
FileDescription
prongunio.hDeclaration of Speaker class added
Declaration of GameConsole class added
prongunio.cppImplementation of Speaker class added
Implementation of GameConsole class added
Pronguino Library
FileDescription
pronguino.inoAdded logic for GameConsole / Speaker
Added testing for GameConsole / Speaker
Pronguino Sketch
Testing

As previously, if you change TESTING to be true, upload the new code, you will see the same results in the serial monitor as in Servuino Testing, but will now also play sounds depending on the button(s) pressed as follows …

SoundButton(s)
Ball hits wallLeft
Paddle hits ballRight
Point scoredLeft+Right
Troubleshooting

Check the following, as well as items in previous Troubleshooting guide ….

IssueResolution
Nothing is workingCheck Arduino is plugged in securely.
Check red & black wires on board are secure.
Check wires connecting bus lines.
No sound (when buttons pressed in test mode, or when events triggered such as ball hits wall etc)Check piezo is firmly pushed in.
Check the wires to D10 are connected ok.
Check the wires to GND are ok.
Check wires from buttons are ok.
Troubleshooting Guide

Challenge: As I write the code, sometimes I have little ideas, notes to improve or fix things, see if you can tackle any that you see a the top of some of the code files, feel free to comment any questions you may have.

Processing

After some experimentations with trying to create the correct tones / and also .wav playback using the standard Sound Library, I ended up using the Minim library, which simplified the process of .wav playback.

Challenge: See if you can utilise the standard Sound library to either produce the tones or playback .wav files, I tried a few things .. SqrOsc, AudoSample and SoundFile, but couldn’t quite get them to work as I would expect, I may have missed something.

You will need to install the Minim library from the Processing IDE …

Sketch -> Import Library -> Manage Libraries … and search for “minim”

Have added a Speaker interface along with implementations for an ExternalSpeaker (Arduino) and InternalSpeaker (Computer), and also introduced the Console interface, with implementations for ExternalConsole and InternalConsole, to look after the controllers and speaker.

What’s New
FileDescription
data/WallSound.wavWave Audio file (Ball hits wall)
data/PaddleSound.wavWave Audio file (Paddle hits ball)
data/PointSound.wavWave Audio file (Point scored)
ConsoleConsole interface
AbstractConsole implementation of abstract class
ConsoleFactory, determines which type of console to create
ExternalConsole implementation
InternalConsole implementation
SpeakerSpeaker Interface
ExternalSpeaker implementation
InternalSpeaker implementation
What’s Changed
FileDescription
ConstantsAdded data bit masks for sounds
ControllerRemoved connected flag (moved to Console)
PaddleAdded position function to determine how/where to initially position paddle
PlayerUse console to access controller
PronguinoSome Refactoring
Consumes Console class
Adds calls to sound methods in relevant places
Troubleshooting
IssueResolution
The package “ddf” does not exist. You might be missing a library.
No library found for ddf.minim
Make sure minim library has been installed via Sketch -> Import Library -> Manage Libraries …
Troubleshooting Guide

Rules

No functional changes to game, so rules are the same ..

RuleDescription
Game OverThe first player to SCORE_MAX wins the game.
Opening ServePLAYER_ONE has the first service.
Player MissesThe same player serves.
Serve DirectionIf the player serving has paddle in top half of surface, the direction will be down/left or right depending on player.

If the player serving has paddle in bottom half of surface, the direction will be up/left or right depending on player.

Play

Break time … Turn off testing mode, and re-upload …. volume up (if not using UNO) .. no change in controls …

DirectionPlayer 1Player 2
Upqp
Downal
Pause/Resume
Serve
spacespace
Key Controls

Piezo

A piezo sounder contains a piezo electric vibration plate within a moulded case.  Sound is emitted when a voltage is applied and the piezo element inside the case vibrates. I won’t get into the details of how it works, just keeping simple for the context of this project.


Terminal 1 (T_1) connected to GND.

Terminal 2 (T_2) connected to digital pin D10.

The tone() function generates a square wave where the duty cycle is 50% (HIGH half the time, LOW half the time), at a given frequency in cycles per second (Hertz), for a specified duration in milliseconds to the required pin, in this case digital pin D10.

Learn: I find it important to get to know your components, and how they work, it helps when things don’t work as planned, look up the datasheets of the components to get data to be used to equations such as Ohms law to make sure you choose correctly.

Conclusion

A reasonably easy addition to this project. Throughout these posts hopefully covered some of the very basics of a classic retro game, jazzing it up a bit, and building our own “console”. Will probably be referring back to some posts or sections in future projects.