Last active
May 28, 2019 21:04
-
-
Save pfiller/5680471 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require("johnny-five"), | |
board, servo, piezo; | |
board = new five.Board(); | |
board.on("ready", function() { | |
piezo = new five.Piezo(9); | |
board.repl.inject({ | |
piezo: piezo | |
}); | |
piezo.song("cdfda ag cdfdg gf ", "111111442111111442") | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Board = require("../lib/board.js"), | |
events = require("events"), | |
util = require("util"); | |
function Piezo( opts ) { | |
opts = Board.options( opts ); | |
// Hardware instance properties | |
this.board = Board.mount( opts ); | |
this.firmata = this.board.firmata; | |
this.mode = this.firmata.MODES.PWM; | |
this.pin = opts.pin || 3; | |
this.noteObject = { | |
c: 262, | |
d: 294, | |
e: 330, | |
f: 349, | |
g: 392, | |
a: 440, | |
b: 494, | |
C: 523 | |
}; | |
if ( !Board.Pin.isPWM(this.pin) ) { | |
this.emit( "error", this.pin + "is not a valid PWM pin" ); | |
} | |
// Set the pin to INPUT mode | |
this.firmata.pinMode( this.pin, this.mode ); | |
// Piezo instance properties | |
this.interval = null; | |
this.playing = false; | |
this.queue = []; | |
// TODO: Implement a playback stack | |
} | |
util.inherits( Piezo, events.EventEmitter ); | |
Piezo.prototype.tone = function( tone, duration ) { | |
this.firmata.analogWrite( this.pin, tone ); | |
setTimeout(function() { | |
this.firmata.analogWrite( this.pin, 0 ); | |
}.bind(this), duration ); | |
return this; | |
}; | |
Piezo.prototype.song = function( tune, tempo ){ | |
var playTime = 400; | |
var sequenceTime = 0; | |
var self = this; | |
var note, duration; | |
for (var i = 0; i < tune.length; i+=1) { | |
note = tune[i]; | |
duration = tempo[i]; | |
if (duration == '1') { | |
duration = playTime / 4; | |
} | |
else if (duration == '2') { | |
duration = playTime / 2; | |
} | |
else if (duration == '4') { | |
duration = playTime; | |
} | |
if (note == " ") { | |
self.firmata.analogWrite( self.pin, 0 ); | |
sequenceTime = sequenceTime + duration; | |
console.log('break!'); | |
} | |
else { | |
console.log(tune, tempo); | |
myNote = this.noteObject[note]; | |
(function (myNote, sequenceTime) { | |
setTimeout(function () { | |
self.firmata.analogWrite(self.pin, myNote); | |
console.log('start' + myNote + ' - ' + sequenceTime); | |
}, sequenceTime); | |
}(myNote, sequenceTime)); | |
sequenceTime = sequenceTime + duration; | |
(function (myNote, sequenceTime) { | |
setTimeout(function () { | |
self.firmata.analogWrite( self.pin, 0 ); | |
console.log('stop' + myNote + ' - ' + sequenceTime); | |
}, sequenceTime); | |
}(myNote, sequenceTime)); | |
} | |
} | |
}; | |
Piezo.prototype.fade = function( fromVol, toVol ) { | |
// TODO: Add speed control | |
toVol = toVol === 0 ? -1 : toVol; | |
var now = fromVol, | |
step = toVol < fromVol ? -1 : 1; | |
this.interval = setInterval(function() { | |
now = now + step; | |
if ( now !== toVol ) { | |
this.firmata.analogWrite( this.pin, now ); | |
} else { | |
// this.firmata.analogWrite( this.pin, 0 ); | |
clearInterval( this.interval ); | |
} | |
}.bind(this), 50 ); | |
return this; | |
}; | |
Piezo.prototype.noTone = function(){ | |
this.firmata.analogWrite( this.pin, 0 ); | |
} | |
// Piezo.prototype.alarm = function( pattern ) { | |
// }; | |
// Piezo.prototype.note = function( note, duration ) { | |
// var notes = { | |
// "c": 1915, | |
// "d": 1700, | |
// "e": 1519, | |
// "f": 1432, | |
// "g": 1275, | |
// "a": 1136, | |
// "b": 1014, | |
// "C": 956 | |
// }; | |
// return this; | |
// }; | |
module.exports = Piezo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool, thanks for the help, going to play with this some more. going to see if adding accurate timing helps (https://gist.github.com/manast/1185904)
also saw that https://github.com/sparkfun/SIK-Guide-Code/blob/master/Circuit_11/Circuit_11.ino has a 10 millisecond pause after each note