Lab Assignment - Photobooth!

Submitted by Jenton on Sun, 02/24/2013 - 17:10

 

Parts used:

  • 2 pots
  • 1 FSR
  • Arduino Uno
  • Breadboard
  • Lots of cable
  • Cardboard pedal for the FSR

Description:

I used code to activate the Macbook webcam in Processing. The two pots control the X and Y position of the hipster glasses. And the FSR controls the camera shutter (which is really just stopping and starting the camera, while saving the frame to the hard drive).

For the physical aspect, I created a control box so you can turn the pot and press a button to activate the FSR (the shutter). 

Code:

import processing.video.*;
import processing.serial.*;
 
//float fgcolor;
float xpos = 0, ypos = 0; //starting position of the glasses
 
//Image File
PImage img;
 
// camera stuff
Capture cam;
int cam_on = 1;
int time;
 
//timer for camera shutter
int lastTime;
final int DISPLAY_TIME = 1000; // 1000 ms = 1 seconds
 
//serial stuff
String portname = Serial.list()[0]; // or "COM5"
Serial port;
 
void setup() {
  size(800, 600);
  String[] cameras = Capture.list();
  println(Serial.list());
 
  //glasses image
  img = loadImage("glasses.gif");
 
  //camera stuff
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } 
  else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
 
    // The camera stuff
    cam = new Capture(this, cameras[0]);
    cam.start();
  }      
  port = new Serial(this, portname, 9600);
  port.bufferUntil('\n');
}
 
void draw() {
 
  if (cam.available() == true) {
    cam.read();
    /* 
     // the following code was supposed to flip the video capture on the x-axis, but I couldn't get it to work
     pushMatrix();
     //translate(-cam.width,0);
     scale(-1,1);
     image(cam, 0, 0);
     popMatrix();*/
  }
 
  image(cam, 0, 0); //camera
  image(img, xpos, ypos); //glasses
}
 
// called whenever serial data arrives
void serialEvent(Serial p) {
  // read the serial buffer:
  String myString = port.readStringUntil('\n');
  if (myString != null) {
    myString = trim(myString);
    //split the string at the commas
    // and convert the sections into integers:
    int sensors[] = int(split(myString, ','));
    // print out the values you got:
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      print("Sensor " + sensorNum + ": " + sensors[sensorNum] + " ");
    }
    // add a linefeed after all the sensor values are printed:
    println();
    println(sensors.length);
    ///*
    // make sure there are three values before you use them:
    if (sensors.length == 3) {
 
      //when you press the force sensor, you have to wait 1 second before you can press it again
      //pressing the sensor will start or stop the camera. When the camera stops, it saves the frame as a png -Jenton
      //I used this code for the ideahttp://wiki.processing.org/w/I_display_images_in_sequence_but_I_see_only_the_last_one._Why%3F            
      if (sensors[0] >= 500) {
        if (millis() - lastTime >= DISPLAY_TIME) { //if that formula is greater than Display_Time, then code will run
          if (cam_on == 1) {
            int time = millis() - lastTime;
            cam.stop();
            saveFrame("output/frames####.png");
            cam_on = 0;
            println("time="+time);
            println("Camera Stop");
            lastTime = millis();
          } 
          else if (cam_on == 0) {
            int time = millis() - lastTime;
            cam.start();
            cam_on = 1;
            println("time="+time);
            println("Camera Start");
            lastTime = millis();
          }
        }
      }
      //controls the glasses
      xpos = map(sensors[1], 0, 1023, 0, width);
      ypos = map(sensors[2], 0, 1023, 0, height);
    }//*/
  }
}
 
glasses!
The control box
0
Your rating: None
Drupal theme by Kiwi Themes.