Recitation 10: Making a Media Controller (Thomas Waugh)

 

Technology is completely the base of this assignment. Everything about it screams technology, I mean, it’s literally two pieces of software communicating with each other resulting in the manipulation of an image. I find this assignment to be particularly tech oriented as this is one of the first assignment to incorporate not only arduino and processing code but also outside media. This combination of things has really opened up a whole new world of possibilities. Before, with the limited tools available, there were less options when one wanted to get creative with their project. Now, however, with the ability to implement all these things together to create a project the possibilities for creativity are much, much greater than before.

// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing

/*
 * Based on the readStringUntil() example by Tom Igoe
 * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
 */

import processing.serial.*;

PImage gene;

String myString = null;
Serial myPort;


int NUM_OF_VALUES = 3;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;      /** this array stores values from Arduino **/

int filterNO;

void setup() {
  size(600,600);
  gene = loadImage("gene.jpeg");
  background(0);
  setupSerial();
}


void draw() {
  updateSerial();
  printArray(sensorValues);
  println();
  println();
  println();

  background(0);
  rectMode(CENTER);
  image(gene, mouseX-(sensorValues[1]/4), mouseY-(sensorValues[0]/4), sensorValues[1]/2,sensorValues[0]/2);
  
  if(sensorValues[2] < 150){
    filter(THRESHOLD);
  }
  
  if(sensorValues[2] > 150){
    if(sensorValues[2] < 300){
      filter(GRAY);
    }
  }
  
  if(sensorValues[2] > 300){
    if(sensorValues[2] < 450){
      filter(INVERT);
    }
  }
  
  if(sensorValues[2] > 450){
    if(sensorValues[2] < 600){
      filter(POSTERIZE, 80);
    }
  }
  
  if(sensorValues[2] > 600){
    if(sensorValues[2] < 750){
      filter(ERODE);
    }
  }
  
  if(sensorValues[2] > 750){   
    filter(DILATE);
  }
}


void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 2 ], 9600);
  // WARNING!
  // You will definitely get an error here.
  // Change the PORT_INDEX to 0 and try running it again.
  // And then, check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index number of the port.

  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = 'n'  Linefeed in ASCII
  myString = null;

  sensorValues = new int[NUM_OF_VALUES];
}



void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = 'n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}


// IMA NYU Shanghai
//// Interaction Lab
//// For sending multiple values from Arduino to Processing
//
//
//void setup() {
//  Serial.begin(9600);
//}
//
//void loop() {
//  int sensor1 = analogRead(A0);
//  int sensor2 = analogRead(A1);
//  int sensor3 = analogRead(A2);
//
//  // keep this format
//  Serial.print(sensor1);
//  Serial.print(",");  // put comma between sensor values
//  Serial.print(sensor2);
//  Serial.print(","); // add linefeed after sending the last sensor value
//  Serial.print(sensor3);
//  Serial.println();
//
//
//  // too fast communication might cause some latency in Processing
//  // this delay resolves the issue.
//  delay(100);
//}

Leave a Reply