donderdag 18 december 2014

Webcam controlled fan

A simple interface to switch a circuit with a webcam (no direct use at all)

Processing is 'controlling' the webcam, when key-c is pressed a picture is taken and the 'dark pixels' are counted. When more then 11000 dark pixels, a serial signal is send to the Arduino who switches the tranistor to low. When key-r is pressed, the Arduino gets a signal and the transistor is switched high again.


Arduino sketch:

int pin12 = 12;
int numReceived;

void setup(){
  Serial.begin(9600);
  pinMode(pin12, OUTPUT);
  digitalWrite(pin12, HIGH);
}

void loop(){
  if (Serial.available() > 0){
    numReceived = Serial.read();
  }
  if (numReceived == 125){
    digitalWrite(pin12, LOW);
  } else {
    digitalWrite(pin12, HIGH);
  }
}

Processing sketch:

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

Capture cam;
PImage img;
color pixelColor;
Serial myPort;

void setup() {
  size(640, 480, P3D);
  myPort = new Serial(this, Serial.list()[0], 9600);

  String[] cameras = Capture.list();
  
  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]);
    }
    println("Press c to capture || Press r restart");
    
    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[1]);
    cam.start();
  }  
}

void draw() {
  if (cam.available() == true) {
    cam.read();  
  }
  image(cam, 0, 0);
}

void keyPressed(){
  if (key == 'r' || key == 'R'){
    cam.start();
    loop();
    int restart = 0;
    myPort.write(restart);
  }
  
  if (key == 'c' || key == 'C') {
    noLoop();
    img = get (0, 0, width, height);
    color dark = color(30, 30, 30);
    color black = color(0, 0, 0);
    color white = color(255, 255, 255);
    color pixelColor;
    int countDark = 0;
    int dimension = width * height;
    img.loadPixels();
    for (int i = 0; i < dimension; i ++) {
      pixelColor = img.pixels[i];
      if (pixelColor <= dark){
        img.pixels[i] = black;
        countDark++;
      } else {
        img.pixels[i] = white;
      }
    }
    img.updatePixels();
    img.save("test");
    cam.stop();
    PImage photo = loadImage("test.tif");
    image(photo, 0, 0);
    println("Dark: "+countDark);
    if (countDark > 11000){
      int stop = 125;
      myPort.write(stop);
      println("byte:"+stop);
    }      
  }
}

Geen opmerkingen:

Een reactie posten