- Programming the BeagleBone
- Yogesh Chavan
- 439字
- 2021-07-23 14:36:47
Dancing LEDs
BeagleBone has four onboard LEDs adjacent to each other. Let's create an LED on/off pattern so that we get an illusion of dancing LEDs. We will follow the turning on the LED sequence: USER0 -> USER1 -> USER2 -> USER3 -> USER0 -> USER1 -> USER2 -> USER3 -> USER0, and so on. This will give the illusion that light is traveling from USER0 to USER3 in loops. Type the following program in Cloud9, save it as danceLEDs.js
and run it. The code for danceLEDs.js
is as follows:
var b = require('bonescript'); var glowTime = 100; var exitTime = 30000; var tempTimer; b.pinMode("USR0", b.OUTPUT); b.pinMode("USR1", b.OUTPUT); b.pinMode("USR2", b.OUTPUT); b.pinMode("USR3", b.OUTPUT); var exitTimer = setTimeout(exitProgram,exitTime); glowUser0(); function glowUser0() { b.digitalWrite("USR3", b.LOW); b.digitalWrite("USR0", b.HIGH); tempTimer = setTimeout(glowUser1,glowTime); } function glowUser1() { b.digitalWrite("USR0", b.LOW); b.digitalWrite("USR1", b.HIGH); tempTimer = setTimeout(glowUser2,glowTime); } function glowUser2() { b.digitalWrite("USR1", b.LOW); b.digitalWrite("USR2", b.HIGH); tempTimer = setTimeout(glowUser3,glowTime); } function glowUser3() { b.digitalWrite("USR2", b.LOW); b.digitalWrite("USR3", b.HIGH); tempTimer = setTimeout(glowUser0,glowTime); } function exitProgram() { b.digitalWrite("USR0",b.LOW); b.digitalWrite("USR1",b.LOW); b.digitalWrite("USR2",b.LOW); b.digitalWrite("USR3",b.LOW); clearTimeout(tempTimer); }
Program explanation
Though this program looks bigger, it has lots of repetitive code. All the functions except exitProgram()
are almost identical. The glowTime
and exitTime
variables are soft-coded. Set them to different values and observe the difference.
The following is a line-by-line explanation:
- First, we mark all the LEDs as the output.
- Create a timer event named
exitTimer
withsetTimeout()
. It will clear thetempTimer
event and exit the program after 30 seconds. - The
glowUser0()
function turns on the USER0 LED and calls theglowUser1()
function with a delay ofglowTime
(100 milliseconds). As this issetTimeout()
, the calling ofglowUser1()
will be only once. - The
glowUser1()
function turns off the USER0 LED that was just turned on in the previous step. Then, it will turn on the USER1 LED and callglowUser2()
only once with a delay of 100 milliseconds. - The
glowUser2()
function turns off USER1 that we turned on earlier. Then, it will turn on the USER2 LED and callglowUser3()
only once with a delay of 100 milliseconds. - The
glowUser3()
function turns off USER2 and callsglowUser0()
only once with aglowTime
delay. This completes the loop. TheglowUser0()
function will turn off the lit USER3 LED and turn on the USER0 LED. Then, it calls theglowsUser1()
function with a delay of 100 milliseconds. This way, the loop continues. - While this loop is going on, the
exitTimer
event is counting time in parallel. It will trigger after 30 seconds. TheexitProgram()
function will be called. It will turn off all the LEDs and cleartempTimer
. As nothing is left for execution, the program will exit.
推薦閱讀
- MongoDB for Java Developers
- Java游戲服務器架構實戰
- INSTANT CakePHP Starter
- GitLab Repository Management
- Python:Master the Art of Design Patterns
- JavaScript腳本特效編程給力起飛
- Java圖像處理:基于OpenCV與JVM
- 創意UI:Photoshop玩轉APP設計
- Learning Nessus for Penetration Testing
- 單片機原理及應用技術
- 現代C:概念剖析和編程實踐
- 會當凌絕頂:Java開發修行實錄
- Visual C++程序開發范例寶典
- Learning Swift
- 智能優化算法與MATLAB編程實踐