Skip to content

Instantly share code, notes, and snippets.

@piatra
Forked from rmurphey/gist:3185390
Created July 27, 2012 06:57
Show Gist options
  • Save piatra/3186566 to your computer and use it in GitHub Desktop.
Save piatra/3186566 to your computer and use it in GitHub Desktop.
shiftOut in javascript
var five = require("johnny-five"),
board;
board = new five.Board();
board.on("ready", function() {
(new five.Led(13)).on();
var dataPin = 2;
var clockPin = 3;
var latchPin = 4;
var value = 0;
next(value);
function next(i) {
board.digitalWrite(latchPin, board.firmata.LOW);
shiftOut(dataPin, clockPin, i);
board.digitalWrite(latchPin, board.firmata.HIGH);
if (++i > 255) {
return;
}
setTimeout(function() {
next(i);
}, 500);
}
function shiftOut(dataPin, clockPin, value) {
// much thanks to http://www.sqlskills.com/blogs/paulselec/post/Arduino-figuring-out-shift-registers.aspx
for (var mask = 128; mask > 0; mask = mask >> 1) {
board.digitalWrite(clockPin, board.firmata.LOW);
board.digitalWrite(
dataPin, board.firmata[ value & mask ? 'HIGH' : 'LOW' ]
);
board.digitalWrite(clockPin, board.firmata.HIGH)
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment