Pages: [1]   Go Down
Author Topic: Arduino + Accelerometer and Graphing  (Read 697 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

But, I can't get the line to draw properly.

The program below just draws a vertical line from the bottom to the sensor value and it moves across the screen. It doesn't use the previous value and draw the line from the previous values...

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();
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what the program displays... This moves across the screen.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It doesn't use the previous value and draw the line from the previous values...

Where are you recording the previous values? Where are you telling it to use the previous values (hint: you aren't, because you are not recording the previous values)?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did that just to try out so I added the following, just after the line was drawn:

Code:
  pxVal = (260 - xVal);

then used that as the starting ypos for the line. But, when i did that it just gave a dot that moves according to the sensor value.

above was added just below:

Code:
line(xPos , pxVal , xPos , (260 - xVal));

Do I need to store the previous values in a array for it to work?
« Last Edit: May 06, 2010, 08:59:52 pm by SilverZed » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to draw the line from pxVal, pyVal to xVal, yVal. I don't see that you are doing that.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got a processing app the graphs acc data from the wii chuck attached to an arduino - you're welcome to it:

http://www.jfernsler.com/blog/?p=29
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

^ thanks for that. I got it working after some fiddling around using the original method.

I'll try your sketch as well. it looks pretty good. Nicely done.
Logged

Pages: [1]   Go Up
Jump to: