Divine Intervention Switch v2

For our computer vision/image manipulation assignment, I chose to go back to the basics and produce another iteration of my handless switch where an LED is turned on whenever God and Adam’s hand touch.

Adam’s hand is linked to a color-tracker coded in Processing, such that whenever the color being tracked comes near God’s hand, Processing communicates with the Arduino to turn on the LED. In order to do this, I rely on serial communication. Moreover, whenever Adam’s hand touches God’s ‘Hallelujah,’ from Handel’s ‘Messiah,’ plays in the background.

In order to do this project, I relied on Dan Shiffman’s tutorials, Aaron’s color-tracking example code, and the MINIM library for playing sound files in Processing.


####### ARDUINO CODE

const int ledPin1 = 3;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("0,0");

pinMode(3,OUTPUT);

}

void loop() {
while(Serial.available()){
int switchState=Serial.read();
if(switchState==1){
digitalWrite(ledPin1,HIGH);
delay(100);
} else{
digitalWrite(ledPin1,LOW);
}
}
}

####### PROCESSING CODE

import processing.video.*;
import processing.serial.*;
import ddf.minim.*;
Capture video;
Serial myPort;
PImage adam;
PImage hand;
color trackColor;
int locX, locY;
boolean touch=false;
Minim minim;
AudioPlayer hallelujah;

void setup() {
size(640, 480);
video = new Capture(this, 640, 480, 30);
adam = loadImage("creation_adam1.png");
hand = loadImage("adam_hand.png");
video.start();

minim = new Minim(this);
hallelujah = minim.loadFile( "hallelujah_short.mp3");

printArray(Serial.list());
String portname=Serial.list()[5];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil('\n');

}

void draw() {
if (video.available()) {
video.read();
}
video.loadPixels();
float dist=500;
for (int y=0; y<height; y++) {
for (int x=0; x<width; x++) {
int loc = (video.width-x-1)+(y*width);
color pix=video.pixels[loc];
float r1=red(pix);
float g1=green(pix);
float b1=blue(pix);
float r2=red(trackColor);
float g2=green(trackColor);
float b2=blue(trackColor);
float diff=dist(r1,g1,b1,r2,g2,b2);

if (diff<dist){
dist=diff;
locX=x;
locY=y;
}
}
}
video.updatePixels();
pushMatrix();
translate(width,0);
scale(-1,1);
image(video,0,0);
popMatrix();
fill(trackColor);
ellipse(locX,locY,30,30);
image(hand, 30,100, locX, locY);
image(adam, 100, 50, 600, 480);

stroke(255,255,255,0);
fill(255,255,255,0);
ellipse(325,270,20,20);

if(dist(locX,locY,325,270)<20){
touch=true;
println("1");
if (hallelujah.isPlaying())
{
println("audio is playing");
} else {
hallelujah.play();
hallelujah.rewind();
}
}
else {
touch=false;
println("0");
}

myPort.write(int(touch));

}

void mousePressed(){
int loc=(video.width-mouseX-1)+(mouseY*width);
trackColor=video.pixels[loc];
}
//void serialEvent(Serial myPort){
// String s = myPort.readStringUntil('\n');
// s =trim(s);
// println(s);
// if(s!=null){
// int value[]=int(split(s,','));
// if(value.length==2){
// }
// }
// myPort.write(int(touch));
//}