-
Notifications
You must be signed in to change notification settings - Fork 0
Piezo
The Piezo
class constructs objects that represent a single piezo component attached to the physical board.
-
pin A Number or String address for the Piezo (+) pin (digital):
var piezo = new five.Piezo(3);
-
options An object of property parameters.
Property Type Value/Description Default Required pin Number Digital Pin. yes
Property Name | Description | Read Only |
---|---|---|
board |
A reference to the board object the Led is attached to | No |
id |
A user definable id value. Defaults to null | No |
pin |
The pin address that the Led is attached to | No |
mode |
Mode the piezo's pin is set to: output (1). | Yes |
isPlaying |
Boolean: is the piezo currently playing? | Yes |
new five.Piezo({
pin: 3
});
var piezo = new five.Piezo({
controller: "I2C_BACKPACK",
pin: 3
});
Firmware: https://github.com/rwaldron/johnny-five/blob/master/firmwares/piezo_i2c_backpack.ino
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
// Create a standard `piezo` instance on pin 3
var piezo = new five.Piezo(3);
// 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
});
});
-
frequency(frequency, duration) Play tone at
frequency
(in Hz) forduration
milliseconds.piezo.frequency(587, 1000); // Play note d5 for 1 second
-
play(tune, callback) Play a
tune
with an optional callback to invoke when the tune is done playing. Thetune
object contains asong
(an array of the notes in thetune
) and optional settings (e.g.tempo
):piezo.play({ tempo: 150, // Beats per minute, default 150 song: [ // An array of notes that comprise the tune [ "c4", 1 ], // Each element is an array in which // [0] is the note to play and //[1] is the duration in "beats" (tempo, above) [ "e4", 2 ], [ "g4", 3 ], [ null, 4 ] // null indicates "no tone" for the beats indicated ] });
The notes you can use are defined in
lib/piezo.js
asPiezo.Notes
:Piezo.Notes = { "c4": 262, "c#4": 277, "d4": 294, "d#4": 311, "e4": 330, "f4": 349, "f#4": 370, "g4": 392, "g#4": 415, "a4": 440, "a#4": 466, "b4": 494, "c5": 523, "c#5": 554, "d5": 587, "d#5": 622, "e5": 659, "f5": 698, "f#5": 740, "g5": 784, "g#5": 831, "a5": 880, "a#5": 932, "b5": 988, "c6": 1047 };
You can also use frequencies directly if you'd like:
piezo.play({ song: [ [ 698, 1 ], // Play frequency 698 for 1 beat [ 831, 2 ] // ... ] });
-
tone(frequency, duration) Play a
tone
forduration
ms. Thetone
value in this case is a computed duty cycle (in microseconds). This is a lower-level method thanfrequency
(which does the translation from frequency to tone for you). Most of the time you likely want to usefrequency
. -
noTone Stop tone playing from Piezo. Will immediately stop playing a tone (
digitalWrite
the output pin low) and clear any existing queued tone timers. -
off Alias of
noTone
Piezo objects are output only and therefore do not emit any events.