Recitation 10: Media Controller by Kaycee

For this recitation, I intended to utilize the camera and pixel function that I learned in class and the infrared distance sensor to make a device, which can change the resolution of the camera by changing the distance that the infrared sensor senses.

I met some challenges in coding this program. The range of the infrared distance sensor that I used was 20 cm to 150 cm. In the first trial, I set the beginning number of the mapped range as 0 and it turned out that there was an error in the code. When I pressed the run button, only black canvas showed on my screen. Therefore, I changed the beginning range from 0 to 1 and this made the camera interface appeared on the canvas. The interface of the camera appeared unclear at first, after strengthening each connecting part,  the device finally produced the effect I wanted. When the user came close to the infrared sensor, the ellipse on the interface would become smaller, producing the effect that the camera became clearer. And when the distance that the sensor got became larger, the radius of the ellipse would increase which made the camera vague again.

After finish reading “Computer Vision for Artist and Designers”, I contemplated more about the “algorithm communication”. This is exactly how those brilliant works that made by computers come from. I also used this technology in my project. In fact, the code that I have written until now can all be concluded as algorithm communication, since they all rely on simple codes to produce complicated effects.

Code:

Processing

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Arduino to Processing

import processing.serial.*;
import processing.video.*;
Capture cam;
int Size = 10;

Serial myPort;
int valueFromArduino;
int PORT_INDEX=5;

void setup() {
size(640, 480);
cam = new Capture (this, 640, 480);
cam.start();

printArray(Serial.list());
// this prints out the list of all available serial ports on your computer.

myPort = new Serial(this, Serial.list()[ PORT_INDEX ], 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.
}

void draw() {
if (cam.available()) {
cam.read();
cam.loadPixels();
}
while ( myPort.available() > 0) {
valueFromArduino = myPort.read();
}
int a = int(map (valueFromArduino,200,1500,1,10));
for (int y=0; y<cam.height; y+=a) {
for (int x=0; x<cam.width; x+=a) {
int i= y*cam.width+x;
fill( cam.pixels[i] );
noStroke();
ellipse(x, y, a, a);
}
}
//image(cam,0,0,width,height);
cam.updatePixels();

// to read the value from the Arduino

println(valueFromArduino);//This prints out the values from Arduino
}

Arduino

// IMA NYU Shanghai
// Interaction Lab
// This code sends one value from Arduino to Processing

void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}

void loop() {
Serial.write(sensorValue);
uint16_t sensorValue = analogRead (pin);
double distance = get_IR (sensorValue);
Serial.println (sensorValue);
Serial.println (” cm”);
Serial.println ();
delay (500);
delay(10);
double get_IR (uint16_t sensorValue) {
if (sensorValue < 16) sensorValue = 16;
return 2076.0 / (sensorValue – 11.0);
}

Leave a Reply