By Grace Kim

2-Axis Accelerometer Skirt Visualization

From Tom Igoe's Datalogger
and Tom Igoe's TwoByteReceiver

Back to Accelerometer Report

import processing.serial.*;
Serial port;                         // The serial port
int[] serialInArray = new int[4];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
boolean firstContact = false; 
       // Whether we've heard from the microcontroller
int xTilt;
int yTilt;
byte[] inBuffer = new byte[2];
int minyTilt = 185; //variables used to trigger lights
int midyTilt = 220;
int maxyTilt = 270;


void setup() {
  size(900, 600);  // Stage size
  noStroke();      
  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());
  // From this debugging statement, I saw that my adapter was listed 3rd
  port = new Serial(this, Serial.list()[2], 9600);
  port.write(65);    // Send a capital A to start the microcontroller sending
  background(255, 250, 205);
}

void draw() {
  // Get any new serial data
  while (port.available() > 0) {
    serialEvent();
    // Note that we heard from the microntroller:
    firstContact = true;

  }
  if (firstContact == false) {
    delay(300);
    port.write(65);
  }
}

void drawGraph () {
  int valueToGraph = 0;
  valueToGraph = yTilt;
  smooth();
  strokeWeight(0.75);
  fill(0,0,128);
  stroke(0,0,128);

  line(10, 10, (width-10), 10);
  line((width-10), 10, (width-10), height-10);
  line((width-10), height-10, 10, height-10);
  line(10, height-10, 10, 10);

  //Curve takes 4 inputs: curve(x1, y1, x2, y2, x3, y3, x4, y4);
  //x1, y1, z1 	  	int or float: coordinates for the first ancho
  //x2, y2, z2 	  	int or float: coordinates for the first point
  //x3, y3, z3 	  	int or float: coordinates for the second point
  //x4, y4, z4 	  	int or float: coordinates for the second anchor

  line((width/2 - 75), 50, (width/2 - 75), 40); //draw waistband of skirt
  line((width/2 + 75), 50, (width/2 + 75), 40);
  curve((width/2), 0, (width/2 - 75), 40, (width/2 + 75), 40, (width/2), 0); 
  curve((width/2), 0, (width/2 - 75), 50, (width/2 + 75), 50, (width/2), 0);

  curve((width/2 -75), (height/2 + 100), (width/2 + 75), 50, ((width/2+valueToGraph)), (height-100), (width/2 - 75), (height/2 + 100)); //draw skirt, second x value is determined by the amount that y tilts
  curve((width/2 + 75), (height/2 + 100), (width/2 -75), 50, ((width/2-valueToGraph)), (height-100), (width/2 + 75), (height/2 + 100)); 
  curve((width/2), (height-300), ((width/2+valueToGraph)), (height-100), ((width/2-valueToGraph)), (height-100), (width/2), (height-300));

  strokeWeight(.75); // draw lights
  fill(255, 250, 205);
  stroke(0,0,128);
  ellipse((width/2), (height - 150), 20, 20);
  ellipse((width/2), (height - 200), 20, 20);
  ellipse((width/2), (height - 250), 20, 20);
  ellipse((width/2), (height - 150), 15, 15);
  ellipse((width/2), (height - 200), 15, 15);
  ellipse((width/2), (height - 250), 15, 15);

  if((valueToGraph > minyTilt)&&(valueToGraph<=midyTilt)){
  fill(129,229, 17);
  stroke(0,0,128);
  ellipse((width/2), (height - 150), 15, 15);
  fill(255, 250, 205);
  stroke(0,0,128);
   ellipse((width/2), (height - 200), 15, 15);
  ellipse((width/2), (height - 250), 15, 15);
  }

  if((valueToGraph > midyTilt)&&(valueToGraph<=maxyTilt)){
  fill(129,229, 17);
  stroke(0,0,128);
  ellipse((width/2), (height - 150), 15, 15);
  ellipse((width/2), (height - 200), 15, 15);
   fill(255, 250, 205);
  stroke(0,0,128);
  ellipse((width/2), (height - 250), 15, 15);
  }

  if(valueToGraph > maxyTilt){
  fill(129,229, 17);
  stroke(0,0,128);
  ellipse((width/2), (height - 150), 15, 15);
  ellipse((width/2), (height - 200), 15, 15);
  ellipse((width/2), (height - 250), 15, 15);
  }
}


void serialEvent() {
  processByte((int)port.read());
}

void processByte(int inByte) {
  // Add the latest byte from the serial port to array:
  serialInArray[serialCount] = inByte;
  serialCount++;
  // If we have 4 bytes:
  if (serialCount > 3 ) {
    xTilt = (serialInArray[0] * 256) + serialInArray[1];
    yTilt = (serialInArray[2] * 256) + serialInArray[3];

    println("xTilt is " + xTilt + " -- yTilt is " + yTilt);
    background(255, 250, 205);
    drawGraph();

    //VERY IMPORTANT! 
    delay(100);
    // Send a capital A to request new sensor readings:
    port.write(65);
    // Reset serialCount:
    serialCount = 0;
  }
}