Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import blobscanner.*;
- import processing.video.*;
- import processing.serial.*;
- import org.firmata.*;
- import cc.arduino.*;
- // Include Networking things required for the hue
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPut;
- import org.apache.http.impl.client.DefaultHttpClient;
- import java.io.*;
- import java.awt.*;
- import java.lang.Object.*;
- import java.util.Date;
- /* EDIT THIS TO MATCH YOUR HUE BRIDGE */
- final static String HUE_KEY = "JorenBroekema";
- final static String HUE_IP = "192.168.1.101";
- PFont font;
- //Arduino related
- Arduino arduino;
- int sensorPin0 = 0;
- int sensorValue0;
- int sensorPin1 = 1;
- int sensorValue1;
- int led = 3;
- //Blob detection related
- Detector bd;
- color boundingBoxCol = color(255, 0, 0);
- int boundingBoxThickness = 1;
- int blobNum;
- // Capture device related
- Capture video;
- PImage prevFrame;
- // How different must a pixel be to be a "motion" pixel
- float threshold = 30;
- //Timer related for hue (10 fps)
- long startTime = 0;
- int frameDiff = 100;
- //Distance calculation related
- float distanceFromLamp1; float twoDistanceFromLamp1; float threeDistanceFromLamp1; float fourDistanceFromLamp1;
- float distanceFromLamp2; float twoDistanceFromLamp2; float threeDistanceFromLamp2; float fourDistanceFromLamp2;
- float distanceFromLamp3; float twoDistanceFromLamp3; float threeDistanceFromLamp3; float fourDistanceFromLamp3;
- float maxDistance = 220;
- boolean[] playerArray = new boolean[3];
- //Hue lamps
- int NUM_HUE_LAMPS = 7;
- int h; int h1; int h2; int h3;
- int s; int s1; int s2; int s3;
- int b; int b1; int b2; int b3;
- float[][] lampValues = new float[NUM_HUE_LAMPS][3];
- int lampIndex = 0;
- void setup()
- {
- size(640,480);
- video = new Capture(this, width, height, 30);
- // Create an empty image the same size as the video
- prevFrame = createImage(video.width,video.height,RGB);
- video.start();
- font = loadFont("Aharoni-Bold-24.vlw");
- textFont(font, 24);
- smooth();
- arduino = new Arduino(this, Arduino.list()[0], 57600);
- arduino.pinMode(sensorPin0, Arduino.INPUT);
- arduino.pinMode(sensorPin1, Arduino.INPUT);
- //arduino.pinMode(led, Arduino.OUTPUT);
- //arduino.analogWrite(led, 0);
- setupHue();
- }
- void draw()
- {
- for(int i = 0; i < NUM_HUE_LAMPS; i++)
- {
- lampValues[0][0] = h; lampValues[1][0] = h1; lampValues[2][0] = h2; lampValues[6][0] = h3;
- lampValues[0][1] = s; lampValues[1][1] = s1; lampValues[2][1] = s2; lampValues[6][1] = s3;
- lampValues[0][2] = b; lampValues[1][2] = b1; lampValues[2][2] = b2; lampValues[6][2] = b3;
- }
- if (millis() - startTime > frameDiff)
- {
- startTime = millis();
- sendToHue();
- }
- // Capture video
- if (video.available())
- {
- // Save previous frame for motion detection!!
- prevFrame.copy(video,0,0,video.width,video.height,0,0,video.width,video.height); // Before we read the new frame, we always save the previous frame for comparison!
- prevFrame.updatePixels();
- video.read();
- }
- loadPixels();
- video.loadPixels();
- prevFrame.loadPixels();
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ )
- {
- for (int y = 0; y < video.height; y ++ )
- {
- int loc = x + y*video.width; // Step 1, what is the 1D pixel location
- color current = video.pixels[loc]; // Step 2, what is the current color
- color previous = prevFrame.pixels[loc]; // Step 3, what is the previous color
- // Step 4, compare colors (previous vs. current)
- float r1 = red(current); float g1 = green(current); float b1 = blue(current);
- float r2 = red(previous); float g2 = green(previous); float b2 = blue(previous);
- float diff = dist(r1,g1,b1,r2,g2,b2);
- // Step 5, How different are the colors?
- // If the color at that pixel has changed, then there is motion at that pixel.
- // boolean isBlack = false; // Added
- if (diff > threshold)
- {
- // If motion, display black
- pixels[loc] = color(0);
- // isBlack = true;
- }
- else
- {
- // If not, display white
- pixels[loc] = color(255);
- }
- }
- }
- updatePixels();
- video.filter(THRESHOLD);
- bd = new Detector(this, 255);
- bd.findBlobs(video.pixels, video.width, video.height);
- bd.loadBlobsFeatures();
- bd.findCentroids();
- stroke(255, 100, 0);
- fill(120, 250, 0);
- ellipse(640, 480, 20, 20);
- ellipse(0, 0, 20, 20);
- ellipse(640, 0, 20, 20);
- ellipse(0, 480, 20, 20);
- for(int i = 0; i < bd.getBlobsNumber(); i++)
- {
- //print(bd.getBlobsNumber());
- stroke(0, 255, 0);
- strokeWeight(5);
- //...and draws a point to their location.
- point(bd.getCentroidX(i), bd.getCentroidY(i));
- if (bd.getBlobsNumber() == 1)
- {
- playerArray[0] = true; playerArray[1] = false; playerArray[2] = false; // only 1 player active
- //distance from lamp 1
- distanceFromLamp1 = dist(640, 480, bd.getCentroidX(0), bd.getCentroidY(0));
- //distance from lamp 2
- twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- //distance from lamp 3
- threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- //distance from lamp 4
- fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
- }
- if ( bd.getBlobsNumber() > 1 )
- {
- playerArray[0] = true; playerArray[1] = true; playerArray[2] = false; // only 2 players active
- distanceFromLamp1 = dist(640, 480, bd.getCentroidX(0), bd.getCentroidY(0));
- distanceFromLamp2 = dist(640, 480, bd.getCentroidX(1), bd.getCentroidY(1));
- //distance from lamp 2
- twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- twoDistanceFromLamp2 = dist(0, 0, bd.getCentroidX(1), bd.getCentroidY(1));
- //distance from lamp 3
- threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- threeDistanceFromLamp2 = dist(640, 0, bd.getCentroidX(1), bd.getCentroidY(1));
- //distance from lamp 4
- fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
- fourDistanceFromLamp2 = dist(0, 480, bd.getCentroidX(1), bd.getCentroidY(1));
- }
- if ( bd.getBlobsNumber() > 2 )
- {
- playerArray[0] = true; playerArray[1] = true; playerArray[2] = true; // only 3 players active
- //distance from lamp 1
- distanceFromLamp1 = dist(250, 250, bd.getCentroidX(0), bd.getCentroidY(0));
- distanceFromLamp2 = dist(250, 250, bd.getCentroidX(1), bd.getCentroidY(1));
- distanceFromLamp3 = dist(250, 250, bd.getCentroidX(2), bd.getCentroidY(2));
- //distance from lamp 2
- twoDistanceFromLamp1 = dist(0, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- twoDistanceFromLamp2 = dist(0, 0, bd.getCentroidX(1), bd.getCentroidY(1));
- twoDistanceFromLamp3 = dist(0, 0, bd.getCentroidX(2), bd.getCentroidY(2));
- //distance from lamp 3
- threeDistanceFromLamp1 = dist(640, 0, bd.getCentroidX(0), bd.getCentroidY(0));
- threeDistanceFromLamp2 = dist(640, 0, bd.getCentroidX(1), bd.getCentroidY(1));
- threeDistanceFromLamp3 = dist(640, 0, bd.getCentroidX(2), bd.getCentroidY(2));
- //distance from lamp 4
- fourDistanceFromLamp1 = dist(0, 480, bd.getCentroidX(0), bd.getCentroidY(0));
- fourDistanceFromLamp2 = dist(0, 480, bd.getCentroidX(1), bd.getCentroidY(1));
- fourDistanceFromLamp3 = dist(0, 480, bd.getCentroidX(2), bd.getCentroidY(2));
- }
- }
- if (distanceFromLamp1 <= maxDistance || distanceFromLamp2 <= maxDistance || distanceFromLamp3 <= maxDistance)
- {
- if (distanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- distanceFromLamp1 = maxDistance;
- }
- if (distanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- distanceFromLamp2 = maxDistance;
- }
- if (distanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- distanceFromLamp3 = maxDistance;
- }
- if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
- {
- b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0));
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
- {
- b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp2, 0, maxDistance, 255, 0));
- if(b >= 255)
- {
- b = 255;
- }
- println(b);
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
- {
- b = int(map(distanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(distanceFromLamp3, 0, maxDistance, 255, 0));
- if(b >= 255)
- {
- b = 255;
- }
- println(b);
- }
- }
- if (twoDistanceFromLamp1 <= maxDistance || twoDistanceFromLamp2 <= maxDistance || twoDistanceFromLamp3 <= maxDistance)
- {
- if (twoDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- twoDistanceFromLamp1 = maxDistance;
- }
- if (twoDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- twoDistanceFromLamp2 = maxDistance;
- }
- if (twoDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- twoDistanceFromLamp3 = maxDistance;
- }
- if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
- {
- b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0));
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
- {
- b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp2, 0, maxDistance, 255, 0));
- if(b1 >= 255)
- {
- b1 = 255;
- }
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
- {
- b1 = int(map(twoDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(twoDistanceFromLamp3, 0, maxDistance, 255, 0));
- if(b1 >= 255)
- {
- b1 = 255;
- }
- }
- }
- if (threeDistanceFromLamp1 <= maxDistance || threeDistanceFromLamp2 <= maxDistance || threeDistanceFromLamp3 <= maxDistance)
- {
- if (threeDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- threeDistanceFromLamp1 = maxDistance;
- }
- if (threeDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- threeDistanceFromLamp2 = maxDistance;
- }
- if (threeDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- threeDistanceFromLamp3 = maxDistance;
- }
- if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
- {
- b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0));
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
- {
- b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp2, 0, maxDistance, 255, 0));
- if(b2 >= 255)
- {
- b2 = 255;
- }
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
- {
- b2 = int(map(threeDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(threeDistanceFromLamp3, 0, maxDistance, 255, 0));
- if(b2 >= 255)
- {
- b2 = 255;
- }
- }
- }
- if (fourDistanceFromLamp1 <= maxDistance || fourDistanceFromLamp2 <= maxDistance || fourDistanceFromLamp3 <= maxDistance)
- {
- if (fourDistanceFromLamp1 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- fourDistanceFromLamp1 = maxDistance;
- }
- if (fourDistanceFromLamp2 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- fourDistanceFromLamp2 = maxDistance;
- }
- if (fourDistanceFromLamp3 >= maxDistance) // make sure that if they're out of range they are defined as being maximum distance away from the lamp (0 influence) else they'll receive negative values
- {
- fourDistanceFromLamp3 = maxDistance;
- }
- if(playerArray[0] == true && playerArray[1] == false && playerArray[2] == false)
- {
- b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0));
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == false)
- {
- b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp2, 0, maxDistance, 255, 0));
- if(b3 >= 255)
- {
- b3 = 255;
- }
- }
- if(playerArray[0] == true && playerArray[1] == true && playerArray[2] == true)
- {
- b3 = int(map(fourDistanceFromLamp1, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp2, 0, maxDistance, 255, 0)) + int(map(fourDistanceFromLamp3, 0, maxDistance, 255, 0));
- if(b3 >= 255)
- {
- b3 = 255;
- }
- }
- }
- sensorValue0 = arduino.analogRead(sensorPin0);
- h = int(map(sensorValue0, 0, 1023, 0, 255));
- sensorValue1 = arduino.analogRead(sensorPin1);
- h1 = int(map(sensorValue1, 0, 1023, 0, 255));
- s = s1 = s2 = s3 = 255;
- h2 = h3 = 166;
- }
- void sendToHue(){
- sendHSBToHue(lampIndex+1, int(lampValues[lampIndex][0]), int(lampValues[lampIndex][1]), int(lampValues[lampIndex][2]));
- lampIndex++;
- if(lampIndex >= NUM_HUE_LAMPS)
- {
- lampIndex = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement