Skip to content

Commit

Permalink
added javascript version of LED blink
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChambers committed Jan 15, 2018
1 parent 4d0192b commit 3fe907a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions project01/testwiringpi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// blink an LED with JavaScript.
//
// requires the following:
// nodejs as a container to run in
// should already be in the Raspbian image
// npm to install the onoff library
// sudo apt-get install npm
// onoff to interface with the GPIO board
// sudo npm install onoff

// a simple and brute force sleep function that
// Javascript does not have as a built-in.
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}

var Gpio = require('onoff').Gpio;
var LED = new Gpio(17, 'out'); // use BCM pin numbering, not wiringPi

// a loop to blink the LED 10 times.
for (i=0; i < 10; i++) {
LED.writeSync(0);
sleep (1000);
LED.writeSync(1);
sleep (1000);
}

LED.unexport();

0 comments on commit 3fe907a

Please sign in to comment.