Lab 4

victorpras's picture

Lab 4 by Victor Tjhia

Description

Using force sensor to "explode" an image

Components Used

·         Resistor

·         Arduino

·         Solderless Board

·         Force sensor

 

/**
 * Explode
 * by Daniel Shiffman.
 * Modified by Victor Tjhia
 * Mouse horizontal location controls breaking apart of image and
 * Maps pixels from a 2D image into 3D space. Pixel brightness controls
 * translation along z axis.
 */
 
 import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM7"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
 
PImage img;       // The source image
int cellsize = 2; // Dimensions of each cell in the grid
int columns, rows;   // Number of columns and rows in our system

int value = 100;


void setup() {
  size(1600, 900, P3D);
  img = loadImage("eames.jpg");  // Load the image
  columns = img.width / cellsize;  // Calculate # of columns
  rows = img.height / cellsize;  // Calculate # of rows
  port = new Serial(this, portname, 9600);
}

void draw() {
  background(0);
  // Begin loop for columns
  for ( int i = 0; i < columns; i++) {
    // Begin loop for rows
    for ( int j = 0; j < rows; j++) {
      int x = i*cellsize + cellsize/2;  // x position
      int y = j*cellsize + cellsize/2;  // y position
      int loc = x + y*img.width;  // Pixel array location
      color c = img.pixels[loc];  // Grab the color
      // Calculate a z position as a function of mouseX and pixel brightness
      float z = (value * 3 / float(width)) * brightness(img.pixels[loc]) - 20.0;
      // Translate to the location, set fill and stroke, and draw the rect
      pushMatrix();
      translate(x + 200, y + 100, z);
      fill(c, 204);
      noStroke();
      rectMode(CENTER);
      rect(0, 0, cellsize, cellsize);
      popMatrix();
    }
  }
}


// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    int val = int(buf);
    println("val="+val);
    value = val;
    buf = "";
    
  }
}

sensor2.jpg
sensor1.jpg
sensor.jpg
0
Your rating: None