Last active
August 9, 2017 19:54
-
-
Save Redmega/c9a2f12367e990c6099dd95a6792a686 to your computer and use it in GitHub Desktop.
Johnny Five app
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
const five = require('johnny-five'); | |
var board = new five.Board(); | |
// The board's pins will not be accessible until | |
// the board has reported that it is ready | |
const LIGHTS = [10, 11, 12, 13]; | |
const BUTTONS = [5, 4, 3, 2]; | |
const NOTES = ['C', 'E', 'G', 'B']; | |
const PIEZO = 6; | |
let holdtime = 250; | |
board.on('ready', function() { | |
console.log('Ready!'); | |
const buttons = initButtons(); | |
const speaker = new five.Piezo(PIEZO); | |
// const potentiometer = new five.Sensor({ | |
// pin: 'A5', | |
// freq: 1000, | |
// }); | |
setupButtons(buttons, speaker); | |
// setupPotentiometer(potentiometer, buttons); | |
const deathButton = new five.Button(7); | |
setupDeathButton(deathButton, speaker); | |
speaker.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, | |
}); | |
this.repl.inject({ | |
buttons: buttons, | |
}); | |
}); | |
function initButtons() { | |
return BUTTONS.map( | |
(pin, i) => | |
new five.Button({ | |
pin: pin, | |
holdtime: holdtime, | |
custom: { | |
light: new five.Led(LIGHTS[i]), | |
}, | |
}), | |
); | |
} | |
function setupButtons(buttons, speaker) { | |
buttons.map((button, i) => { | |
button.on('down', function() { | |
button.custom.light.on(); | |
speaker.play({ song: NOTES[i], beats: 1 }); | |
console.log(NOTES[i], 'full beat'); | |
}); | |
button.on('hold', function() { | |
speaker.play({ song: NOTES[i], beats: 1 / 8 }); | |
console.log(NOTES[i], 'eighth beat'); | |
}); | |
button.on('up', function() { | |
button.custom.light.off(); | |
}); | |
}); | |
} | |
function setupDeathButton(button, speaker) { | |
button.on('down', function() { | |
speaker.play({ | |
song: [ | |
['E', 1], | |
['', 1 / 4], | |
['E', 1], | |
['', 1 / 4], | |
['E', 1], | |
['', 1 / 4], | |
['C', 3], | |
['D', 1], | |
['', 1 / 4], | |
['D', 1], | |
['', 1 / 4], | |
['D', 1], | |
['', 1 / 4], | |
['B3', 3], | |
], | |
}); | |
console.log('Playing death sound'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment