Write the following program in Cloud9 and save it as danceExternalLEDs.js
. Run it and you should see dancing LEDs in both directions:
var b = require('bonescript'), var loopTime = 50; var exitTime = 20000; var reverse_direction; var index; var LEDs = ["P8_7","P8_9","P8_11","P8_13","P8_15","P8_17","P8_19"]; for(index in LEDs) { b.pinMode(LEDs[index],b.OUTPUT); } index=0; var loopTimer = setInterval(danceLEDs, loopTime); var exitTimer = setTimeout(exitProgram,exitTime); function danceLEDs() { switch(index) { case 0: b.digitalWrite(LEDs[index+1],b.LOW); b.digitalWrite(LEDs[index++],b.HIGH); reverse_direction = false; break; case (LEDs.length-1): b.digitalWrite(LEDs[index-1],b.LOW); b.digitalWrite(LEDs[index--],b.HIGH); reverse_direction = true; break; default: if(reverse_direction == true) { b.digitalWrite(LEDs[index+1],b.LOW); b.digitalWrite(LEDs[index--],b.HIGH); } else { b.digitalWrite(LEDs[index-1],b.LOW); b.digitalWrite(LEDs[index++],b.HIGH); } } } function stopTimer() { for(index in LEDs) { b.digitalWrite(LEDs[index],b.LOW); } clearTimeout(tempTimer); }
First we declared an array of strings and named it LEDs
. We initialized it with names of all attached GPIO
pins sequentially. The for(index in LEDs)
code iterates over all elements in array LEDs and sets the direction of each GPIO
pin to output. We set the exitTime
timer to call the exitProgram()
function after 20 seconds. It will turn off an LED if any one is left turned on and exit the program. Then we set the loopTime
timer to call the danceLEDs()
function after each 50
milliseconds.
Inside the danceLEDs()
function, we consider four different cases based on the value of the global variables index
and reverse_direction
:
index = 0
then we turn the first GPIO
pin(P8_7
) to HIGH
. This will glow the first LED. Then we increment the index
and break the loop. After 50
milliseconds, danceLED()
will be called again.0 < index < 7
(length of array) and reverse_direction
flag is not set, then turn the current array index GPIO
to HIGH
and turn the previous array index GPIO
to LOW
. Then we increment the index
and break the loop. This will create a glowing LED illusion from the second LED until the last LED.index = 7
, then turn the last GPIO
pin (p8_19
) to HIGH
and the previous GPIO
(P8_17
) to LOW
. This will create a glowing LED illusion from the second to last LED to the last LED. The variable index
is decremented here and the flag reverse_direction
is set.0 < index < 7
and reverse_direction
flag is set, then turn the current array index GPIO
to HIGH
and turn the next array index GPIO
to LOW
. Then we decrement the index
and break the loop. This creates a glowing LED illusion in the reverse direction (from the last LED till 1st LED). When control reaches the first LED, the reverse_direction
flag is cleared and we are into the index = 0
case again.Function danceLEDs()
works on members of LEDs
array. We have not hard-coded values. If we add another GPIO
pin as a new member in the LEDs
array, the program will still consider newly added pins and iterate through all array members. That means we can extend this program to more LEDs. All you need to do is connect a new LED to free GPIO
pins physically and add their names in LEDs
array in the program. You can replace the LEDs' array initialization line with the following to get the same glowing pattern on USER LEDs:
var LEDs = ["USR0","USR1","USR2","USR3"];
Troubleshooting steps for this program are the same as for the previous program.
18.117.78.237