Changing video in Processing using a sensor and an Arduino

edited May 2015 in Arduino

I'm currently in the middle of creating a book on which the content is projected down rather than printed on to the pages. This is because I want to incorporate media like video and animation in to the project.

I'm using an ultrasonic sensor to tell the Arduino if a page is turning or not. It sends out a signal which is displayed in the Serial Monitor as either PageTurn or NoPageTurn respectively. This is the Arduino code:

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  if (distance >= 400 || distance <= 2){
    Serial.println("Out of range");
  }
  else if (distance >=3 && distance <=100){
    Serial.print(distance);
    Serial.println(" cm");
    Serial.println("PageTurn");
  }

   else {
    Serial.print(distance);
    Serial.println(" cm");
    Serial.println("NoPageTurn");
  }
  delay(2000);

}

The Arduino code is working perfectly. The serial port displays either PageTurn or NoPageTurn depending on whether not the page of the physical book is turned.

I want to bring this in to Processing but I am limited in knowledge on the subject. My aim is to use Processing to display video, but I can't get the video to display properly. This is my Processing code: import processing.video.*; Movie myMovie;

void setup() {
  size(200, 200);
  myMovie = new Movie(this, "ch1.mov");
  myMovie.play();
}

void draw() {
  image(myMovie, 0, 0);
}

void movieEvent(Movie m) {
  m.read();
}

Questions

  1. How do I make the video display through Processing?

  2. How can I use the sensor to change the video that's playing, when it displays "PageTurn" in the serial port in Arduino. The pages of the book are all .mov files, labelled ch1.mov - ch6.mov. When the page is turned, I want to change the video that's projected on to the page. This is done through Processing I assume, but I can't figure out how.

  3. Also, I'm going to need to display the video in full screen mode, in order for it to be projected properly. I'm considering using the GSVideo Library for this, but I don't know how to go about this.

  4. I come from a design background so all of this is new to me, if I should be using something other than Processing to do the video stuff please let me know.

Thanks for any help you can provide on the subject.

Answers

  • edited May 2015

    I'm actually just sniffing around the internet looking for ways to control video with Arduino input and stumbled upon your post. I'm by no means an expert as I've just begun experimenting with the combination Arduino - Processing. (My first goal is to control the speed and frame position of videos - so far I've got code working that makes a turning knob, potentiometer, controlling the playback speed). What I've learned so far might be helpful in your case so bear with me as I might be talking a little chaotic/blurry/ ...:

    Question1: To make video play in Processing: the code above looks about right, just place your videofile(s) inside a folder called 'data' next to your *.pde (or you could even use an internet address, e.g. "http://nojpeg.org/img/NoJPEG.jpg") Also make sure that Processing is listening to the Arduino by selecting the right serial port. You can use this code to list available ports, and afterwards changing the number between the brackets to the right port (start counting at zero, so if your port is listed as the fourth change the number between the brackets to 3: [3]) So run the code once, change the port to the right number.

    //import the serial library:
    import processing.serial.*;
    //also put this above void setup to create the variable:
    Serial myPort;
    //this piece goes into the void setup: 
    println("Available serial ports");
    println(Serial.list());
    myPort = 
      new Serial(this, Serial.list()[4],9600);
    

    Question 2: In your Arduino code use Serial.write instead of Serial.print e.g. my code for a potentiometer looks like this:

    void setup() {
      Serial.begin(9600);
    }
    void loop() {
      Serial.write(analogRead(A0)/4);
      delay(1);
    }
    

    What I've found was that the delay might be too fast for Processing, the solution I found somewhere was to put this into the processing code:

    myPort.clear();

    Question 3: I haven't tried this before, but maybe just put your image size to the resolution of your screen. a quick lookup learns me that this code can be used, not tested yet though:

    size(displayWidth, displayHeight);

    Question 4: On my not so very fast laptop I found my experiment rather laggy, hence why I'm looking at other projects and tips. So no idea whether Processing is the right tool.

    For reference here's my full Processing code:

        //accumulated from the demos provided by Processing and project 14 from the Arduino starterkit
        import processing.serial.*;
        import processing.video.*;
    
        Serial myPort;
    
        Movie myMovie;
    
        int rate = 0;
    
        void setup(){
    
          println("Available serial ports");
          println(Serial.list());
          myPort = 
            new Serial(this, Serial.list()[4],9600);
    
          size(1920,1080);
          background(0);
          myMovie = new Movie(this,"transit.mov");
          myMovie.loop();
    
        }
    
        void draw() {
    
          if (myPort.available() > 0) {
           rate = myPort.read();
           myPort.clear();
           println(rate);
    
          }
    
          if (myMovie.available()) {
            myMovie.read();
          }
    
        image(myMovie, 0, 0, width, height);
    
          float newSpeed = map(rate, 255, 0, 0.1, 5);
          myMovie.speed(newSpeed);
    
          fill(255);
          text(nfc(newSpeed, 2) + "X", 10, 30);
    
        }
    
  • hello,

    I am trying to find out the same process, did it work with you guys?

Sign In or Register to comment.