Categories
Interaction Lab

Recitation 10 :Image & Video

 Part 1: Media controller

I decided to make the image of a hunter for our final project and control it with the variable resistors. So I made the code of the hunter. 

void drawMan(float u, float v, float s, color c){
   fill(255);
  noStroke();
  circle(u-450,v-450,s-20);
  
  ellipse(u-450,v-415,s+5,s);
  ellipse(u-433,v-390,s-30,s-40);
  ellipse(u-465,v-390,s-30,s-40);
  
  ellipse(u-475, v-410, s-40, s-20);
  ellipse(u-425, v-425, s-20, s-40); 
  
  
  fill(0);
  noStroke();
  stroke(0);
  line(u-455,v-450,u-445,v-450);
  circle(u-455,v-450,s-45);
  circle(u-445,v-450,s-45);
 
  
  fill(0);
  noStroke();
  rect(u-413,v-450,s-45,s+10);
  rect(u-420,v-450,s-30,s-40);
  rect(u-420,v-470,s-45,s-20);
  rect(u-413,v-470,s-45,s-20);
  rect(u-405,v-470,s-45,s-20);
  
}

I forgot that I need to use an image at that time and change the numbers into variables. And then I realized this problem and save it as a picture.

 

Here is the code.

Arduino

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

void loop() 
{
  // to send values to Processing assign the values you want to send
  //this is an example
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);


  // send the values keeping this format
  Serial.print(sensor1);
  Serial.print(",");  // put comma between sensor values
  Serial.print(sensor2);
  Serial.println(); // add linefeed after sending the last sensor value

  // too fast communication might cause some latency in Processing
  // this delay resolves the issue.
  delay(100);

  // end of example sending values
}

Processing

PImage hunter; 
import processing.serial.*;
int NUM_OF_VALUES_FROM_ARDUINO = 2;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int sensorValues[];      /** this array stores values from Arduino **/

String myString = null;
Serial myPort;
void setup(){
  size(600,600);
  background(237,222,139);
  setupSerial(); 
  imageMode(CENTER);
}
void draw(){
  background(237,222,139);
   getSerialData();
  printArray(sensorValues);
 float val1 = sensorValues[0];
 float val2 = sensorValues[1];
 hunter = loadImage("hunter.png");
 hunter.resize(90,90);
 
 image(hunter,val1,val2);

}

void setupSerial() {
  //printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 3 ], 9600);

  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_FROM_ARDUINO];
}

void getSerialData() {
  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_FROM_ARDUINO) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

 

Part 2: Musical Instrument

At first, I didn’t understand the requirement of the exercise so I asked LA for help. I knew that the functions of envelope and difference are necessary but I don’t know how to use them. I spent a long time solving this problem. 

Code

import processing.video.*; 
import processing.sound.*;
TriOsc triOsc;
Env env;
String[] cameras = Capture.list();
Capture cam;
float attackTime = 0.006;
float sustainTime = 0.002;
float sustainLevel = 0.3;
float releaseTime = 0.4;
int size = 20;
float r, prer;
void setup() {
  size(640, 480);
  cam = new Capture(this, cameras[0]);
  cam.start();
  triOsc = new TriOsc(this); 
  env  = new Env(this);  
}
void draw() {
  if (cam.available()) {
   cam.read(); 
  } 
 
  for (int x=0; x<cam.width; x=x+size) {
     for (int y=0; y<cam.height; y=y+size) {
       color c=cam.get(x,y);
       fill(c);
       noStroke();
       rect(x,y,size,size);
     }
}
  color c = cam.get(width/2, height/2); 
  r = red(c);
  float difference = abs(r-prer);
  if (difference>10) {
    triOsc.play();
    triOsc.freq(map(r, 0, 255, 100, 600));
    env.play(triOsc, attackTime, sustainTime, sustainLevel, releaseTime);
}
prer = r;
}

 

 

 

Leave a Reply

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