Dancing LEDs

BeagleBone has four onboard LEDs adjacent to each other. Let's create an LED on/off pattern so that we get an illusion of dancing LEDs. We will follow the turning on the LED sequence: USER0 -> USER1 -> USER2 -> USER3 -> USER0 -> USER1 -> USER2 -> USER3 -> USER0, and so on. This will give the illusion that light is traveling from USER0 to USER3 in loops. Type the following program in Cloud9, save it as danceLEDs.js and run it. The code for danceLEDs.js is as follows:

var b = require('bonescript'),
var glowTime = 100;
var exitTime = 30000;
var tempTimer;

b.pinMode("USR0", b.OUTPUT);
b.pinMode("USR1", b.OUTPUT);
b.pinMode("USR2", b.OUTPUT);
b.pinMode("USR3", b.OUTPUT);

var exitTimer = setTimeout(exitProgram,exitTime);
glowUser0();

function glowUser0()
{
  b.digitalWrite("USR3", b.LOW);
  b.digitalWrite("USR0", b.HIGH);
  tempTimer = setTimeout(glowUser1,glowTime);
}

function glowUser1()
{
  b.digitalWrite("USR0", b.LOW);
  b.digitalWrite("USR1", b.HIGH);
  tempTimer = setTimeout(glowUser2,glowTime);
}

function glowUser2()
{
  b.digitalWrite("USR1", b.LOW);
  b.digitalWrite("USR2", b.HIGH);
  tempTimer = setTimeout(glowUser3,glowTime);
}

function glowUser3()
{
  b.digitalWrite("USR2", b.LOW);
  b.digitalWrite("USR3", b.HIGH);
  tempTimer = setTimeout(glowUser0,glowTime);
}

function exitProgram()
{
  b.digitalWrite("USR0",b.LOW);
  b.digitalWrite("USR1",b.LOW);
  b.digitalWrite("USR2",b.LOW);
  b.digitalWrite("USR3",b.LOW);
  clearTimeout(tempTimer);
}

Program explanation

Though this program looks bigger, it has lots of repetitive code. All the functions except exitProgram() are almost identical. The glowTime and exitTime variables are soft-coded. Set them to different values and observe the difference.

The following is a line-by-line explanation:

  1. First, we mark all the LEDs as the output.
  2. Create a timer event named exitTimer with setTimeout(). It will clear the tempTimer event and exit the program after 30 seconds.
  3. The glowUser0() function turns on the USER0 LED and calls the glowUser1() function with a delay of glowTime (100 milliseconds). As this is setTimeout(), the calling of glowUser1() will be only once.
  4. The glowUser1() function turns off the USER0 LED that was just turned on in the previous step. Then, it will turn on the USER1 LED and call glowUser2() only once with a delay of 100 milliseconds.
  5. The glowUser2() function turns off USER1 that we turned on earlier. Then, it will turn on the USER2 LED and call glowUser3() only once with a delay of 100 milliseconds.
  6. The glowUser3() function turns off USER2 and calls glowUser0() only once with a glowTime delay. This completes the loop. The glowUser0() function will turn off the lit USER3 LED and turn on the USER0 LED. Then, it calls the glowsUser1() function with a delay of 100 milliseconds. This way, the loop continues.
  7. While this loop is going on, the exitTimer event is counting time in parallel. It will trigger after 30 seconds. The exitProgram() function will be called. It will turn off all the LEDs and clear tempTimer. As nothing is left for execution, the program will exit.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.154