- 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.
- PWA入門與實踐
- Learning Cython Programming
- Android項目開發入門教程
- Visual C++串口通信開發入門與編程實踐
- Building Modern Web Applications Using Angular
- GameMaker Programming By Example
- 碼上行動:用ChatGPT學會Python編程
- 大學計算機基礎實驗指導
- Java EE核心技術與應用
- 深入分布式緩存:從原理到實踐
- ASP.NET開發與應用教程
- C#程序設計教程(第3版)
- Building Wireless Sensor Networks Using Arduino
- Python+Tableau數據可視化之美
- Swift 4從零到精通iOS開發