Make our program better

If you executed the previous program a few times, you would have noticed that though the program is getting executed correctly, it does not exit by itself. It has an infinite loop of timer. So, it needs to be stopped manually. If the LED state is on at the time of stopping the program, it will remain on later. Let's add a new timer that will trigger after a minute to exit the program and turn off the USER3 LED if it is on. Also, let's change the hard-coded values that we used in the previous program. Type the following program in Cloud9, save it as blinkOnboardLED2.js, and run it. The program should stop automatically after a minute with the USER3 LED always off. The code for blinkOnboardLED2.js is as follows:

var b = require('bonescript'),
var led = "USR3";
var loopTime = 1000;
var exitTime = 60000;
var state = b.HIGH;

b.pinMode(led, b.OUTPUT);
b.digitalWrite(led, state);

var loopTimer = setInterval(blink, loopTime);
setTimeout(exitProgram,exitTime);

function blink()
{
  if(state == b.LOW)
    state = b.HIGH;
  else
    state = b.LOW;
  b.digitalWrite(led, state);
}
function exitProgram()
{
  b.digitalWrite(led,b.LOW);
  clearInterval(loopTimer);
}

Program explanation

This code is small addition to the previous LED blinking code:

  1. We hard-coded the values of the USER3 LED and blinking time in the last program. Here, we declared them in the beginning and initialized them with appropriate values. Now, just by changing the values of the variables at the beginning of the code, we can test different behaviors easily.
  2. This time, we are naming the timer created by setInterval() as loopTimer. It is scheduled to tick after each second. Our program cannot exit if loopTimer is not cleared. The new timer event setTimeout() method asks the system to call the exitProgram() handler function after 60,000 milliseconds (one minute) only once. Unlike setInterval(), the setTimeout() function generates a timer event only once.
  3. The blink() function is the same as in the earlier program. This function turns the LED on/off after each second. The new exitProgram() callback function is added. In exitProgram(), the LED is turned off first. Then, the clearInterval() method stops further execution of the blink() function associated with the loopTimer timer event. The clearInterval() method takes the timer name as an argument. When loopTimer is cleared, there is nothing left for execution and the program exits. Thus, the LED will always be off when the program finishes. The setTimeout(), setInterval(), and clearInterval() methods come from an HTML DOM window object. The HTML DOM window object is a top-level object in the JavaScript hierarchy and it represents the web browser window. If you are curious, you can refer to http://www.w3schools.com/js/js_timing.asp.

If you have connected BeagleBone via Ethernet and your smartphone is in the same LAN network, then you can open Cloud9 in your smartphone browser (by opening <beagleboneip>:3000) and run the program. In this case, you will be able to start/stop the LED blinking remotely via a smartphone. This hack is applicable to all the programs in this book.

Until now, we have been running the Cloud9 IDE to execute our programs. What if we want to run it directly without Cloud9? Cloud9 IDE uses the /usr/bin/node interpreter to execute the code. Can the IDE be bypassed in order to run the BoneScript program directly by the Node interpreter? The answer is yes.

All the files created in the Cloud9 IDE get stored in the /var/lib/cloud9 directory by default. Get the shell access of BeagleBone in any of the ways suggested in Chapter 1, Cloud9 IDE, and run the following command:

node /var/lib/cloud9/blinkOnboardLED2.js

You will see the USER3 LED blinking until a minute. You can create JavaScript programs in your favorite editor and run these programs by changing the filename in the preceding command. This way, we can bypass Cloud9 totally.

..................Content has been hidden....................

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