Quick program to blink onboard LED

Now that we have written a program to turn the USER3 LED on and off, let's write a program to blink this LED. Type the following program in Cloud9, save it as blinkOnboardLED.js, and run it. Your USER3 LED should start blinking each second. This is an infinite loop. You can stop the loop by clicking on the red Stop button in Cloud9. The code for blinkOnboardLED.js is as follows:

var b = require('bonescript'),
var state = b.HIGH;

b.pinMode("USR3", b.OUTPUT);
b.digitalWrite("USR3", state);

setInterval(blink,1000);

function blink()
{
  if(state == b.LOW)
    state = b.HIGH;
  else
    state = b.LOW;
  b.digitalWrite("USR3", state);
}

Program explanation

We want to blink the LED after each second. If one wants to code this in C, they will undoubtedly use the sleep() function in the loop. However, JavaScript is used in web development mostly. It executes client-side code and talks with the web server asynchronously. So, it has different rules. It prefers asynchronous event handler functions over sleeping the whole JavaScript engine. There is no sleep-like function in JavaScript. Instead, it provides timing events. We need to write an event handler function and event condition. The handler function gets called asynchronously when the timing event condition is met. So, the JavaScript execution engine does not have to keep on waiting until the condition is met. It can go ahead executing the next code. These handler functions are callback functions in event-driven programming. This whole concept is the same as registering the signal and writing the signal handler function for that signal.

The following is a line-by-line explanation of the preceding code:

  1. We are importing the BoneScript library and assign it to the b variable.
  2. The new variable is named state and assigned a HIGH value. We will assign it to the USER3 LED soon.
  3. We declare that the USER3 pin will be used for the output using the pinMode() function.
  4. Write the state variable (which is HIGH) on the USER3 LED. USER3 is turned on at this point.
  5. We are creating a timer event, which will be triggered each second. The JavaScript setInterval() method provides the functionality to execute a block of code again and again after a specified time. This block of code should be put in the setInterval() method. The setInterval() method takes the handler function to be called repetitively as the first argument. It takes second argument as interval in milliseconds after which the handler function should be called repeatedly. Here, we are calling the blink() function after 1,000 milliseconds (one second) again and again. This is similar to the loop() function in the Arduino world.
  6. Now, we write the blink() function, which will be called after each second. The blink() function is the timer event handler function that toggles the USER3 LED on or off. We check the current value of the state variable in the blink() function. If it is LOW, then state is changed to a HIGH value. Otherwise, it is set to a LOW value. Later, we set USER3 to the state variable.

Note

If you noticed, we did not specify a variable type at the time of declaring the variable. In JavaScript, a variable's type is set based on the value it got.

Program execution

When we run this program, it will first set the USER3 LED to HIGH. This results in turning on the V16 (or GPIO1_24) pin of the processor internally, and 3.3 voltage is put across this pin. As the V16 pin is connected to the USER3 LED, the current starts flowing through the USER3 LED and it glows. For more details, check the User LEDs section in the BeagleBone System Reference Manual(SRM),. This manual PDF file can be found in the FAT partition of BeagleBone. You can get the online version from https://github.com/CircuitCo/BeagleBone-Black/blob/rev_b/BBB_SRM.pdf.

Then, the setInterval() method will set the timer with the blink() callback function for one second. This means that blink() will be called automatically after each second. At the first call, as the state variable has the HIGH value, it will be changed to LOW and digitalWrite() will be called. Control will go in the BoneScript library code and the library will write LOW on the sysfs file associated with the USER3 LED. The current will stop flowing through USER3 and it will be turned off.

After one second, the blink() function will be called again. This time, the state variable has the LOW value. It will be changed to HIGH and the USER3 LED will glow again for a second. The blink() function will be called again and the USER3 LED will turn off again, and so on.

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

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