- Programming the BeagleBone
- Yogesh Chavan
- 772字
- 2021-07-23 14:36:47
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:
- We are importing the BoneScript library and assign it to the
b
variable. - The new variable is named
state
and assigned a HIGH value. We will assign it to the USER3 LED soon. - We declare that the USER3 pin will be used for the output using the
pinMode()
function. - Write the
state
variable (which is HIGH) on the USER3 LED. USER3 is turned on at this point. - 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 thesetInterval()
method. ThesetInterval()
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 theblink()
function after 1,000 milliseconds (one second) again and again. This is similar to theloop()
function in the Arduino world. - Now, we write the
blink()
function, which will be called after each second. Theblink()
function is the timer event handler function that toggles the USER3 LED on or off. We check the current value of thestate
variable in theblink()
function. If it is LOW, thenstate
is changed to a HIGH value. Otherwise, it is set to a LOW value. Later, we set USER3 to thestate
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.
- Java應(yīng)用與實戰(zhàn)
- AngularJS Testing Cookbook
- INSTANT OpenCV Starter
- PyTorch自動駕駛視覺感知算法實戰(zhàn)
- Java應(yīng)用開發(fā)與實踐
- SQL for Data Analytics
- Python高級編程
- STM32F0實戰(zhàn):基于HAL庫開發(fā)
- 概率成形編碼調(diào)制技術(shù)理論及應(yīng)用
- 學(xué)Python也可以這么有趣
- SharePoint Development with the SharePoint Framework
- 自然語言處理Python進階
- MongoDB,Express,Angular,and Node.js Fundamentals
- Mastering Elixir
- 分布式數(shù)據(jù)庫原理、架構(gòu)與實踐