Hardware Reference
In-Depth Information
Solution
BoneScript has an analogWrite() function that uses the Bone's pulse width modula-
tion (PWM) hardware to produce an analog out signal. We'll use the same circuit as before
( Figure 3-3 ) and declare the pin mode to be ANALOG_OUTPUT . Add the code in
Example 3-3 to a file called fadeLED.js and then run it as before.
Example 3-3. Code for using an external LED (fadeLED.js)
#!/usr/bin/env node
var b = require ( 'bonescript' );
var LED = 'P9_14' ; // Pin to use
var step = 0.02 , // Step size
min = 0.02 , // dimmest value
max = 1 , // brightest value
brightness = min ; // Current brightness;
b . pinMode ( LED , b . ANALOG_OUTPUT , 6 , 0 , 0 , doInterval );
function doInterval ( x ) {
if ( x . err ) {
console . log ( 'x.err = ' + x . err );
return ;
}
setInterval ( fade , 20 );
// Step every 20 ms
}
function fade () {
b . analogWrite ( LED , brightness );
brightness += step ;
if ( brightness >= max || brightness <= min ) {
step = - 1 * step ;
}
}
Search WWH ::




Custom Search