title: Mouse emulator using Processing, Arduino, and Robot class: inconsistent function
body: <p>I have built a mouse emulator for a severely disabled person, which has a series of buttons arranged in a + pattern, with the center button being a 'click'. The inputs are read into an Arduino Micro, which then connects through USB to the computer. A processing script, using the Arduino library, responds to these changes by moving the mouse, using the Robot class. I exported an application, for both 32-bit and 64-bit windows, but found that the .exe files were problematic, and instead opted for .bat files using the embedded Java.</p>
  
  <p>This all works perfectly on my computer (Microsoft Surface, Windows 8, 64-bit). But on the client's computer (Windows 7, 32-bit), the results are erratic. Each time I load the program, I get a different result. For example, sometimes up, down, and click work. Sometimes only up and click work. And so forth. The click usually seems to work, but the directions are less reliable, with vertical working more often than horizontal.</p>
  
  <p>I have verified that nothing is wrong with the board. I tried putting in a debug statement which ran println() each time a movement's "if" statement was tripped - I only got printing from directions that worked on that particular run.</p>
  
  <p>I see a number of potential sources for this problem: the differences between 32-bit and 64-bit Windows, possible issues with the Robot class, USB performance issues (though I've tried multiple ports on the client's laptop), etc. The client's laptop has generally poor performance (for example, if I click "Control Panel", it may be a full minute before Control Panel opens), so that may also be related.</p>
  
  <p>Does anyone have other insights on this?</p>
  
  <p>EDIT: As requested, here's a minimal version of the Processing code. The Arduino is just running Firmata:</p>
  
  <pre><code>import processing.serial.*;
  import cc.arduino.*; 
  import java.awt.*;
  import java.awt.event.*;
  Arduino arduino;
  Robot r;
  final int upPin=8;
  final int downPin=12;
  final int leftPin=10;
  final int rightPin=11;
  final int buttonPin=9;
  final int SPEED=8;
  boolean arduinoConnected;
  int prevButton=Arduino.LOW;
  
  void setup() {
    arduinoConnected = (Arduino.list().length&gt;0);
    if (arduinoConnected) {
      arduino = new Arduino(this, Arduino.list()[Arduino.list().length-1], 57600);
      arduino.pinMode(buttonPin, Arduino.INPUT);
      arduino.pinMode(upPin, Arduino.INPUT);
      arduino.pinMode(downPin, Arduino.INPUT);
      arduino.pinMode(leftPin, Arduino.INPUT);
      arduino.pinMode(rightPin, Arduino.INPUT);
    } 
    try {
      r = new Robot();
    } 
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  void draw() {
    arduinoConnected = (Arduino.list().length&gt;0);
    if (arduinoConnected &amp;&amp; arduino==null) setup();
    if (arduinoConnected) {
      if (arduino.digitalRead(upPin)==Arduino.HIGH) moveMouse(0,0-SPEED);
      if (arduino.digitalRead(downPin)==Arduino.HIGH) moveMouse(0,SPEED);
      if (arduino.digitalRead(leftPin)==Arduino.HIGH) moveMouse(0-SPEED,0);
      if (arduino.digitalRead(rightPin)==Arduino.HIGH) moveMouse(SPEED,0);
      int button=arduino.digitalRead(buttonPin);
      if (button==Arduino.LOW &amp;&amp; prevButton==Arduino.HIGH) clickMouse(InputEvent.BUTTON1_DOWN_MASK);
      prevButton=button;
    }
  }
  
  void moveMouse(int x, int y) {
    PointerInfo pinfo = MouseInfo.getPointerInfo();
    Point p = pinfo.getLocation();
    r.mouseMove((int)p.getX()+x, (int)p.getY()+y);
  }
  
  void clickMouse(int whichType) {
    r.mousePress(whichType);
    r.mouseRelease(whichType);
    r.waitForIdle();
  }
  </code></pre>