Importing Lilypad Arduino sensor data (Lilypad accelerometer)
in
Integration and Hardware
•
3 years ago
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()" ? This program compiles fine, but gives me 0s in the serial monitor.
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()" ? This program compiles fine, but gives me 0s in the serial monitor.
- 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);
//SPHERE
for (i = 0; i < sensorValueX.length; i++) {
fill(i*5,i*10,255-i*5);
ellipse(width/2 + sensorValueX[i]*1.5, height/1.5 - sensorValueY[i]*1.5, i*2.5, i*2.5); //x, y, width, height
}
}
int getAverage(int valueArray[]){
int i;
int sum = 0;
int average;
for (i=0;i<valueArray.length;i++) {
sum += valueArray[i];
}
average = sum/valueArray.length;
return average;
}
void addEntry(int value, int valueArray[]) {
int i;
for (i=0;i<valueArray.length-1;i++) {
valueArray[i]=valueArray[i+1];
}
valueArray[valueArray.length-1] = value;
}
1