Visualizing Accelerometer data in Processing

Hello All,

I am using the following code to access accelerometer data from Arduino and need to plot a real time graph on Processing.

Arduino Code:

#include “NAxisMotion.h” //Contains the bridge code between the API and the Arduino Environment
#include <Wire.h>

NAxisMotion mySensor; //Object that for the sensor
unsigned long lastStreamTime = 0; //To store the last streamed time stamp
const int streamPeriod = 40; //To stream at 25Hz without using additional timers (time period(ms) =1000/frequency(Hz))
bool updateSensorData = true; //Flag to update the sensor data. Default is true to perform the first read before the first stream

void setup() //This code is executed once
{
//Peripheral Initialization
Serial.begin(115200); //Initialize the Serial Port to view information on the Serial Monitor
I2C.begin(); //Initialize I2C communication to the let the library communicate with the sensor.
//Sensor Initialization
mySensor.initSensor(); //The I2C Address can be changed here inside this function in the library
mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
//Setting to MANUAL requires lesser reads to the sensor
mySensor.updateAccelConfig();
updateSensorData = true;
Serial.println();
Serial.println(“Default accelerometer configuration settings…”);
Serial.print("Range: ");
Serial.println(mySensor.readAccelRange());
Serial.print("Bandwidth: ");
Serial.println(mySensor.readAccelBandwidth());
Serial.print("Power Mode: ");
Serial.println(mySensor.readAccelPowerMode());
Serial.println(“Streaming in …”); //Countdown
Serial.print(“3…”);
delay(1000); //Wait for a second
Serial.print(“2…”);
delay(1000); //Wait for a second
Serial.println(“1…”);
delay(1000); //Wait for a second
}

void loop() //This code is looped forever
{
if (updateSensorData) //Keep the updating of data as a separate task
{
mySensor.updateAccel(); //Update the Accelerometer data
mySensor.updateLinearAccel(); //Update the Linear Acceleration data
mySensor.updateGravAccel(); //Update the Gravity Acceleration data
mySensor.updateCalibStatus(); //Update the Calibration Status
updateSensorData = false;
}
if ((millis() - lastStreamTime) >= streamPeriod)
{
lastStreamTime = millis();

//Serial.print("Time: ");
//Serial.print(lastStreamTime);
//Serial.print("ms ");

//Serial.print(" aX: ");
Serial.print(mySensor.readAccelX()); //Accelerometer X-Axis data
//Serial.print("m/s2 ");

Serial.println();

updateSensorData = true;
}
}

The processing code, which I try to modify is following:

import processing.serial.*;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph

void setup () {
// set the window size:
size(400, 300);

// List all the available serial ports
println(Serial.list());

// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 115200);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(’\n’);
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(’\n’);

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
println(inByte);
// draw the line:
stroke(inByte,0,255);
noFill();
line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}

What I want to try as a first step is just send one stream of value for instance “aX” and plot it with respect to time in real time. Even though I see the values in Arduino, I stream something else in Processing, and above all the Graphic window is absolutely blank.
Can anyone tell me, where the problem lies?

Even though I see the values in Arduino, I stream something else in Processin

You can't see the values in Arduino. You might me able to see the values in the Serial Monitor application that connects to the serial port that the Arduino is sending data to.

But, if you are doing that, then Processing can't connect to the same serial port, so it is not surprising that it can't get any data.