arduino w/ switch function

arduino code

processing code
import FaceDetect.*;
import processing.net.*;
import processing.serial.*;
import processing.video.*;

Client client;
boolean netSendInProgress = false;

FaceDetect fd;
//Capture cam;

int MAX = 100;

int[] x2 = new int[MAX];
int[] y2 = new int[MAX];
int[] r = new int[MAX];
int[][] Faces = new int[MAX][3];
int switchvalue = 0;

PImage b;

String filename = “yourface”; // don’t use .jpg extension
String url = “http://itp.nyu.edu/~js960/thesis/website/uploadjpg.php”;

boolean takingPicture = false;

Capture myCapture;
int a=50; //stepsize x
int c=1; //stepsize y
int x=0;
int xres = 480;
int yres = 360;
int s = second();

Serial[] myPorts = new Serial[4];  // Create a list of objects from Serial class

//rgb value to use for color per each circle
int[] rgbValuesA = new int[3];         // a list to hold data from the serial ports
int[] rgbValuesB = new int[3];
int[] rgbValuesC = new int[3];
int[] rgbValuesD = new int[3];

PFont myFont;     // The display font:
String inString;  // Input string from serial port:
int lf = 10;      // ASCII linefeed

int clearVal=0;

int redVal = 0;
int greenVal = 0;
int blueVal = 0;

void setup() {
size(1280,800);
smooth();
b = loadImage(“logo.jpg”);

myCapture = new Capture(this, xres, yres, 25);
fd = new FaceDetect(this);
fd.start(“haarcascade_frontalface_alt.xml”, xres,yres,25);
stroke(255,200,0);

noFill();

// Make your own font. It’s fun!
myFont = loadFont(“ArialMT-36.vlw”);
textFont(myFont, 18);
// List all the available serial ports:
//println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
// myPort = new Serial(this, Serial.list()[0], 9600);

// get the ports’ names:
String portOne = Serial.list()[0];
String portTwo = Serial.list()[2];
String portThree = Serial.list()[4];
String portFour = Serial.list()[6];

// open the ports:
myPorts[0] = new Serial(this, portOne, 9600);
myPorts[1] = new Serial(this, portTwo, 9600);
myPorts[2] = new Serial(this, portThree, 9600);
myPorts[3] = new Serial(this, portFour, 9600);

myPorts[0].bufferUntil(lf);
myPorts[1].bufferUntil(lf);
myPorts[2].bufferUntil(lf);
myPorts[3].bufferUntil(lf);

}

void captureEvent(Capture myCapture) {
myCapture.read();
}

/*JPGMakerUploader jpger = new JPGMakerUploader(img,filename,url);
jpger.saveJPG();
jpger.upload();
noLoop();*/

