{"id":20,"date":"2006-10-08T20:23:05","date_gmt":"2006-10-09T01:23:05","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/processing\/20"},"modified":"2007-08-06T23:23:35","modified_gmt":"2007-08-07T04:23:35","slug":"serial-quicktime-movie-controller","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/20\/","title":{"rendered":"Serial Quicktime Movie Controller"},"content":{"rendered":"<p>This Processing sketch controls the playback of a Quicktime movie using serial data.  To use it, send a serial value from 0 &#8211; 255.  The movie position will be set based on that value.  0 = beginning of movie, 255 = end of movie.  It doesn&#8217;t matter what&#8217;s sending the serial data, as long as the value is between 0 and 255.\n<\/p>\n<p><!--more--><\/p>\n<pre>\n\/* \n Serial Movie controller \n Language: Processing\n \n Reads a value from 0 - 255 from the serial port.  Then uses that \n value to control playback of a quicktime movie.\n \n by Tom Igoe\n Created 8 Oct. 2006\n Modified:\n \n *\/\n\n\nimport processing.serial.*;\nimport processing.video.*;\n\nSerial myPort;        \/\/ The serial port\nint sensorValue;      \/\/ value of the byte received\n\nMovie myMovie;        \/\/ variable to store an instance of the movie library\nfloat movieDuration;  \/\/ duration of the movie you're using\n\n\nvoid setup() {\n  \/\/ set the window size:\n  size(200,200);\n\n  \/\/ List all the available serial ports\n  println(Serial.list());\n\n  \/\/ I know that the first port in the serial list on my mac\n  \/\/ is always my  Arduino module, so I open Serial.list()[0].\n  \/\/ Change the 0 to the appropriate number of the serial port\n  \/\/ that your microcontroller is attached to.\n  myPort = new Serial(this, Serial.list()[0], 9600);\n\n  \/\/ set the framerate for this sketch:\n  framerate(30);\n\n  \/\/ fill in your own movie here.  Place it in a folder called \"Data\"\n  \/\/ inside your sketch folder:\n  myMovie = new Movie(this, \"station.mov\");\n  myMovie.loop();\n  \/\/ get the movie duration:\n  movieDuration = myMovie.duration();\n}\n\nvoid draw() {\n  background(0);\n\n  \/\/ if there's a movie to read, read it:\n  if(myMovie.available()) {\n    myMovie.read();\n  }\n\n  \/\/ calculate the movie position based on the value from the sensor.\n  \/\/ the sensor value can range from 0 - 255, so dividing the sensorValue\n  \/\/ by 255 and multiplying by the duration always gives some fraction of\n  \/\/ the total movie duration:\n  float moviePosition = ((float)sensorValue \/ (float)255) * movieDuration;\n\n  \/\/ jump to the calculated movie position:\n  myMovie.jump(moviePosition); \n\n  \/\/ draw the movie:\n  image(myMovie, 0, 0);\n}\n\n\n\/\/ serialEvent  method is run automatically by the Processing applet\n\/\/ whenever the buffer reaches the  byte value set in the bufferUntil() \n\/\/ method in the setup():\n\nvoid serialEvent(Serial myPort) { \n  \/\/ read the serial buffer:\n  sensorValue = myPort.read();\n} \n<\/pre>\n<p>Here&#8217;s a simple Wiring\/Arduino program to send data to this Processing program. Attach a potentiometer or other analog sensoe to  analog input 0:\n<\/p>\n<pre>\nvoid setup() {\n   \/\/ open the serial port at 9600 bits per second:\n   Serial.begin(9600);\n}\n\nvoid loop() {\n   \/\/ read the potentiometer, reduce the range to 0 - 255 by dividing by 4:\n   int analogValue = analogIn(0) \/4;\n\n   \/\/ send the value out the serial port:\n   Serial.print(analogValue, BYTE);\n   \n   \/\/ delay 10 milliseconds so the analog to digital converter settles:\n   delay(10);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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]&#8230;.  Place it in a folder called &#8220;Data&#8221; \/\/ inside your sketch folder: myMovie = new Movie(this, &#8220;station.mov&#8221;); myMovie.loop(); \/\/ get the movie duration: movieDuration = myMovie.duration(); } void draw() { background(0); \/\/ if there&#8217;s a movie to read, read it: if(myMovie.available()) { myMovie.read(); } \/\/ calculate the movie position based on the value from the sensor.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,7],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}