The code finally worked at 1 am. I had tried several different ways of merging the movie manipulation code with the call and response method that was working with simple color manipulation with all of the sensors, but something was going wrong. I will try again, but the standard serial communication worked fine after a few attempts. There is a tint effect based on 3 flex sensors and one fsr controlling tint strength, a speed change based on another fsr, a video/channel switch also from another fsr, which happens when that one reaches a value above 700. Here is the Processing code:
import processing.serial.*;
Serial myPort;
import jmcvideo.*;
import processing.opengl.*;
import javax.media.opengl.*;
int sensor1;
int sensor2=0;
int sensor3=0;
int sensor4;
int sensor5=0;
int sensor6;
int vidNum = 0;
float movieRate=0;
float movieAlpha=0;
JMCMovieGL myMovie;//from JMC example, load movie string
int pvw, pvh;
String[] vids = new String[]{"static.mov","sledding.mov",
"slide.mov","californiadreams.mov", "commercials.mov",
"gummibears.mov","wwf.mov"};
void setup() {
size(800,600, OPENGL);
frame.setResizable(true);
background(0);
myMovie = movieFromDataPath(vids[vidNum]);
myMovie.loop();
println(Serial.list());//establish serial comm.
myPort = new Serial(this, Serial.list()[0], 9600);
//read bytes into a buffer until you get a linefeed:
myPort.bufferUntil('\n');
}
void draw()
{ //background based on 3 sensor values:
background(map(sensor2,0,1000,0,255),
map(sensor3,250,500,0,255),
map(sensor5,100,900,0,255));
//draw the movie:
PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
GL gl = pgl.beginGL();
{
if (pvw != width || pvh != height)
{
background(0);
gl.glViewport(0, 0, width, height);
pvw = width;
pvh = height;
}
//change the transparency based on sensor4:
movieAlpha=map(sensor4,0,900,255,1);
myMovie.centerImage(gl);
myMovie.alpha=(1f*movieAlpha/255f);
//change the movie rate based on sensor1:
movieRate=map(sensor1,0,1000,.2,2);
myMovie.setRate(movieRate);
}
pgl.endGL();
//switch video when sensor 4 is on high:
if(sensor4>700){
vidNum = (vidNum + 1) % vids.length;
myMovie.switchVideo(vids[vidNum]); }else{
myMovie.loop();}
}
//get sensor values from serial port:
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length;
sensorNum++) {
print("Sensor " + sensorNum + ": " +
sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
sensor1=sensors[0];
sensor2=sensors[1];
sensor3=sensors[2];
sensor4=sensors[3];
sensor5=sensors[4];
sensor6=sensors[5];
}
}
}
JMCMovieGL movieFromDataPath(String filename)
{
return new JMCMovieGL(this, filename, RGB);
}
int analogOne = 0; // analog input
int analogTwo = 1; // analog input
int analogThree = 2;
int analogFour=3;
int analogFive=4;
int analogSix=5;
int digitalOne = 2;
int sensorValue = 0; // reading from the sensor
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(digitalOne, INPUT);
}
void loop() {
// read the sensor:
sensorValue = analogRead(analogOne);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(analogTwo);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(analogThree);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
sensorValue = analogRead(analogFour);
Serial.print(sensorValue, DEC);
Serial.print(",");
sensorValue = analogRead(analogFive);
Serial.print(sensorValue, DEC);
Serial.print(",");
sensorValue = analogRead(analogSix);
Serial.print(sensorValue, DEC);
Serial.print(",");
//sensorValue = digitalRead(digitalOne);
// print the last sensor value with a println()
Serial.println(sensorValue, DEC);
}
.
.
.
.
.
.
.
.
The physical Part :
I began by making the sensors we were interested in using talk to the computer. We had ordered some sensors we wanted to play with: flex sensors, a larger force sensing resistor, accelerometers, light sensors, and a proximity sensor.
I wired the sensors so that we have their output ranges, and their circuit needs, and a general serial communication code. Based on the last lab, I wired them and read them as an array in processing, but this time using the call and response method to foresee any issues with delay because of the media processing.
They are wired in this order:
0. long force sensing resistor
1. flex sensing resistor
2. flex sensing resistor
3. accelerometer
4. ambient light sensor
5. infrared proximity sensor
digital 2. switch
Arduino sketch for 6 analog inputs and one switch:
int analogOne = 0; // analog input int analogTwo = 1; int analogThree = 2; int analogFour=3; int analogFive=4; int analogSix=5; int digitalOne = 2; // digital input
int sensorValue = 0; // reading from the sensor
And the processing sketch receiving 7 variables, still just using a few of the readings to move the ball from the lab, but we will transform this sensor to variable output to control our media.
boolean firstContact=false;
import processing.serial.*;
Serial myPort;
PFont f;
float bgcolor;
float fgcolorR,fgcolorG,fgcolorB;
float xpos, ypos;
void setup() {
size(800,480);
f=createFont("Arial",16,true);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
if(firstContact==false){
if(myString.equals("hello")){
myPort.clear();
firstContact=true;
myPort.write('A');
}
}
else{
int sensors[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < sensors.length;
sensorNum++) {
print("Sensor " + sensorNum + ": " +
sensors[sensorNum] + "\t");
background(203,0,57);
textFont(f);
fill(0);
textAlign(LEFT);
text("sensor1: " + sensors[0] + " sensor2: "+
sensors[1] +" sensor3: " +sensors[2]+
" sensor4: " +sensors[3]+ " sensor5: " +
sensors[4]+ " sensor6:"+sensors[5]+
" switch: " +sensors[6], width/10, height/4);
}
println();
}
myPort.write('A');
}
}
void draw() {}





