Recitation 10: Image & Video

#1 Media controller

For this exercise, I wanted to use a potentiometer to control the tint of the video. In terms of circuitry, I just need to build a simple potentiometer circuit, and after practicing this semester, I found that I can do it easily. The code is based on the serial communication, movie_class, and movie_tint examples. The serial communication went well.

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


void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensor1 = analogRead(A0);
  Serial.print(sensor1);
  Serial.println(); 
  delay(100);
}

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

import processing.serial.*;
import processing.video.*;

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

String myString = null;
Serial myPort;
Movie myMovie;

void setup() {
  size(352,288);
 myMovie = new Movie(this, "100.mp4");
  myMovie.play();
  setupSerial();
}

void draw() {
  getSerialData();
  printArray(sensorValues);
 if (myMovie.available()) {
    myMovie.read();
  }
  float m = map(sensorValues[0], 0, 1023, 0, 255);
  println (m);
  tint(m, 100, 240); 
  image(myMovie, 0, 0);
}
  
void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 2 ], 9600);
  myPort.clear();
  myString = myPort.readStringUntil( 10 ); 
  myString = null;

  sensorValues = new int[NUM_OF_VALUES_FROM_ARDUINO];
}

void getSerialData() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); 
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES_FROM_ARDUINO) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

# 2 Musical Instrument

I wrote the code based on the capture example while adding code for Pixel and SinOsc. I managed to write nested for loops. But my camera kept not displaying the image. With the help of my roommate, I found that I had chosen the wrong code for the camera, and after correcting it in time, I finally succeeded. And before that, because I used the capture example, at first it was showing very clear images instead of pixels. So I watched the recorded video of the class again and made the changes and it was done.

import processing.video.*;
String[] cameras = Capture.list();
Capture cam;

import processing.sound.*;
SinOsc sine;
float attackTime = 0.002;
float sustainTime = 0.006;
float sustainLevel = 0.4;
float releaseTime = 0.5;

int s = 10;
float r, pr; 
void setup() {
  size(640, 480);
  printArray(cameras);
  cam = new Capture(this, cameras[1]);
  cam.start();

  sine = new SinOsc(this);
 
}

void draw() {
  if (cam.available()) {
    cam.read();
  }
  
   color c = cam.get(width/2, height/2);
  r = red(c);
    float difference = abs(r-pr);
    
  for (int x=0; x<width; x=x+s) {
    for (int y=0; y<height; y=y+s) {
        // get the pixel color
    color f = cam.get(x, y); 
    // draw a circle with the color
     //int size = int( random(1, 20));
       fill(f);
       noStroke();
      rect(x, y, s, s);
    }
  }

if (difference>10) {
    sine.play();
    sine.freq(map(r, 0, 255, 100, 800));
 
  }
  
  //image(cam, 0, 0);
  //sine.play();
    pr=r;
}

Leave a Reply

Your email address will not be published. Required fields are marked *