#Blog 18 Recitation 10: Image & Video

 Part 1: Media controller

I used 2 pressure sensor to control the x axis of the picture, which serves as a prototype for my final project.

Photo credit to: https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.cleanpng.com%2Fpng-happy-sheep-snake-vs-bricks-emoji-version-android-962446%2F&psig=AOvVaw3esHoZ2sgiUmful8rgMhqv&ust=1651316466550000&source=images&cd=vfe&ved=0CAoQjhxqFwoTCKix7fecufcCFQAAAAAdAAAAABAD

 

Arduino:

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

void loop() 
{
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);    
  Serial.print(sensor1);
  Serial.print(",");  // put comma between sensor values
  Serial.print(sensor2);
  Serial.println(); // add linefeed after sending the last sensor value
  delay(100);
}

Processing:

import processing.serial.*;
PImage photo;
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;
int x;
int r =50;

void setup() {
  size(800, 800);
  background(0);
  setupSerial();
  x = width/2;
  photo = loadImage("kisspng-happy-sheep-snake-vs-bricks-emoji-version-androi-sheep-5ac7a6a8007676.7413455415230337680019.jpg");
  photo.resize(r, r);
}

void draw() {
  background(0);
  getSerialData();
  printArray(sensorValues);

  if (sensorValues[0]>600) {
    x=x+20;
  }
  if (sensorValues[1]>600) {
    x=x-20;
  }
  image(photo, x, height-r);

  if (x>width-r) {
    x=width-r;
  }
  if (x<0) {
    x=0;
  }
}

void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[ 2 ], 9600);
  myPort.clear();
  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

The difficulty is that I am not very familiar with envelope and difference function so that there were a lot of details that needs to be changed

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

import processing.sound.*;
SinOsc sine;
Env env;
float attackTime = 0.001;
float sustainTime = 0.004;
float sustainLevel = 0.3;
float releaseTime = 0.4;
int s = 20;

void setup() {
  size(640, 480);
  printArray(cameras);
  cam = new Capture(this, cameras[0]);
  cam.start();
  sine = new SinOsc(this);
  env  = new Env(this); 
  //sine.play();
}
void draw() {
  background(0);
  if (cam.available()) {
    cam.read();
  }
  color c = cam.get(width/2, height/2);
  b = blue(c);
  float difference = abs(b-pb);
  for (int i=0; i<width; i = i + s) {
    for (int j=0; j<height; j = j + s) {
      float size = map(b, 0, 255, 5, 80);
      color all =cam.get(i, j);
      fill(all);
      noStroke();
      rect(i, j, size, size);
    }
  }
  if (difference>10) {
    sine.play();
    sine.freq(map(b, 0, 255, 100, 800));
    env.play(sine, attackTime, sustainTime, sustainLevel, releaseTime);
  }
  pb=b;
}

April 29, Jingqiao, Younian Liu

Leave a Reply

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