Strobing Led and Piezo Song
This is one of the exercises for the Port Coquitlam Node and Nodebots Meetup. This challenge was posted in order to do a quick piezo demo.
'use strict';
var five = require('johnny-five'),
board = new five.Board();
board.on('ready', function(){
console.log('Board is ready');
led.strobe(10000, function(){
var led = new five.Led(13);
var piezo = new five.Piezo(3);
piezo.play({
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
});
});
});
console.log('Waiting for board...');
Hey, there is a bug in your code!
You got it! There is a bug. Can you spot it? After loading the firmatta to the microcontroller and running the above program we saw in the console something like this:
The board is telling us that "pin 3 is already in use". Hum, it is only instantiated once... or is it? Strobe is an interval operation and every time the callback function is executed we try to create a new piezo in the scope of that function using pin 3. During the first strobe interval there is no error but the second time the strobe interval passes then we get the warning because pin 3 is already taken.
To fix the bug we need to take the line where the new piezo is declared and place it before led.strobe()
Fixed!
'use strict';
var five = require('johnny-five'),
board = new five.Board();
board.on('ready', function(){
console.log('Board is ready');
var led = new five.Led(13);
var piezo = new five.Piezo(3);
led.strobe(10000, function(){
piezo.play({
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
});
});
});
console.log('Waiting for board...');
No comments:
Post a Comment