void draw() {
background(255);

image(myCapture, 270, 40);

if (!takingPicture)
{

loadPixels();
filter (GRAY);

//fill(#B69041);
text(“COLOR VALUES”,780,60);
text(“forehead: ‘” + rgbValuesA[0]+”,”+rgbValuesA[1]+”,”+rgbValuesA[2] + “‘”, 780, 90);
text(“left cheek: ‘” + rgbValuesB[0]+”,”+rgbValuesB[1]+”,”+rgbValuesB[2] + “‘”, 780, 120);
text(“right cheek: ‘” + rgbValuesC[0]+”,”+rgbValuesC[1]+”,”+rgbValuesC[2] + “‘”, 780, 150);
text(“nose: ‘” + rgbValuesD[0]+”,”+rgbValuesD[1]+”,”+rgbValuesD[2] + “‘”, 780, 180);

text(“1. Wear a mask in front of you”,780,220);
text(“2. Wait 3 seconds and take off mask”,780,250);
text(“3. Face the monitor and don’t let your face get out of boundary. “,780,280);
text(“4. Click appropriate buttons below”,780,310);

stroke(255);

//face detect
Faces = fd.face(myCapture);
int count = Faces.length;
//    println(count);
if (count>0) {
for (int i = 0;i<count;i++) {
x2[i] = Faces[i][0];
y2[i] = Faces[i][1];
r[i] = Faces[i][2] * 2;

fill(rgbValuesA[0], rgbValuesA[1], rgbValuesA[2]);
ellipse(x2[i]+260, y2[i] – 40, 20, 20);
//left cheeck
fill(rgbValuesB[0], rgbValuesB[1], rgbValuesB[2]);
ellipse(x2[i]+220, y2[i]+60, 20, 20);
//right cheek
fill(rgbValuesC[0], rgbValuesC[1], rgbValuesC[2]);
ellipse(x2[i]+330, y2[i]+50, 20, 20);
//nose
fill(rgbValuesD[0], rgbValuesD[1], rgbValuesD[2]);
ellipse(x2[i]+270, y2[i]+40, 20, 20);
// println(i);
}
}

}
else{
saveFrame(“####_new.jpeg”);
takingPicture = false;
}

//average rgb colors
int avgR = (rgbValuesA[0] + rgbValuesB[0] + rgbValuesC[0]) + rgbValuesD[0] / 4;
int avgG = (rgbValuesA[1] + rgbValuesB[1] + rgbValuesC[1]) + rgbValuesD[0] / 4;
int avgB = (rgbValuesA[2] + rgbValuesB[0] + rgbValuesC[2]) + rgbValuesD[0] / 4;

color avg = color(avgR,avgG,avgB);

fill(avg,100);
ellipse(40,40,40,40);

}

void serialEvent(Serial thisPort) {
// variable to hold the number of the port:
int portNumber = -1;

// iterate over the list of ports opened, and match the
// one that generated this event:
for (int p = 0; p < myPorts.length; p++) {
if (thisPort == myPorts[p]) {
portNumber = p;
println(“portnumber = “+portNumber);
}
}

// read a byte from the port:
inString = (thisPort.readString());
inString = inString.trim();
String[] vals = split(inString, “,”);
// switchvalue = vals[3];  add the switch to each arduino and then uncomment
if(switchvalue == 0){// if the switch is in the 0 position, read and record the color sensor values
switch(portNumber)
{
case 0:
rgbValuesA[0] = int(vals[0]);
rgbValuesA[1] = int(vals[1]);
rgbValuesA[2] = int(vals[2]);
switchvalue = int(vals[3]);
println(“Got ” + rgbValuesA[0] + “,” + rgbValuesA[1] + “,” + rgbValuesA[2] + ” from serial port ” + portNumber);
break;

case 1:
rgbValuesB[0] = int(vals[0]);
rgbValuesB[1] = int(vals[1]);
rgbValuesB[2] = int(vals[2]);
switchvalue = int(vals[3]);
println(“Got ” + rgbValuesB[0] + “,” + rgbValuesB[1] + “,” + rgbValuesB[2] + ” from serial port ” + portNumber);
break;

case 2:
rgbValuesC[0] = int(vals[0]);
rgbValuesC[1] = int(vals[1]);
rgbValuesC[2] = int(vals[2]);
switchvalue = int(vals[3]);
println(“Got ” + rgbValuesC[0] + “,” + rgbValuesC[1] + “,” + rgbValuesC[2] + ” from serial port ” + portNumber);
break;

case 3:
rgbValuesD[0] = int(vals[0]);
rgbValuesD[1] = int(vals[1]);
rgbValuesD[2] = int(vals[2]);
switchvalue = int(vals[3]);
println(“Got ” + rgbValuesD[0] + “,” + rgbValuesD[1] + “,” + rgbValuesD[2] + ” from serial port ” + portNumber);
break;

default:
println(“Unable to determine port number”);
break;
}
}
else {
println(“switch is off”);
switchvalue = int(vals[3]);
}
}
void send2PHP(int portNumber, int[] rgb)
{
if(!netSendInProgress)
{
netSendInProgress = true;

String hostName = “itp.nyu.edu”;
String scriptLocation = “/”;

client = new Client(this, hostName, 80);

client.write(“GET ” + scriptLocation + “?colorSensor=” + portNumber + “&r=” + rgb[0] + “&g=” + rgb[1] + “&b=” + rgb[2] + “HTTP/1.1\n”);
client.write(“HOST: ” + hostName + “\n\n”);

println(“Uploaded”);

client.stop();
netSendInProgress = false;
}
}

void mousePressed() {

takingPicture = true;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

May 1, 2009. thesis.

Trackback URI