Serial Quicktime Movie Controller

This Processing sketch controls the playback of a Quicktime movie using serial data. To use it, send a serial value from 0 – 255. The movie position will be set based on that value. 0 = beginning of movie, 255 = end of movie. It doesn’t matter what’s sending the serial data, as long as the value is between 0 and 255.

/* 
 Serial Movie controller 
 Language: Processing
 
 Reads a value from 0 - 255 from the serial port.  Then uses that 
 value to control playback of a quicktime movie.
 
 by Tom Igoe
 Created 8 Oct. 2006
 Modified:
 
 */


import processing.serial.*;
import processing.video.*;

Serial myPort;        // The serial port
int sensorValue;      // value of the byte received

Movie myMovie;        // variable to store an instance of the movie library
float movieDuration;  // duration of the movie you're using


void setup() {
  // set the window size:
  size(200,200);

  // 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  Arduino module, so I open Serial.list()[0].
  // Change the 0 to the appropriate number of the serial port
  // that your microcontroller is attached to.
  myPort = new Serial(this, Serial.list()[0], 9600);

  // set the framerate for this sketch:
  framerate(30);

  // fill in your own movie here.  Place it in a folder called "Data"
  // inside your sketch folder:
  myMovie = new Movie(this, "station.mov");
  myMovie.loop();
  // get the movie duration:
  movieDuration = myMovie.duration();
}

void draw() {
  background(0);

  // if there's a movie to read, read it:
  if(myMovie.available()) {
    myMovie.read();
  }

  // calculate the movie position based on the value from the sensor.
  // the sensor value can range from 0 - 255, so dividing the sensorValue
  // by 255 and multiplying by the duration always gives some fraction of
  // the total movie duration:
  float moviePosition = ((float)sensorValue / (float)255) * movieDuration;

  // jump to the calculated movie position:
  myMovie.jump(moviePosition); 

  // draw the movie:
  image(myMovie, 0, 0);
}


// serialEvent  method is run automatically by the Processing applet
// whenever the buffer reaches the  byte value set in the bufferUntil() 
// method in the setup():

void serialEvent(Serial myPort) { 
  // read the serial buffer:
  sensorValue = myPort.read();
} 

Here’s a simple Wiring/Arduino program to send data to this Processing program. Attach a potentiometer or other analog sensoe to analog input 0:

void setup() {
   // open the serial port at 9600 bits per second:
   Serial.begin(9600);
}

void loop() {
   // read the potentiometer, reduce the range to 0 - 255 by dividing by 4:
   int analogValue = analogIn(0) /4;

   // send the value out the serial port:
   Serial.print(analogValue, BYTE);
   
   // delay 10 milliseconds so the analog to digital converter settles:
   delay(10);
}