Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 1 | ||||
| × | 1 |
An example dedicated to the Christmas period, which exploits the potential of our system with Node.js.
The script will perform a classic Christmas tune that will interact with the smart wifi lamp Lifx. Each note will correspond to a different color of the lamp.
What we need:
Warning: embedding parts within the project story has been deprecated. To edit, remove or add more parts, go to the "Hardware" tab. To remove this list from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Hardware" tab). | ||||||
![]() |
|
|||||
|
||||||
|
We create a new project in the workspace Ideino. In the file package.json insert addiction module lifx.
package.json:
Code
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
{
"name": "XmasLifx",
"version": "0.0.1",
"description": "Ideino project",
"author": {
"name": "Ideino Team"
},
"dependencies": {
"lifx": "*"
}
}
The code of the script:
Code
Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).
var linino = require('ideino-linino-lib'),
board = new linino.Board(),
tone = require('tone.json'),
buz = board.pin.pwm.P5;
var del = 0;
var lifx = require('lifx');
var lx = lifx.init();
var jbells = [
"NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_C5", "NOTE_D5", "NOTE_E5",
"NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_D5", "NOTE_D5", "NOTE_E5", "NOTE_D5", "NOTE_G5",
"NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_C5", "NOTE_D5", "NOTE_E5",
"NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_G5", "NOTE_F5", "NOTE_D5", "NOTE_C5", "0", "0"
];
var jbellstimes = [
200, 200, 400, 200, 200, 400, 200, 200, 200, 200, 800,
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 400, 400,
200, 200, 400, 200, 200, 400, 200, 200, 200, 200, 800,
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 800
];
lx.lightsOn();
function play(melody, times) {
board.noTone(buz);
for (thisNote = 0; thisNote < melody.length; thisNote++) {
sendsound(tone.tone[melody[thisNote]], del);
del = times[thisNote] + del;
}
}
function sendsound(sound, time) {
setTimeout(function() {
board.tone(buz, sound);
lx.lightsColour((sound * 16384) & 0xFFFF, 32767, 65535, 0, 0);
}, time);
}
board.connect(function() {
board.pinMode(buz, board.MODES.PWM);
play(jbells, jbellstimes);
});
We connect the buzzer to Pin PWM 5.
Require file tone.json, because we use to map each note to a corresponding frequency. Download the file tone.json
We send running the file and let’s enjoy this new Christmas.
{
"name": "XmasLifx",
"version": "0.0.1",
"description": "Ideino project",
"author": {
"name": "Ideino Team"
},
"dependencies": {
"lifx": "*"
}
}
var linino = require('ideino-linino-lib'),
board = new linino.Board(),
tone = require('tone.json'),
buz = board.pin.pwm.P5;
var del = 0;
var lifx = require('lifx');
var lx = lifx.init();
var jbells = [
"NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_C5", "NOTE_D5", "NOTE_E5",
"NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_D5", "NOTE_D5", "NOTE_E5", "NOTE_D5", "NOTE_G5",
"NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_C5", "NOTE_D5", "NOTE_E5",
"NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_F5", "NOTE_E5", "NOTE_E5", "NOTE_E5", "NOTE_G5", "NOTE_G5", "NOTE_F5", "NOTE_D5", "NOTE_C5", "0", "0"
];
var jbellstimes = [
200, 200, 400, 200, 200, 400, 200, 200, 200, 200, 800,
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 400, 400,
200, 200, 400, 200, 200, 400, 200, 200, 200, 200, 800,
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 800
];
lx.lightsOn();
function play(melody, times) {
board.noTone(buz);
for (thisNote = 0; thisNote < melody.length; thisNote++) {
sendsound(tone.tone[melody[thisNote]], del);
del = times[thisNote] + del;
}
}
function sendsound(sound, time) {
setTimeout(function() {
board.tone(buz, sound);
lx.lightsColour((sound * 16384) & 0xFFFF, 32767, 65535, 0, 0);
}, time);
}
board.connect(function() {
board.pinMode(buz, board.MODES.PWM);
play(jbells, jbellstimes);
});
Comments
Please log in or sign up to comment.