Background Information
By precisely modulating a pin, the Digital Sandbox can create electronic waves which, when routed through a loudspeaker, can produce a musical tone. We can program the Sandbox to control two characteristics of musical tone: pitch and duration.
A tone’s pitch is what we perceive when we think of a note as being very high (screams, forks scratching plates, etc.) versus very low (like earth-rumbling bass). The pitch of a tone is very closely related to the frequency played through a speaker. If we toggle a pin from HIGH-to-LOW then LOW-to-HIGH 440 times per second, for example, it produces a 440 Hz (hertz) frequency - a “middle A” pitch. Humans can hear frequencies ranging from 20 (low-pitch, bass) to 20,000 Hz (high-pitch, “ow, my ears”).
We can also program the duration of a tone - the length of time a pitch is played. In our program we’ll use the delay function to set the duration. Playing a tone with the Sandbox is very easy. Just give it a pitch and it’ll start toggling the output pin for you. Much like analog output, you can set it and forget it; the tone won’t stop playing until you tell it to.
Hardware
The joystick should be connected as before. The buzzer is connected as follows:
Code Components
This program introduces the Tone and No Tone block.
- Tone: This block takes two inputs: a pin number and a frequency. The pin number can be any digital pin, but in this case we’ll use 10. Frequency can be anything from 31 Hz to the extent of your audible range.
A tone initiated by the Tone block will go on and on until you call a No Tone block. No Tone simply halts a pin from playing a tone. Both of these blocks can be found under the Utilities bin. - Map: This handy function maps a value from one range to another. In the example above, we’re taking a value (the output from the joystick) between zero and 60, and mapping that to a range from 440 to 880. So an analog reading of zero becomes 440, and a reading of 60 becomes 880, anything in between is relative to those two ranges. The map function can be especially useful in mapping an analog input (0-1023) to an output (0-255).
Further Explorations
If you have some sheet music, you can convert it to individual notes, made of frequencies and durations using the following table:
No comments:
Post a Comment