CODE:
Arduino Code:::::::::::::::
//* Vanity Mirror Sonar Sensor to Processing
#define LIVE_VIDEO_TIME 10 /* how long live video lasts in seconds */
#define BLINK_DELAY1 10
#define BLINK_DELAY2 20
int ledPin = 13; // pin for the LED
int sonarSensor = 0; // analog pin for the sonarSensor
int ledStatus = LOW; // we use a variable to toggle the LEDs state
int IR = 2; // Infrared input
int val = 0; // variable to store the potentiometer's reading
int irval = 0;
int BLED = 9;
long smootheValue = 0; // to smoothen out analog value
void setup() {
pinMode(IR, INPUT);
pinMode(BLED, OUTPUT);
pinMode(ledPin, OUTPUT); // declare the ledPin as an output
Serial.begin(9600); // initialize the serial port
}
boolean send_live_video = false;
long live_video_triggered_time = 0;
void loop() {
// read the potentiometer's value and store it
irval = digitalRead(IR);
/*
Serial.print("irval is ");
Serial.println(irval,DEC);
Serial.print("send_live_video is ");
Serial.println(send_live_video,DEC);
Serial.print("live video triggerd time is = ");
Serial.println(live_video_triggered_time,DEC);
*/
if ( irval == 1 && send_live_video == false) {
send_live_video = true;
live_video_triggered_time = millis()/1000; // convert mills to seconds
}
if ( send_live_video == true ) {
if ( millis()/1000 - live_video_triggered_time > LIVE_VIDEO_TIME) {
send_live_video = false;
}
}
// send the value over the port
if ( send_live_video == true)
{
//Serial.println(irval);
Serial.print(68, BYTE);
} else {
smootheValue = 0;
for(int i = 0; i < 10; i++){
smootheValue += analogRead(sonarSensor);
//delayMillisecond(
}
smootheValue = smootheValue/10;
// change the state of the LED (if HIGH, then LOW, and viceversa)
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
if (smootheValue >= 140 && smootheValue <= 255)
{
//Serial.println(smootheValue);
Serial.print(65, BYTE);
digitalWrite(BLED, HIGH);
}
if (smootheValue >= 30 && smootheValue <= 129)
{
//Serial.println(smootheValue);
Serial.print(66, BYTE);
//digitalWrite(BLED, 50);
/* digitalWrite(BLED, LOW);
delay(BLINK_DELAY1);
digitalWrite(BLED, HIGH);
delay(BLINK_DELAY2);
digitalWrite(BLED, LOW);
delay(BLINK_DELAY1);
digitalWrite(BLED, HIGH);
delay(BLINK_DELAY2); */
}
if (smootheValue >= 0 && smootheValue <= 25)
{
// Serial.println(smootheValue);
Serial.print(67, BYTE);
/* digitalWrite(BLED, LOW);
delay(BLINK_DELAY1);
digitalWrite(BLED, HIGH);
delay(BLINK_DELAY1);
digitalWrite(BLED, LOW);
delay(BLINK_DELAY1);
digitalWrite(BLED, HIGH);
delay(BLINK_DELAY1); */
}
}
// wait a bit
delay(100);
}
Processing Code::::::::::::
// Video Mirror Designed by Allistar D. Peters & Youn Ji Choi
import krister.Ess.*;
import processing.video.*;
import processing.serial.*;
AudioChannel myChannel1;
AudioChannel myChannel2;
AudioChannel myChannel3;
//Variable declarations
int inByte = 0;
int serialData = 0;
int serialCount = 0; // A count of how many bytes we receive
boolean firstContact = false; // Whether we've heard from the microcontroller
//input boolean statemnts for A B C D
boolean Aallowed = true;
boolean Ballowed = true;
boolean Callowed = true;
boolean Dallowed = true;
int[] go = new int[4];
Serial port; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
float yspeed = 1.0; //Speed of pixels falling
float grav = 0.1; //Gravity of pixel falling
int numPixels;
int blockSize = 10;
int cellsize = 30; // Dimensions of each cell in the grid
int COLS, ROWS; // Number of columns and rows in our system
boolean captureMode = false;
boolean movieMode = false;
Capture myCapture;
PImage p; //Setting pixel image For Live Capture transistion
int px, py; //Declaring x and y for Pimage
Movie movies;
color myMovies[];
int[] a = new int[10]; //set up array for random clips at the beginning
int[] clip_end = new int[10]; // array for clip end time
void setup()
{
a[0] = 1; // looking
a[1] = 5; // yawn
a[2] = 21; // crossedEyed
a[3] = 32; // lookingAT
a[4] = 36; // lookinghappybday
a[5] = 90; // leaveironon
a[6] = 65; // LookiIntoEyes
a[7] = 73; // latking2me
a[8] = 83; // Shallow
a[9] = 59; // LikeMyFrame
clip_end[0] = 4;
clip_end[1] = 20;
clip_end[2] = 31;
clip_end[3] = 35;
clip_end[4] = 58;
clip_end[5] = 127;
clip_end[6] = 72;
clip_end[7] = 82;
clip_end[8] = 89;
clip_end[9] = 64;
size(640, 480, P3D);
//size(screen.width, screen.height);
frameRate(30);
//Ess.start();
/*
myChannel1=new AudioChannel("man I love.aif");
myChannel1.stop();
myChannel2=new AudioChannel("Neruda.aif");
myChannel2.stop();
myChannel3=new AudioChannel("getakick.aif");
myChannel3.stop();
*/
movies = new Movie(this, "/Users/allistarpeters/Documents/Processing/vanity_MirrorFinal6/data/vanity mirror1A.mov");
movies.loop();
myCapture = new Capture(this, Capture.list()[1],width,height,15);
COLS = width/cellsize; // Calculate # of columns
ROWS = height/cellsize; // Calculate # of rows
colorMode(RGB,255,255,255,100); // Setting the colormode
p = createImage(COLS,ROWS,RGB); //Breaking Image up into many peices
numPixels = width / blockSize;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
port.write(65); // Send a capital A to start the microcontroller sending
}
//Enables the live video Capture
void captureEvent(Capture myCapture)
{
myCapture.read();
}
int c = 0;
void draw()
{
//inverts the image on screen to reflect real mirrored image
translate(width/2,height/2);
rotateY(radians(180)); // flips image on Y axis
translate(-width/2,-height/2);
if (captureMode)
{
image(myCapture, 0, 0); // Image from camera
image(p,px,py*yspeed/2+grav);
//px++;
py++;
yspeed++;
}
else
{
image(movies, 0, 0); // Image from pre re-recorded clip
}
if(movies.available()) {
movies.read();
}
while (port.available() > 0)
{
serialEvent();
//have we heard from the microcontroller
firstContact = true;
}
if (firstContact == false)
{
port.write(65); // Send a capital A to start the microcontroller sending
// firstContact = true;
}
}
void serialEvent()
{
// if this is the first byte received,
// take note of that fact:
firstContact = true;
println ("in serialEvent");
processByte((char)port.read());
}
int something = 0;
boolean init = true;
boolean played_have_a_nice_day = false;
boolean movie_end () {
if ( init ) {
init = false;
return true;
}
if ( clip_end[something] > movies.time() ) {
return true;
}
else
return false;
}
void pick_movie () {
movieMode = true;
captureMode = false;
int something = (int) random(0,a.length); // random play of clips
println(something);
movies.jump(a[something]);
movies.speed(1.0); // used to play movie instead of .play() : bug in program
port.write(65);
// Reset serialCount:
serialCount = 0;
//println(65);
}
void processByte(char inByte)
{
serialCount++;
println(inByte); //prints teh incoming bytes A B C or D
if(inByte == 'C')
{
if(Callowed == true)
{
// println ("SerialCount==2");
movieMode = true;
captureMode = false;
movies.jump(156);
movies.speed(1.0); // used to play movie instead of .play() : bug in program
//inByte = (char)port.read();
Aallowed = true;
Ballowed = true;
Callowed = false;
Dallowed = true;
c = 0;
played_have_a_nice_day = false;
init = true;
}
}
else if(inByte == 'D') //switch to video cam
{
if(Dallowed == true)
{
p.copy(movies,0,0,width,height,0,0,width,height);
captureMode = true;
movieMode = false;
serialCount = 0;
c = 0 ;
if (movies != null)
{
println("stopping movie");
movies.speed(0.0); // used to pause movie instead of .stop() : bug in program
//inByte = (char)port.read();
Aallowed = true;
Ballowed = true;
Callowed = true;
Dallowed = false;
played_have_a_nice_day = false;
init = true;
}
}
} else if(inByte == 'A') {
if ( movie_end() )
pick_movie();
Aallowed = false;
Ballowed = true;
Callowed = true;
Dallowed = true;
played_have_a_nice_day = false;
//inByte = (char)port.read();
} else if(inByte == 'B') {
if(Ballowed == true)
{
movieMode = true;
captureMode = false;
movies.jump(139);
movies.speed(1.0); // used to play movie instead of .play() : bug in program
//inByte = (char)port.read();
Aallowed = true;
Ballowed = false;
Callowed = true;
Dallowed = true;
c = 0;
init = true;
}
else {
if ( movies.time() > 157 && played_have_a_nice_day == false ) {
movies.jump(213);
movies.speed(1.0);
played_have_a_nice_day = true;
}
}
if ( played_have_a_nice_day ) {
if ( movies.time() > 223 ) {
init = true;
processByte('A');
}
}
}
if (serialCount > 0 ) {
serialData = serialInArray[0];
// Send a capital A to request new sensor readings:
}
}