Advertisement
3rdWard_Arduino

class4_Processing code for Serial to Processing

Feb 15th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. /**
  2. * Loop Video with values sent from Arduino.
  3. *
  4. * Using an Arduino and Potentiometer, contro the playback speed of a video
  5. * Recieve values using Serial I/O library
  6. */
  7.  
  8. import processing.video.*;// import video library
  9. import processing.serial.*;// import serial library
  10.  
  11. Movie myMovie; // The movie object
  12. float timeJump = 0.0; // amount of time that is displaced when scrubbing the movie in reverse
  13. float timePrevious = 0.0; // the last position of the playhead
  14. float timeFuture = 0.0; // the future position of the playhead
  15.  
  16. float videoSpeed = 0.0; // the speed at which the video plays
  17. float maxSpeed = 5.0;
  18. float minSpeed = -5.0;
  19.  
  20. Serial myPort; // The serial port
  21.  
  22. void setup() {
  23. size(640, 480, P2D);
  24. background(0);
  25. // Load and play the video in a loop
  26. myMovie = new Movie(this, "station.mov");
  27. myMovie.loop();
  28. println(Serial.list());
  29. myPort = new Serial(this, Serial.list()[0], 9600);
  30. }
  31.  
  32. void movieEvent(Movie myMovie) {
  33. myMovie.read();
  34. }
  35.  
  36. void draw() {
  37. // tint(255, 30);
  38.  
  39. if(videoSpeed < 0) {
  40. timeJump = myMovie.time() - timePrevious;
  41. timeFuture = myMovie.time() + timeJump;
  42. if(timeFuture <= 0) {
  43. // we only need to ajust the playhead if the playhead goes past
  44. // the beginning of the movie in reverse
  45. float newBackwardsTime = myMovie.duration() + timeFuture;
  46. myMovie.jump(newBackwardsTime);
  47. }
  48. }
  49. myMovie.speed(videoSpeed);
  50. image(myMovie, 0, 0, width, height);
  51. timePrevious = myMovie.time();
  52. }
  53. /**
  54. * serialEvent
  55. *
  56. * This function access the values coming in from the serial port
  57. * It assigns the values to the play speed of the video, called videoSpeed
  58. * The videoSpeed has a range of -5x to +5x normal playing speed
  59. * These values are determined by the minSpeed and maxSpeed variables
  60. * If you alter them you can change the range of speed the video will play
  61. * For this to work, an Arduino must be sending one byte through the serial port
  62. */
  63. void serialEvent (Serial myPort) {
  64. // get the byte:
  65. int inByte = myPort.read();
  66. videoSpeed = map(inByte, 0, 255, minSpeed, maxSpeed);
  67. // print it:
  68. println(inByte);
  69. }
  70.  
  71. // This can be tested with using the keys if you are unable to access and Arduino and potentiometer
  72. // The UP key increases the speed and the DOWN key decreases the speed
  73. void keyPressed() {
  74. if(keyCode == UP) {
  75. videoSpeed += 0.1;
  76. videoSpeed = constrain(videoSpeed, minSpeed, maxSpeed);
  77. }
  78. if(keyCode == DOWN) {
  79. videoSpeed -= 0.1;
  80. videoSpeed = constrain(videoSpeed, minSpeed, maxSpeed);
  81. }
  82. println("adjusted videoSpeed with keys to: " + videoSpeed);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement