- Programming the BeagleBone
- Yogesh Chavan
- 563字
- 2021-07-23 14:36:47
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:
- 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 byb.<function name>
and variables byb.<variable name>
. The BoneScript library provides several functions that are useful to interact with BeagleBone. - 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.
- 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 asUSR3
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.
- Learn TypeScript 3 by Building Web Applications
- Software Defined Networking with OpenFlow
- JavaScript修煉之道
- 數(shù)據(jù)結構(Python語言描述)(第2版)
- 老“碼”識途
- YARN Essentials
- Drupal 8 Module Development
- 從零開始學C語言
- C專家編程
- Kivy Cookbook
- 深入理解C指針
- Ext JS 4 Plugin and Extension Development
- Visual C++開發(fā)寶典
- Java高級程序設計
- Flask開發(fā)Web搜索引擎入門與實戰(zhàn)