March 22, 2008

Graph 3 Values (as from an Accelerometer)

I threw together something for graphing the X Y Z values from the accelerometers, assuming we ever get them wired up correctly. My partner has the accelerometer right now, so I can’t test with it, so I’m just using input from the mouse for the 3 graphs. This is based on the Processing MouseSignals example and the Sensors Tutorial I mentioned previously. Code:

/**
* 3 Val Graph
* 
* Based off of the Mouse Signals Example and 
* http://protolab.pbwiki.com/TutorialSensors Tutorial
*
* hacked together by Ryan Galgon  3/22/2008
*/

int[] xvals;
int[] yvals;
int[] zvals;

int arrayIndex = 0;

void setup()
{
  size(256,256);
  xvals = new int[width];
  yvals = new int[width];
  zvals = new int[width];
}

void draw()
{
  background(102);
  
  for( int i = 1; i < width; i++ ) {
    xvals[i-1] = xvals[i];
    yvals[i-1] = yvals[i];
    zvals[i-1] = zvals[i];
  }
  
  /* Add new values to the end of the respective array */
   * you would get these values by:
   *      analogRead(xPin) / 4  (convert from 0..1024 to 0..255)
   */
  xvals[width-1] = mouseX; 
  yvals[width-1] = mouseY; 
  zvals[width-1] = (mouseX + mouseY)/2;
  
  fill(255);
  noStroke();
  
  for(int i = 1; i < width; i++ ) {
    stroke(#ff0000);
    point(i, xvals[i]/3);
    stroke(#00ff00);
    point(i, height/3+yvals[i]/3);
    stroke(#0000ff);
    point(i, 2*height/3+zvals[i]/3);
  }
}