var cncserver = require("cncserver");
var serialport = require("./node_modules/cncserver/node_modules/serialport/serialport.js");
var SerialPort = serialport.SerialPort;

var port = process.argv[2] ? process.argv[2] : "/dev/ttyACM0";

// Actually try to init the connection and handle the various callbacks
cncserver.start({
  success: function() {
    console.log('Port found, connecting...');
  },
  error: function(err) {
    console.log("couldn't connect to bot!");
  },
  connect: function() {
    console.log('Bot Connected!');
    cncserver.setHeight('up');
    startReading();
  },
  disconnect: function() {
    console.log("Bot disconnected!")
  }
});

function startReading(){
  var x = 0;
  var y = 0;
  var b = 0;

  var lastB = 0;
  var lastX = 0;
  var lastY = 0;

  var gettingWater = false;
  var movingPen = false;

  var doubleClickWait = false;
  var reWaterInterval = 0;

  var drawPt = {x: 0, y: 0};

  var serialPort = new SerialPort(port, {
    baudrate : 9600,
    parser: serialport.parsers.readline("\n")
  });

  serialPort.on("data", function(data){
    var d = data.split('|');
    x = parseInt(d[0]);
    y = parseInt(d[1]);
    b = parseInt(d[2]);

    // Button get water
    if (b!== lastB) {
      lastB = b;
      if (b === 0) {
        if (!gettingWater) {
          if (!doubleClickWait) {
            doubleClickWait = true;
            console.log('Single button...');
            reWaterInterval = setTimeout(function(){
              doubleClickWait = false;
              console.log('Button Pressed/Released! getting Water');
              getWater(drawPt);
            }, 250);
          } else {
            console.log('Double button! Parking.');
            clearTimeout(reWaterInterval);
            movingPen = true;
            doubleClickWait = false;
            cncserver.setHeight('up');
            cncserver.setPen({x: 0, y: 0, park: true}, function(){
              movingPen = false;
            });
          }

        } else {
          console.log('Already Getting Water');
        }
      }
    }

    // X/Y Movement
    if (!gettingWater && !movingPen) {
      drawPt = {
        x: (lastX + 1) / 10.24,
        y: (lastY + 1) / 10.24
      };

      if (x!== lastX && x!== lastX-1 && x!== lastX+1) {
        lastX = x;
        drawPt.x = (x+1) / 10.24;
        movingPen = true;
      }

      if (y!== lastY && y!== lastY-1 && y!== lastY+1) {
        lastY = y;
        drawPt.y = (y+1) / 10.24;
        movingPen = true;
      }

      // If this is set, then we had a change enough to set this
      if (movingPen) {
        //console.log(drawPt);
        cncserver.setPen(drawPt, function(){
          movingPen = false;
        });
      }
    }

  });
}

// Wrapper for getting water
function getWater(destPt) {
  gettingWater = true;
  cncserver.setTool('water0', function(){
    cncserver.setPen(destPt, function(){
      cncserver.setHeight('draw', function(){
        movingPen = false;
        gettingWater = false;
      })
    });
  });
}

@@ -198,43 +198,8 @@
 v%5B2%5D
- ? process.argv%5B2%5D : %22/dev/ttyACM0%22
 ;%0D%0A%0D
@@ -536,24 +536,59 @@
 ght('up');%0D%0A
+    connectLeonardo(function()%7B%0D%0A  
     startRea
@@ -592,24 +592,33 @@
 Reading();%0D%0A
+    %7D);%0D%0A
   %7D,%0D%0A  disc
@@ -3134,20 +3134,552 @@
 %0A    %7D);%0D%0A  %7D);%0D%0A%7D%0D%0A
+%0D%0A%0D%0A// Helper function for automatically connecting the littleBits leonardo%0D%0Afunction connectLeonardo(callback) %7B%0D%0A  serialport.list(function (err, ports) %7B%0D%0A    for (var portID in ports)%7B%0D%0A      if (ports%5BportID%5D.serialNumber === 'Arduino_LLC_Arduino_Leonardo') %7B%0D%0A        port = ports%5BportID%5D.comName;%0D%0A      %7D%0D%0A    %7D%0D%0A%0D%0A    if (!port) %7B%0D%0A      console.error('Littlebits Leonardo not found! ERR 16');%0D%0A      process.exit(16);%0D%0A    %7D else %7B%0D%0A      console.log('Leonardo found on port:', port);%0D%0A      callback();%0D%0A    %7D%0D%0A  %7D);%0D%0A%7D%0D%0A

Add auto port finding for the Arduino (& minimum error port exit!)