Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Forked from haugstrup/lcd.js
Created July 29, 2012 18:53
Show Gist options
  • Save rmurphey/3201106 to your computer and use it in GitHub Desktop.
Save rmurphey/3201106 to your computer and use it in GitHub Desktop.
LCD proof of concept for johnny-five
// Wire up LCD as described here:
// http://learn.adafruit.com/character-lcds/overview
var five = require("johnny-five"),
board, lcd;
board = new five.Board();
board.on("ready", function() {
lcd = {
rsPin: 7, // LCD pin 4
// rwPin:, // LCD pin 5, only needed when reading from display
enPin: 8, // LCD pin 6
dataPins: [12, 11, 10, 9] // D7 to D4. LCD pins 14, 13, 12, 11
};
var sleep = function(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
};
var highLowFromBinary = function(number) {
return number === 1 ? board.firmata.HIGH : board.firmata.LOW;
};
var sendCommand = function(command) {
board.digitalWrite(lcd.dataPins[0], highLowFromBinary(command[0]));
board.digitalWrite(lcd.dataPins[1], highLowFromBinary(command[1]));
board.digitalWrite(lcd.dataPins[2], highLowFromBinary(command[2]));
board.digitalWrite(lcd.dataPins[3], highLowFromBinary(command[3]));
// Pulse the EN pin to send first nibble
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
board.digitalWrite(lcd.dataPins[0], highLowFromBinary(command[4]));
board.digitalWrite(lcd.dataPins[1], highLowFromBinary(command[5]));
board.digitalWrite(lcd.dataPins[2], highLowFromBinary(command[6]));
board.digitalWrite(lcd.dataPins[3], highLowFromBinary(command[7]));
// Pulse the EN pin to send second nibble
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
};
var writeMessage = function() {
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
sendCommand([0,0,0,0,0,0,0,1]);
board.digitalWrite(lcd.rsPin, board.firmata.HIGH);
sendCommand([0,1,0,0,1,0,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,1]);
sleep(200);
sendCommand([0,1,1,0,1,0,0,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,1,1,0]);
sleep(200);
sendCommand([0,1,1,1,1,0,0,1]);
sleep(200);
sendCommand([0,0,1,0,1,1,0,1]);
sleep(200);
sendCommand([0,1,1,0,0,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,1,0,0,1]);
sleep(200);
sendCommand([0,1,1,1,0,1,1,0]);
sleep(200);
sendCommand([0,1,1,0,0,1,0,1]);
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
};
// RS to low (for command mode), EN to low to start
board.digitalWrite(lcd.rsPin, board.firmata.LOW);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
// Switch to 4-bit mode
board.digitalWrite(lcd.dataPins[0], board.firmata.LOW);
board.digitalWrite(lcd.dataPins[1], board.firmata.LOW);
board.digitalWrite(lcd.dataPins[2], board.firmata.HIGH);
board.digitalWrite(lcd.dataPins[3], board.firmata.LOW);
board.digitalWrite(lcd.enPin, board.firmata.HIGH);
board.digitalWrite(lcd.enPin, board.firmata.LOW);
// Set to two-line mode
sendCommand([0,0,1,0,1,0,0,0]);
// Clear display and turn it on
sendCommand([0,0,0,0,0,0,0,1]);
sendCommand([0,0,0,0,1,1,1,1]);
// Write some text
writeMessage();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment