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); }
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...