- Programming the BeagleBone
- Yogesh Chavan
- 631字
- 2021-07-23 14:36:47
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:
- 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.
- This time, we are naming the timer created by
setInterval()
asloopTimer
. It is scheduled to tick after each second. Our program cannot exit ifloopTimer
is not cleared. The new timer eventsetTimeout()
method asks the system to call theexitProgram()
handler function after 60,000 milliseconds (one minute) only once. UnlikesetInterval()
, thesetTimeout()
function generates a timer event only once. - The
blink()
function is the same as in the earlier program. This function turns the LED on/off after each second. The newexitProgram()
callback function is added. InexitProgram()
, the LED is turned off first. Then, theclearInterval()
method stops further execution of theblink()
function associated with theloopTimer
timer event. TheclearInterval()
method takes the timer name as an argument. WhenloopTimer
is cleared, there is nothing left for execution and the program exits. Thus, the LED will always be off when the program finishes. ThesetTimeout()
,setInterval()
, andclearInterval()
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.
- Learn Type:Driven Development
- Julia機器學習核心編程:人人可用的高性能科學計算
- Learning Informatica PowerCenter 10.x(Second Edition)
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓版)
- Java軟件開發(fā)基礎
- Mastering ServiceNow(Second Edition)
- Java Web程序設計任務教程
- Gradle for Android
- Mastering JavaScript Design Patterns(Second Edition)
- RealSenseTM互動開發(fā)實戰(zhàn)
- 細說Python編程:從入門到科學計算
- 軟件測試綜合技術
- 程序員的成長課
- Getting Started with Web Components
- 分布式系統(tǒng)架構與開發(fā):技術原理與面試題解析