Lilypad + Lilypad Accelerometer + Processing

Hi all,
I’m having trouble with my code for the lilypad accelerometer (adxl330). I want to import the real time sensor data from the accelerometer into Processing so that I can “draw” with it. In my Processing code, do I import the Lilypad sensor data as “arduino.analogRead()” ? Here is a snippet:

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

int sensorPinX = 0;
int sensorPinY = 1;
int sensorPinZ = 2;  
int[] xArray = new int[8];
int[] yArray = new int[8];
int[] zArray = new int[8];
int [] sensorValueX = new int[50];
int [] sensorValueY = new int[50];
int [] sensorValueZ = new int[50];

int BAUDRATE = 9600; 
char DELIM = ','; 

void setup() { 
  arduino = new Arduino(this, Arduino.list()[0], 9600);

  int i;
  size(1440, 900); 
  noStroke();
  noCursor();
  for (i = 0; i < sensorValueX.length; i++) {
      sensorValueX[i] = 0;
      sensorValueY[i] = 0;
      sensorValueZ[i] = 0;
  }
} 

void draw() {
  int i;
  int xpos;
  int ypos;
  int zpos;

  background(0);
  noStroke();

  xpos = arduino.analogRead(sensorPinX); 
  addEntry(xpos,xArray);
  ypos = arduino.analogRead(sensorPinY);
  addEntry(ypos,xArray);
  zpos = arduino.analogRead(sensorPinZ);
  addEntry(zpos,zArray);

  print(xpos);
  print(" ");
  print(ypos);
  print(" ");
  println(zpos);

  addEntry(getAverage(xArray),sensorValueX);  
  addEntry(getAverage(yArray),sensorValueY);
  addEntry(getAverage(zArray),sensorValueZ);

In my Processing code, do I import the Lilypad sensor data as "arduino.analogRead()"

Yes, you do. But, you want to create a sketch on the Arduino first, to confirm that the accelerometer is connected correctly, and providing accurate data, before it makes any sense to try to use the data in another system.

int BAUDRATE = 9600;
  arduino = new Arduino(this, Arduino.list()[0], 9600);

First, typically, all capital letters are reserved for #define'd terms, so BAUDRATE isn't correct. Second, you never use BAUDRATE, so why define it?

Yes, I've tested the data in an Arduino sketch before working in Processing with it.

The baudrate point is a good one -- that is some left over code from an adjustment I made recently.

I'm having trouble with my code for the lilypad accelerometer (adxl330).

Yes, I've tested the data in an Arduino sketch before working in Processing with it.

So, what trouble ARE you having?

hahah ;)

Well, I keep getting 0s when reading the data in Processing, even if in my Arduino sketch I'm getting proper data! So, I was suspecting that I was not calling the Arduino library functions (ie. arduino.analogRead()), or maybe this line is incorrect: arduino = new Arduino(this, Arduino.list()[0], 9600);

or maybe there is something I am overlooking completely?

sorry, that should read that I was not calling the Arduino library functions correctly...

If you are able to communicate with the Arduino at all, the connection to the serial port is correct.

I presume you are running the standard Firmata sketch on the Arduino. Is that a safe assumption?

What kind of Arduino is it? I know that there are some problems with analog port data collection on some Arduino versions.

You could skip the Firmata path, and write your own protocol for getting analog data. It’s not terribly difficult. Send a command like to read analog pin 3. That’s easy to parse, and easy to implement on the Arduino. Outputting the data back to Processing as a string is trivial. Outputting as two bytes is easy, too, but more difficult to re-assemble in Processing.

I'm using the Lilypad Mega 168 with a Lilypad Accelerometer (ADXL330).

This is the Arduino code that works:

int sensorPinX = 0;
int sensorPinY = 1;
int sensorPinZ = 2;      
int sensorValueX;
int sensorValueY;
int sensorValueZ;  
         
void setup()  {        
         pinMode(sensorPinX, INPUT); 
         pinMode(sensorPinY, INPUT);
         pinMode(sensorPinZ, INPUT);      
         Serial.begin(9600);       
}        
         
void loop() {        
          sensorValueX = analogRead(sensorPinX)*10;       
          sensorValueY = analogRead(sensorPinY)*10;
          sensorValueZ = analogRead(sensorPinZ)*10;
         
          Serial.print(sensorValueX,DEC); 
          Serial.print(",");
          Serial.print(sensorValueY, DEC);
          Serial.print(",");
          Serial.println(sensorValueZ, DEC);      
      
          delay(500);       
}

The Processing sketch has worked intermittently, which is to say that today I could read XYZ coords in the serial monitor in Processing, however it was brief, and then went back to zeros. Might be a hardware problem.