topical media & game development

talk show tell print

graphic-processing-algorithm-Appendix-p332-x2-p332-x2.pde / pde



  import processing.serial.*;
  PrintWriter output;
  String buff = “”;
  int val = 0;
  int NEWLINE = 10;
  Serial port;
  void setup(){
    // Uses the first available port
    port = new Serial(this, “COM6”, 9600);
    output = createWriter(“positions.txt”);
  }
  void draw(){
    while (port.available() > 0)
      serialEvent(port.read());
    background(val);
    println(val);
    output.println(val);
  }
  void keyPressed() {
    output.flush();
    output.close(); // Closes the file
    exit();
  }
  void serialEvent(int serial) {
    if(serial != NEWLINE) {
      buff += char(serial);
    } 
    else {
      buff = buff.substring(0, buff.length()-1);
      // Parse the String into an integer
      val = Integer.parseInt(buff)/4;
      println(val);
      // Clear the value of “buff”
      buff = “”;
    }
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.