nick /224 /photocell_transmit


/* by nick lally and nik hanselmann
with help from Jeff Gray, ITP: http://itp.nyu.edu/physcomp/sensors/Code/ThreeSensorsCallResponseHumanReadable
*/
PFont font;
int counter = 0;
float last_value;

import processing.serial.*;

float val;
Serial port;                         // The serial port
String serialInString = "";          // Where we'll put what we receive
boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
  size(400,200);  // Stage size

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());
  port = new Serial(this, Serial.list()[2], 9600);

  font = loadFont("HelveticaNeue-12.vlw");
        textFont(font);
}

void draw() {
  //println(val);
  fill(val);
  rect(0, 0, width, height);

  counter--;

  if(counter <= 0){
    counter = 5;
    last_value = val;
    String[] results =
       loadStrings("http://transmogrify.me/lounge.php?v=" + val);
  }
  fill(0,0,255);
  text("Sensor 1 transmitting", 20,20);
  text("last transmission:", 20, 45);
  text("penultimate transmission:", 160,45);
  text("value: " + val, 20, 60);
  text("value: " + last_value, 160,60);
  text("time until next transmission: " + counter, 20, 140);
  text("PLEASE DO NOT CLOSE", 20, 170);
  delay(1000);
}

void serialEvent(Serial port) {
  // read a byte from the serial port:
  int inByte = port.read();
  // if this is the first byte received,
  // take note of that fact. Otherwise, add it to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      port.clear();          // clear the serial port buffer
      firstContact = true;
      port.write('A');
    }
  // if firstContact has been made, check to see if a full string has come in
  } else {
    if(inByte == '\n'){
      // split the serialInString into pieces of data
      String[] pieces = serialInString.split(",");

      // grab strings from array, and convert into ints
      val = Integer.parseInt(pieces[1]);
      val = map(val, 0, 950, 0, 255);
      //ypos = Integer.parseInt(pieces[3]);
      //fgcolor = Integer.parseInt(pieces[5]);

      // print the values (for debugging purposes only):
      //println(val + "\t");

      // Send a capital A to request new sensor readings:
      port.write('A');
      // Reset serialInString
      serialInString = "";
    } else {
      serialInString += (char)inByte;
    }
  }
}


Page Details
Contact DANM  |  Digital Arts and New Media  |  Arts Division  |  Grad Division
login