Line | |
---|
1 | /*
|
---|
2 | Esplora Music
|
---|
3 |
|
---|
4 | This sketch turns the Esplora in a simple musical instrument.
|
---|
5 | Press the Switch 1 and move the slider to see how it works.
|
---|
6 |
|
---|
7 | Created on 22 november 2012
|
---|
8 | By Enrico Gueli <enrico.gueli@gmail.com>
|
---|
9 | modified 22 Dec 2012
|
---|
10 | by Tom Igoe
|
---|
11 | */
|
---|
12 |
|
---|
13 |
|
---|
14 | #include <Esplora.h>
|
---|
15 |
|
---|
16 | // these are the frequencies for the notes from middle C
|
---|
17 | // to one octave above middle C:
|
---|
18 | const int note[] = {
|
---|
19 | 262, // C
|
---|
20 | 277, // C#
|
---|
21 | 294, // D
|
---|
22 | 311, // D#
|
---|
23 | 330, // E
|
---|
24 | 349, // F
|
---|
25 | 370, // F#
|
---|
26 | 392, // G
|
---|
27 | 415, // G#
|
---|
28 | 440, // A
|
---|
29 | 466, // A#
|
---|
30 | 494, // B
|
---|
31 | 523 // C next octave
|
---|
32 | };
|
---|
33 |
|
---|
34 | void setup() {
|
---|
35 | }
|
---|
36 |
|
---|
37 | void loop() {
|
---|
38 | // read the button labeled SWITCH_DOWN. If it's low,
|
---|
39 | // then play a note:
|
---|
40 | if (Esplora.readButton(SWITCH_DOWN) == LOW) {
|
---|
41 | int slider = Esplora.readSlider();
|
---|
42 |
|
---|
43 | // use map() to map the slider's range to the
|
---|
44 | // range of notes you have:
|
---|
45 | byte thisNote = map(slider, 0, 1023, 0, 13);
|
---|
46 | // play the note corresponding to the slider's position:
|
---|
47 | Esplora.tone(note[thisNote]);
|
---|
48 | }
|
---|
49 | else {
|
---|
50 | // if the button isn't pressed, turn the note off:
|
---|
51 | Esplora.noTone();
|
---|
52 | }
|
---|
53 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.