-
Notifications
You must be signed in to change notification settings - Fork 1
/
piezo.js
51 lines (46 loc) · 1.24 KB
/
piezo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Creates a piezo object and defines the pin to be used for the signal
var piezo = new five.Piezo(9);
// Injects the piezo into the repl
board.repl.inject({
piezo: piezo
});
// Plays a song
piezo.play({
// song is composed by an array of pairs of notes and beats
// The first argument is the note (null means "no note")
// The second argument is the length of time (beat) of the note (or non-note)
song: [
["C4", 1 / 4],
["D4", 1 / 4],
["F4", 1 / 4],
["D4", 1 / 4],
["A4", 1 / 4],
[null, 1 / 4],
["A4", 1],
["G4", 1],
[null, 1 / 2],
["C4", 1 / 4],
["D4", 1 / 4],
["F4", 1 / 4],
["D4", 1 / 4],
["G4", 1 / 4],
[null, 1 / 4],
["G4", 1],
["F4", 1],
[null, 1 / 2]
],
tempo: 100
});
// Plays the same song with a string representation
piezo.play({
// song is composed by a string of notes
// a default beat is set, and the default octave is used
// any invalid note is read as "no note"
song: "C D F D A - A A A A G G G G - - C D F D G - G G G G F F F F - -",
beats: 1 / 4,
tempo: 100
});
});