Firmata digitalRead() returns bad values with P2D?

Greetings.

I am trying to use Processing to read analog values from my Arduino using Firmata and the Processing Firmata libraries.

It seems to mostly work, but there are troubling random “spikes” in the data. More worrisome, the spikes seem to be a bug, since the contract for Arduino.analogRead() says that it would return values between 0 and 1023, and the spikes are values outside of this range (often integers > 10,000 are returned).

Very oddly, this seems only to happen if I use the P2D renderer in Processing. :slight_smile: Bizzare.

I assume it must have something to do with flow control and serial overflows.

Does anybody have any insight into this? Thanks so much for your help!

I am using the StandardFirmata sketch, with the following processing code:

import processing.serial.*;
import cc.arduino.*;

/**
 * The connection to the Arduino
 **/
Arduino arduino;

void setup()
{
    // Connect via Firmata
    arduino = new Arduino(this, Arduino.list()[0]);

    // set the size of the window to 20% of the screen
    size(screen.width*2/10, screen.height*2/10, P2D);
}

void draw()
{    
      delay(5);

      for (int i=0; i<6; i++) {
          int v = arduino.analogRead(i);
          if (v < 0 || v > 1023)
            System.out.println("Got bad value a"+i+": "+v);
      }
    }
}

In my console, I get this:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
Got bad value a2: 1282
Got bad value a4: 1154
Got bad value a2: 1282
Got bad value a4: 1154
Got bad value a1: 15617
Got bad value a3: 15617
Got bad value a1: 15617
Got bad value a3: 15617
Got bad value a0: 15745
Got bad value a1: 15617
Got bad value a2: 15617
Got bad value a3: 15617
Got bad value a4: 15617
Got bad value a1: 14849
...