Turn on a webcam with sensors - Processing + Arduino

Turn on/off a webcam using a photocell (Arduino + Processing)

What you will need:

Circuit

Arduino Code

/* Photocell simple sketch. 

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground 
Connect LED from pin 11 through a resistor to ground 
For more information see http://learn.adafruit.com/photocells */

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider

void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
}

void loop(void) {
  photocellReading = analogRead(photocellPin);  

  //Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading

  delay(100);
}


Processing Code

import processing.serial.*;
import processing.video.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
int number;
String sensor2;

Capture video;

void setup(){
  size(600, 400);
  String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600); 

  video = new Capture(this, 640,480,30);
  video.start();
}

void draw()
{
  background(0);
  if ( myPort.available() > 0) 
  {
   val = myPort.readStringUntil(13);
   if (val != null) {
      sensor2 = trim(val);
      number = Integer.parseInt(sensor2);
   }
  } 
  if(number < 400){
    println(number); //print it out in the console
    if(video.available()){
      video.read();
    }
    image(video, 0,0);
  }

}