O'Reilly logo

Programming the BeagleBone by Yogesh Chavan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program to fade in and fade out LED

Write the following code in Cloud9 and save it as fadeLED.js. Run the program and you should see the LED fading in and out alternatively.

The code for fadeLED.js is as follows:

var b = require('bonescript');
var led = "P9_21";
var loopTime = 20;
var duty_cycle=0;
var increment = true;

var loopTimer = setInterval(fadeLED, loopTime);

function fadeLED()
{
    if(duty_cycle == 100 )
        increment = false;
    if(duty_cycle == 0)
        increment = true;

    if(increment == true)
        duty_cycle = duty_cycle + 1;
    else
        duty_cycle = duty_cycle - 1;

    console.log("duty_cycle = ",duty_cycle/100);
    b.analogWrite(led,duty_cycle/100);
}

Explanation

In Chapter 3, Blinking External LEDs we used the function digitalWrite() with a similar setup. Here, we are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required