Hardware Reference
In-Depth Information
Speakers do not have a polarity; you can connect them in either direction.
After wiring your speaker successfully, you're ready to make music!
MakingSoundSequences
To play back some songs, you first learn about using arrays to store multiple
values easily. You then implement a simple loop to iterate through the arrays
of notes and play them back on the speaker.
Using Arrays
An array is a sequence of values that are related in some way. By grouping them
together, it is an ideal format to iterate through. You can think of an array as a
numbered list. Each position has an index that indicates its location in the list,
and each index has a value that you want to store. You use an array here to store
the list of notes that you want to play, in the order that you want to play them.
To ensure that the Arduino's memory is properly managed, you need to
declare arrays with a known length. You can do this either by explicitly specify-
ing the number of items or by simply populating the array with all the values
you are interested in. For example, if you want to make an array that contains
four integer values, you could create it like this:
int numbers[4];
You can optionally initialize the values when you declare it. If you initialize
the values, specifying the length in the brackets is optional. If unspecified, the
length is assumed to equal the number elements that you initialized:
//Both of these are acceptable
int numbers[4] = {-7, 0, 6, 234};
int numbers[] = {-7, 0, 6, 234};
Note that arrays are zero indexed. In other words, the first number is at
position 0 , the second is at position 1 , and so forth. You can access the elements
in an array at any given index by putting the index of the relevant value in a
square bracket after the variable name. If you want to set the brightness of an
LED connected to pin 9 to the third entry in an array, for example, you can do
so like this:
analogWrite(9,numbers[2]);
Search WWH ::




Custom Search