Processing not reacting properly to arduino via firmata

edited May 2017 in Arduino

Hi, I'm using an arduino uploaded with firmata. I have a potentiometer hooked up and as the user turns the potentiometer, depending on the value they land on, a certain video will play. So if they turn it up more a different video will play etc. What's happening is I have to turn the potentiometer to a value, and then turn it all the way back to zero and then the corresponding video from the first value will play. In between it's showing the "image" I'm calling as a part of the video, but usually only one of them, not all of them. I'm new to the video library and processing & arduino in general. Thanks for your help!

import processing.serial.*;

import cc.arduino.*;
import org.firmata.*;


import processing.video.*;

Arduino arduino;

Movie oneDegree, twoDegrees, threeDegrees;


int sensorPin = 0;    // select the input pin for the potentiometer
int val;



void setup() {
  size(960, 540);
  arduino = new Arduino(this, Arduino.list()[3], 57600); //sets up arduino
   val = arduino.analogRead(sensorPin); 
   println(Arduino.list());
   oneDegree = new Movie(this, "comp1.mp4");
   twoDegrees = new Movie(this, "comp2.mp4");
   threeDegrees = new Movie(this, "comp3.mp4");


}

void draw() {
 val = arduino.analogRead(sensorPin); 
 println(val);
 image(oneDegree, 0, 0);
 image(twoDegrees, 0, 0);
 image(threeDegrees, 0, 0);

  if(val > 10 && val < 348) { 
    oneDegree = new Movie(this, "comp1.mp4");
    oneDegree.play();
  }

  else if(val > 348 && val < 686) {
  twoDegrees = new Movie(this, "comp2.mp4");
    twoDegrees.play();
  }

  else if(val > 686 && val < 1024) {
   threeDegrees = new Movie(this, "comp3.mp4");
  threeDegrees.play();
  }

  if (oneDegree.time() == oneDegree.duration()) { //movie must be finished
twoDegrees.play();
  }
if (twoDegrees.time() == twoDegrees.duration()) { //movie must be finished
threeDegrees.play();
}
}

void movieEvent(Movie m) {
  m.read();
  /* if (m == oneDegree) {
    oneDegree.read();
  } 
  else if (m == twoDegrees) {
    twoDegrees.read();
  }
  else if (m == threeDegrees) {
    threeDegrees.read();
  }*/
}



/* void movieEvent(Movie ) {
  if (val <= 348) {
    oneDegree.read();
  } 
  else if (val > 348 && val <= 686) {
    twoDegrees.read();
  }

  else if (val > 686 && val <= 1024) {
    threeDegrees.read();
  }
} */

Answers

  • This next is untested code.

    Kf

    import processing.serial.*;
    import cc.arduino.*;
    import org.firmata.*;
    import processing.video.*;
    
    final int STATEnull = -1;
    final int STATE1=0;
    final int STATE2=1;
    final int STATE3=2;
    
    int prevVal=-1;
    int state=STATEnull;
    
    Arduino arduino;
    Movie oneDegree, twoDegrees, threeDegrees;
    
    int sensorPin = 0;    // select the input pin for the potentiometer
    int val;
    
    void setup() {
      size(960, 540);
      arduino = new Arduino(this, Arduino.list()[3], 57600); //sets up arduino
      val = arduino.analogRead(sensorPin); 
    
      oneDegree = new Movie(this, "comp1.mp4");
      twoDegrees = new Movie(this, "comp2.mp4");
      threeDegrees = new Movie(this, "comp3.mp4");
    }
    
    
    
    void draw() {
      val = arduino.analogRead(sensorPin); 
      println(val);
    
      switch(state) {
      case STATE1:
        image(oneDegree, 0, 0);
        break;
      case STATE2:   
        image(twoDegrees, 0, 0);
        break;
      case STATE3:
        image(threeDegrees, 0, 0);
        break;
      }
    
      if (state==STATE1 && oneDegree.time() == oneDegree.duration()) { //movie must be finished
        twoDegrees.play();
        state=STATE2;
      }
      if (state==STATE2 && twoDegrees.time() == twoDegrees.duration()) { //movie must be finished
        threeDegrees.play();
        state=STATE3;
      }
    
      //Only trigger video if there is a change in potentiometer
      if (prevVal==val){
        return;
      } 
      else{
        prevVal=val;  //Update
      }
    
      if (val > 10 && val < 348) { 
        oneDegree = new Movie(this, "comp1.mp4");
        oneDegree.play();
        state=STATE1;
      } else if (val > 348 && val < 686) {
        twoDegrees = new Movie(this, "comp2.mp4");
        twoDegrees.play();
        state=STATE2;
      } else if (val > 686 && val < 1024) {
        threeDegrees = new Movie(this, "comp3.mp4");
        threeDegrees.play();
        state=STATE3;
      } else {
        state=STATEnull;
      }
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
Sign In or Register to comment.