Hardware Reference
In-Depth Information
the SPI library is standard and included with the Arduino IDe. Go to the root directory, and then
arduino/avr/libraries ; also remember to fix the reference to Wprogram.h to point to Arduino.h within GD.cpp .
Note
Making Sounds
The Gameduino has the capability to produce stereo sounds via the audio plug. The voice() function in the
Gameduino library can play two different types of wave functions: a sine wave and noise. The frequency range
is about 10 to 8,000 Hz via a 12-bit synthesizer. The Gameduino is capable of 64 different voices that combine to
create the output. The total amplitude of all the playing voices' output is a maximum value of 255 per channel—to
avoid clipping, keep the total amplitude under 255. The frequency argument of the voices is in quarter-Hertz—for
example, a frequency of 880 Hz (an A note) would require an input of 3,520. By adding the sine waves together
simulated square and sawtooth waves can be created to better mimic the sound of old game systems. The noise wave
in conjunction with sine waves can create sound effects for rockets, car engines, and even fighting games. Once the
Gameduino is told to start making a sound, it will continue till told to change. The sound needs time to be heard by
the listener, so there will have to be time delays in the code. This can slow down other aspects in the game. Note that
changes should happen between running of loops, or in more advanced cases, run in the Gameduino's secondary
processor. Sound is a great way to give the player feedback on what is going on (e.g., for losing or completing a level,
or to produce a sense of urgency at certain parts of the game).
Adding the first sound effect to Stack it provides an auditory signal when the button has been pressed, add the
following code line to the beginning of buttonInterrupt() before the loop is entered to have the game make a sound
when the button is pressed.
GD.voice(0, 0, 5000,254,254);
A sound of 1,250 Hz (approximately an E-flat) will start playing from both channels. To get the sound to turn off,
add a corresponding call at the end of the buttonInterrupt() that would appear just before the buttonInterrupt()
function returns:
GD.voice(0,0, 0,0,0);
Listing 11-4 describes three functions that produce more complicated sounds to inform the player of a loss,
a big win, and that the game is currently being played.
The first sound function, moveTone() , plays three notes: 500 Hz (~B), 750 Hz (~F sharp), and 1,000 Hz
(~B + 1 octave). The note timings are based on the delay of the main loop. moveTone() generates sound that increases
in tempo along with the increase in sweep speed of the sprite. The increase in the tempo as the game approaches
the final level provides the feeling of greater urgency. moveTone() needs two global variables that are used to count
the steps between note changes and to allow other functions to turn the move tone on and off. The variables are an
integer and a Boolean declared at the beginning of the code, just after the include section.
int movetonecount = 0;
boolean moveToneflag = true;
Listing 11-4 is split into three parts. The moveTone() , WinTone() , and LossTone() functions are added to the end
of the main sketch after the end of the loop() function. The call to moveTone() is at the end of the loop() function just
before loop() 's ending curly bracket.
 
 
Search WWH ::




Custom Search