We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Arduino + Accelerometer and Graphing
Page Index Toggle Pages: 1
Arduino + Accelerometer and Graphing (Read 4398 times)
Arduino + Accelerometer and Graphing
May 4th, 2010, 3:26am
 
hi guys,

I'm after some help...

I'm making a accelerometer meter using an arduino and an accelerometer based on the MMA7260 chip.

I want to make a Processing program that will display the values sent in by the arduino.

I have made the basic processing program for this.

I have attached a basic layout of the processing program below.

What I want to do is make a square on the right which graphs the X and Y values on a continuous basis. I want the x-axis to be horizontal and the y-axis to be vertically graphed.

I can get one axis to graph but not the other....

Also I want the z-axis on a separate window below the other two.. this can be of a rectangular shape...

The data from the arduino is sent using a string.. I can split the string fine in processing, I'm just stuck on getting the graphing to work..

any help would be much appreciated...

Display Screen (It's not finished just playing with placement):
...

This is my Processing Code so far:

Code:
import processing.serial.*;
import processing.opengl.*;


Serial myPort;    // The serial port:

int baudRate = 115200; //make sure this matches the baud rate in the arduino program.

int lf = 10;  // ASCII linefeed

PFont myFont; // The display font:

int fontSize = 12;

int xAxisVal , yAxisVal , zAxisVal;
int sxAxisVal , syAxisVal , szAxisVal;
int xGForce , yGForce , zGForce;

int xAxisMin = 1023, xAxisMax = 0, yAxisMin = 1023, yAxisMax = 0, zAxisMin = 1023, zAxisMax = 0;
int sxAxisMin = 255, sxAxisMax = 0, syAxisMin = 255, syAxisMax = 0, szAxisMin = 255, szAxisMax = 0;

int width = 700;
int height = 350;

void setup() {
 size(width, height);
 smooth();
 
 // List all the available serial ports:
 println(Serial.list());

 // I know that the first port in the serial list on my machine
 // 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()[1], baudRate);

 myPort.bufferUntil(lf);

 //set up the display text which will display the data recieved via serial
 myFont = createFont("Ti73ProgCodeBold", fontSize);
 textFont(myFont);
 noLoop(); //don't redraw unless serial data so draw() will not loop.
 
}

void draw(){
 
 int   titleSpacing = 20;
 background(200); //nice light gray here

 fill(#030200);
 text("Raw Accelerometer Values:", 12,20);
 
 fill(#F21616);
 text("X - Axis: " + xAxisVal, 20,40); //this is the information recieved from the serial port.
 text("Min: " + xAxisMin ,20,60);
 text("Max: " + xAxisMax ,20,80);
 
 fill(#0006FF);
 text("Y - Axis: " + yAxisVal, 20,120);
 text("Min: " + yAxisMin ,20,140);
 text("Max: " + yAxisMax ,20,160);
 
 fill(#009512);
 text("Z - Axis: " + yAxisVal, 20,200);
 text("Min: " + zAxisMin ,20,220);
 text("Max: " + zAxisMax ,20,240);
 
 stroke(255);
 fill(#FFFCFC);
}


void serialEvent(Serial myPort) {
 // read the serial buffer:
 String myString = myPort.readStringUntil('\n');
 // if you got any bytes other than the linefeed:
 if (myString != null) {

   myString = trim(myString);

   // split the string at the commas
   // and convert the sections into integers:
   int sensors[] = int(split(myString, ','));

   xAxisVal = sensors[0];
   if (sensors[0] < xAxisMin){
     xAxisMin = sensors[0];
   }
   else if (sensors[0] > xAxisMax){
     xAxisMax = sensors[0];
   }        

   yAxisVal = sensors[2];
   if (sensors[2] < yAxisMin){
     yAxisMin = sensors[2];
   }
   else if (sensors[2] > yAxisMax){
     yAxisMax = sensors[2];
   }    

   zAxisVal = sensors[4];
   if (sensors[4] < zAxisMin){
     zAxisMin = sensors[4];
   }
   else if (sensors[4] > zAxisMax){
     zAxisMax = sensors[4];
   }
   
   sxAxisVal = sensors[1];
   if (sensors[1] < sxAxisMin){
     sxAxisMin = sensors[1];
   }
   else if (sensors[1] > sxAxisMax){
     sxAxisMax = sensors[1];
   }    
   
   syAxisVal = sensors[3];
    if (sensors[3] < syAxisMin){
     syAxisMin = sensors[3];
   }
   else if (sensors[3] > syAxisMax){
     syAxisMax = sensors[3];
   }
   szAxisVal = sensors[5];
   if (sensors[5] < szAxisMin){
     szAxisMin = sensors[5];
   }
   else if (sensors[5] > szAxisMax){
     szAxisMax = sensors[5];
   }
   
}
   // add a linefeed after all the sensor values are printed:
   redraw();
}



Thanks,
Re: Arduino + Accelerometer and Graphing
Reply #1 - May 6th, 2010, 3:09am
 
ok guys, I got some of the program to work...

But, I can't get the line to draw properly. It draws like the figure below. It just move across like that. I want it to draw like a line graph...

Any help?????
...

This is the processing code:
It's a bit messy, I haven;t cleaned it up yet...

Code:
import processing.serial.*;
import processing.opengl.*;


Serial myPort;    // The serial port:

int baudRate = 115200; //make sure this matches the baud rate in the arduino program.

int lf = 10;  // ASCII linefeed

PFont myFont; // The display font:

int fontSize = 14;

float xAxisVal , yAxisVal , zAxisVal;
float sxAxisVal , syAxisVal , szAxisVal;
float xGForce , yGForce , zGForce;
float xAxisVol , yAxisVol , zAxisVol , mainVol;
float xAxisMinVol = 5 , xAxisMaxVol = 0 , yAxisMinVol = 5 , yAxisMaxVol = 0 , zAxisMinVol = 5 , zAxisMaxVol = 0;
float xVal , yVal , zVal;
float pyVal;

int xPos = 20;

int width = 820;
int height = 380;

void setup() {
 size(width, height);
 smooth();
 
 // List all the available serial ports:
 println(Serial.list());

 // I know that the first port in the serial list on my machine
 // 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()[1], baudRate);

 myPort.bufferUntil(lf);

 //set up the display text which will display the data recieved via serial
 myFont = createFont("Ti73ProgCodeBold", fontSize);
 textFont(myFont);
 noLoop(); //don't redraw unless serial data so draw() will not loop.

}

void draw(){
 smooth();
 background(200); //nice light gray here
 
 fill(#030000);
 text("Main Voltage: " + mainVol , 580,300);
 text(" Volts" , 725, 300);
 fill(#FF0808);
 
 text("X - Axis Voltage: " + xAxisVol + " Volts", 20,320);
 text("  - Axis Min: " + xAxisMinVol + " Volts" ,20,340);
 text("  - Axis Max: " + xAxisMaxVol + " Volts" ,20,360);
 
 fill(#0513FF);
 text("Y - Axis Voltage: " + yAxisVol + " Volts", 300,320);
 text("  - Axis Min: " + yAxisMinVol + " Volts" ,300,340);
 text("  - Axis Max: " + yAxisMaxVol + " Volts" ,300,360);  
 
 fill(#008127);
 text("Z - Axis Voltage: " + zAxisVol + " Volts", 580,320);
 text("  - Axis Min: " + zAxisMinVol + " Volts" ,580,340);
 text("  - Axis Max: " + zAxisMaxVol + " Volts" ,580,360);

 noFill();
 tint(255, 128);
 stroke(#050505);
 strokeWeight(2.0);
 rect(20,20, 780, 260);
 
 stroke(#030303);
 noFill();
 smooth();
 strokeWeight(3.0);
 strokeJoin(ROUND);
 beginShape();
 line(10, 305, 810, 305);

 line(560, 285, 560, 370);
 line(260, 285, 260, 370);
 endShape();
/*  
 stroke(255);
 fill(#FFFCFC);

 noFill();

 smooth();
 strokeWeight(1.0);

 grid(20,20,780,260,10,#030303);
*/
  stroke(#FF0303);
  strokeWeight(2.0);
  line(xPos , 280 , xPos , (260 - xVal));
  pyVal=(260 - xVal);
  stroke(#0017FF);
  strokeWeight(2.0);
  line(xPos + 5 , 280 , xPos + 5 , (260 - yVal));
  stroke(#046400);
  strokeWeight(2.0);
  line(xPos + 10 , 280 , xPos + 10 , (260 - zVal));
 
  if(xPos >= 780){
    xPos=20;
  }
  else{
    xPos++;
  }


}

void grid(int x, int y, int w, int h, int n, color c) {
stroke(c);
for (int ix=x; ix <= x+w; ix += n) {
  line(ix, y, ix, y+h);
}

for (int iy=y; iy <= y+h; iy += n) {
  line(x, iy, x+w, iy);
}
}


void serialEvent(Serial myPort) {
 // read the serial buffer:
 String myString = myPort.readStringUntil('\n');
 // if you got any bytes other than the linefeed:
 if (myString != null) {

   myString = trim(myString);

   // split the string at the commas
   // and convert the sections into integers:
  float sensors[] = float(split(myString, ','));
 
  xVal = map(sensors[0],0,1023,0,255);
  yVal = map(sensors[2],0,1023,0,255);
  zVal = map(sensors[4],0,1023,0,255);

   mainVol  = sensors[7];
     
   xAxisVol = sensors[8];
   if (sensors[8] < xAxisMinVol){
     xAxisMinVol = sensors[8];
   }
   else if (sensors[8] > xAxisMaxVol){
     xAxisMaxVol = sensors[8];
   }
   
   yAxisVol = sensors[9];
   if (sensors[9] < yAxisMinVol){
     yAxisMinVol = sensors[9];
   }
   else if (sensors[9] > yAxisMaxVol){
     yAxisMaxVol = sensors[9];
   }
   
   zAxisVol = sensors[10];
   if (sensors[10] < zAxisMinVol){
     zAxisMinVol = sensors[10];
   }
   else if (sensors[10] > zAxisMaxVol){
     zAxisMaxVol = sensors[10];
   }    

}

   // add a linefeed after all the sensor values are printed:
   redraw();
}

Re: Arduino + Accelerometer and Graphing
Reply #2 - May 6th, 2010, 8:40pm
 
Chiphacker.com would be the best place to get this question answered.
Re: Arduino + Accelerometer and Graphing
Reply #3 - May 23rd, 2010, 12:40am
 
you need to do something like storing your previous values in an array or ringbuffer-type device so you can draw the previous AND the latest sample.

because
 background(200); //nice light gray here
erases the previous lines! Smiley

or, you could just erase the areas of the screen you're updating, using a filled rect, and draw the new values there. and don't forget to erase any updating text too.  and comment out 'background.'
Page Index Toggle Pages: 1