/*
  Accelerometer Tilt
  Language: Processing
 
  Takes the values in serially from an accelerometer 
  attached to a microcontroller and uses them to set the 
  attitude of a disk on the screen.
*/

import processing.serial.*;      // import the serial lib

int graphPosition = 0;           // horizontal position of the graph
int[] vals = new int[2];         // raw values from the sensor
int[] maximum = new int[2];      // maximum value sensed
int[] minimum = new int[2];      // minimum value sensed
int[] range = new int[2];        // total range sensed
float[] attitude = new float[2]; // the tilt values
float position;                  // position to translate to 

Serial myPort;                   // the serial port
boolean madeContact = false;     // whether there's been serial data in

void setup () {
  // draw the window:
  size(400, 400, P3D); 

  // set the background color:
  background(0);
  // set the maximum and minimum values:
  for (int i = 0; i < 2; i++) {
    maximum[i] = 600;
    minimum[i] = 200;
    // calculate the total current range:
    range[i] = maximum[i] - minimum[i];
  }
  // calculate position:
  position = width/2;

  // create a font with the second font available to the system:
  PFont myFont = createFont(PFont.list()[2], 18);
  textFont(myFont);

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

  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // only generate a serial event when you get a return char:
  myPort.bufferUntil('\r');

  // set the fill color:
  fill(90,250,250);
}

void draw () {
  // clear the screen:
  background(0);

  // print the values:
  text(vals[0] + " " + vals[1], -30, 10);

  // if you've never gotten a string from the microcontroller,
  // keep sending carriage returns to prompt for one: 
  if (madeContact == false) {
    myPort.write('\r');
  }

  // set the attitude:
  setAttitude();
  // draw the plane:
  tilt();
}
void setAttitude() {
  for (int i = 0; i < 2; i++) {
    // calculate the current attitude as a percentage of 2*PI, 
    // based on the current range:
    attitude[i] = (2*PI) * float(vals[i] - minimum[i]) /float(range[i]);
  }
}

void tilt() {
  // translate from origin to center:
  translate(position, position, position);

  // X is front-to-back:
  rotateX(-attitude[1]);
  // Y is left-to-right:
  rotateY(-attitude[0] - PI/2);

  // set the fill color:
  fill(90,250,250);
  // draw the rect:  
  ellipse(0, 0, width/4, width/4);
  // change the fill color:
  fill(0);
  // Draw some text so you can tell front from back:
  // print the values:
  text(vals[0] + " " + vals[1], -30, 10,1);

}

// serialEvent  method is run automatically by the Processing applet
// whenever the buffer reaches the  byte value set in the bufferUntil() 
// method in the setup():

void serialEvent(Serial myPort) { 
  // if serialEvent occurs at all, contact with the microcontroller
  // has been made:
  madeContact = true;
  // 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, ','));
    // if you received all the sensor strings, use them:
    if (sensors.length >= 2) {
      vals[0] = sensors[0];
      vals[1] = sensors[1];   

      // send out the serial port to ask for data:
      myPort.write('\r');
    }
  }
}

Sorry, the comment form is closed at this time.

   
© 2012 Making Things Talk Suffusion theme by Sayontan Sinha