walterhiggins/ScriptCraft — pull request #146 by samkass (2014-06-23 02:55:36+00:00)
Title: Add serduino, a module which allows serial communciation with arduino

This module allows a serial-connected Arduino to communicate with ScriptCraft. It includes sc-serial.js, a serial port communications module; serduino.js, the serduino module that defines a serial protocol; and serduino.ino, an Arduino program that also speaks the serduino protocol.

More details are in the documentation in the code, but here's the Usage section:

```javascript
var serduino = require('serduino');
// Create a connection object
var conn = serduino.connect();
// Start the connection
conn.start();
// After a second or two to make sure it's going...
// Send an initial command to let the Arduino know we're here
conn.writeDigital(2,0);

// Now the Arduino will send us regular updates, and we can set pins

// Put an LED and 1K resistor in series across pin 2. Turn it on with:
conn.writeDigital(2,255); // Any number other than 0 will work...

// Or turn it on when any redstone is activated in the world...
events.blockRedstone(function(event){conn.writeDigital(2,event.newCurrent);});

// Put a potentiometer on analog pin 0 and read its value with:
conn.readAnalog(0);

// Callbacks can also be installed so a function is triggered when a pin changes...
// This example launches some fireworks at location (-292,0,0) when pin 5 goes HIGH:
conn.onPinChanged(5,function(pin,value) {
if (value) new Drone(-292,0,0,1,server.worlds.get(0)).firework();
});
// This example sets night/day according to an analog pin (eg. potentiometer, light meter):
// (It doesn't process 0's in case there's a flaky connection to the sensor)
conn.onPinChanged(0,function(pin,value) {
if (value != 0) server.worlds.get(0).time = 6000+value*12000/256;
});
```