- 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.
推薦閱讀
- SharePoint Development with the SharePoint Framework
- bbPress Complete
- The DevOps 2.5 Toolkit
- Visual C++開發入行真功夫
- 用案例學Java Web整合開發
- 匯編語言編程基礎:基于LoongArch
- Arduino機器人系統設計及開發
- Python Social Media Analytics
- Less Web Development Cookbook
- Python滲透測試編程技術:方法與實踐(第2版)
- Practical Time Series Analysis
- 機器人ROS開發實踐
- Java EE應用開發及實訓
- Learning Unity Physics
- Go語言從入門到進階實戰(視頻教學版)