官术网_书友最值得收藏!

Program to turn onboard LED ON and OFF

We have collected enough information to program an onboard LED. BeagleBone comes with four small onboard user LEDs. They are located above the mini USB port. They are labeled as USER LEDs and named USER0, USER1, USER2, and USER3 LEDs. You can refer to the following image. We will turn on USER3 LED, which is configured by default to turn on each time the emmc is accessed:

Create a new file in Cloud9 as we did in the previous chapter. Write the following code in it and save it as turnOnUSER3.js. Run the program and you should see USER3 LED turned on. The code for turnOnUSER3.js is as follows:

var b = require('bonescript');
b.pinMode("USR3", b.OUTPUT);
b.digitalWrite("USR3", b.HIGH);

Now, turning the USER3 LED off is straightforward. You just need to change b.HIGH to b.LOW in the digitalWrite() function. Save the code as turnOffUSER3.js. Run the program and you should see the USER3 LED turned off. The code for turnOffUSER3.js is as follows:

var b = require('bonescript');
b.pinMode("USR3", b.OUTPUT);
b.digitalWrite("USR3", b.LOW);

Program explanation

This code is in JavaScript. If you know JavaScript, well and good. Even if you do not know, it should not be a big problem. For our work, we need some JavaScript-specific details. We will cover these when we use them. Here is a line-by-line explanation of the preceding code used to turn onboard LED ON and OFF:

  1. We first import the BoneScript library and assign it to the b variable. Now, b is the handle for the BoneScript library. We will get all the BoneScript functionality by b.<function name> and variables by b.<variable name>. The BoneScript library provides several functions that are useful to interact with BeagleBone.
  2. We are declaring here that the USER3 pin will be used as the output. We set the direction as output. This means it is possible to only write on it and not read from it.
  3. Set USER3 LED's pin to HIGH or LOW. The digitalWrite() function in BoneScript is settings USER3 to HIGH or LOW. If set to HIGH, the LED glows. If set to LOW, the LED gets turned off. In the BoneScript code, HIGH is set to 1 and LOW is set to 0. So, you can also use the value 1 instead of HIGH and 0 instead of LOW.

    Note

    Note that we wrote USR3 in the program and not USER3. BoneScript refers to it as USR3 in its code.

    Note that function names are made up of two words. The first word is written in lowercase. The first character of the second word is in uppercase and all the remaining characters are in lowercase. This type of notation is called camel case. This increases the readability of code. It is used to name functions as well as variables. Camel case notation logic is not limited to two words. You can have a multiword function name that follows this notation. JavaScript prefers the camel case notation.

    We turned the LED on/off with our program. This type of showing the physical output is different from the usual way of showing the GUI/CLI output. This type of programming is called physical computing. It involves sensing/getting data from the environment (physical world) and responding to it interactively using physical output components such as LED, buzzer, relay, motor, and so on. Physical computing is used heavily in embedded systems.

主站蜘蛛池模板: 湛江市| 察隅县| 定西市| 图们市| 高邮市| 五原县| 兖州市| 万荣县| 图木舒克市| 玉环县| 金寨县| 曲麻莱县| 忻城县| 华坪县| 类乌齐县| 芮城县| 鹤峰县| 郯城县| 文昌市| 拜城县| 彩票| 芜湖市| 龙井市| 巫溪县| 新兴县| 斗六市| 邢台市| 新田县| 会同县| 岑溪市| 平南县| 嘉黎县| 溧水县| 常州市| 舞阳县| 山东省| 临武县| 泰安市| 凌海市| 河东区| 凉山|