Hardware Reference
In-Depth Information
If you want to make your own music, make sure that the arrays remain of an
equal length and that you change the upper bound on the for() loop. Because
the tone() function can run in the background, it's important to use the delay()
function. By delaying the code for an amount of time equal to the duration of the
note, you ensure that the Arduino doesn't play the next note until the previous
not has finished playing for the time you specified.
UnderstandingtheLimitationsofthetone()Function
The tone() function does have a few limitations to be aware of. Like the servo
l i b r a r y, tone() relies on a hardware timer that is also used by the board's pulse-
width modulation (PWM) functionality. If you use tone() , PWM does not work
right on pins 3 and 11 (on boards other than the Mega).
Also remember that the Arduino I/O pins are not digital-to-analog convert-
ers (DACs). Hence, they output only a square wave at the provided frequency,
not a sine wave. Although this suffices for making tones with a speaker, you'll
find it undesirable for playing back music. If you want to play back wave files,
your options include using a music-playing shield (such as the adafruit Wave
Shield or the SparkFun MP3 shield), implementing a DAC converter, or using the
built-in DAC available on the Arduino Due using the Due-only Audio library.
The last limitation is that you can use the tone() function on only one pin
at a time, so it isn't ideal for driving multiple speakers. If you want to drive
multiple speakers at the same time from a standard Arduino, you have to use
manual timer interrupt control, something you learn more about in Chapter 12,
“Hardware and Timer Interrupts.”
NOTE ToreadatutorialonadvancedmultispeakercontrolwithanArduino,visit
www.jeremyblum.com/2010/09/05/driving-5-speakers-simultaneously-
with-an-arduino/ .YoucanalsofindthistutorialontheWileywebsiteshownat
thebeginningofthischapter.
BuildingaMicroPiano
Playing back sequences of notes is great for adding audio feedback to projects
you've already created. For example, consider replacing or augmenting a green
confirmation LED with a confirmation sound. But, what if you want to dynami-
cally control the sound? To wrap up this chapter, you build a simple pentatonic
piano. The pentatonic scale consists of just five notes per octave rather than the
usual seven. Interestingly, the notes of a pentatonic scale have minimal disso-
nance between pitches, meaning they always sound good together. So, it makes
a lot of sense to use pentatonic notes to make a simple piano.
 
Search WWH ::




Custom Search