Set distance value with ultrasonic sensor

edited November 2017 in Arduino

I want to set the distance from the ultrasonic sensor. For instance, I want to play a video between 0 and 100 cm. Also I stop this video 100 between 250. But I did not write this code part. Can you help me pls ?? :( (I wrote arduino code but I stuck in processing part )

Answers

  • Break this down into two problems

    1) Reading data from Arduino

    2) Loading, starting and stopping video.

    Start with task (1) there are loads of examples showing data being read from Aurduino. Once you can read your sensor data and you understand the code necessary to do that you can move onto task 2.

    BTW your specification is incomplete because you don't say what you want to happen when the distance from the sensor is greater than 250cm ;)

  • edited November 2017

    I did these 2 steps. I read data from Arduino.

    This is my processing code. I still have a problem on set value (also I don't have much knowledge about processing)

    import processing.serial.*;
    import processing.video.*;
    Movie [] mov = new Movie [4];
    Serial myPort;
    
    int val;
    
    void setup() {
    size(1080,720);
    background(0);
    noStroke();
    
    
      mov[1] = new Movie(this, "myMovie1.mov");
      mov[2] = new Movie(this, "myMovie2.mov");
      mov[3] = new Movie(this, "myMovie3.mov");
    
       mov[1].loop();
       mov[2].play();
       mov[3].play();
    
     println(Serial.list());
     // print a list of all available ports 
    
     myPort = new Serial(this, Serial.list()[2], 9600);
    
     // choose the port to which the Arduino is connected
     // on the PC this is usually COM1, on the Macintosh
     // this is usually tty.usbserial-XXX
    }
    
    void draw(){
     if ( myPort.available () > 0 )
     {
           val = myPort.read();
      }
      println(val);
    
      if (val >0 && val<=100)  {
        image(mov[2], 0, 0);
    
      } else if (val > 100){
          image(mov[3], 0, 0);
    
        }
    }
    
    void stopVideos(){
      mov[1].stop();
      mov[2].stop();
      mov[3].stop();
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
  • edited November 2017

    Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    You can also check previous relevant posts: https://forum.processing.org/two/search?Search=ultrasonic

    Kf

  • I modified your code so it does not depend on your serial code. Instructions: You press either 0, 1 or 2 to change state. The first state is for idle, the second state plays the second movie and the third state plays the second movie. Please watch when you access your arrays. The first array position is at zero (0) and not 1 as shown in the example below. Also, you can use this example with your serial device. For that, you will read the serial and assign it to your val. you will need to call updateVal() ONLY If the new value changes to a different state in order to play another movie.

    Kf

    final int IDLE=0;
    final int STATE_CLOSE=1;
    final int STATE_FAR=2;
    final int N=3;  //Three states
    
    import processing.video.*;
    Movie [] mov = new Movie [N];
    
    
    int state;
    int val;
    
    void setup() {
      size(1080, 720);
      background(0);
      noStroke();
    
      state=IDLE;
      surface.setTitle("Current state: "+state);
      updateVal();
    
    
      mov[0] = new Movie(this, "shortrun.mp4");
      mov[1] = new Movie(this, "transit.mov");
      mov[2] = new Movie(this, "video720x480.mp4");
    }
    
    void draw() {
      background(0);
      if (val >0 && val<=100) {
        image(mov[1], 0, 0);
      } else if (val > 100) {
        image(mov[2], 0, 0);
      }
    }
    
    void keyPressed() {
    
      if (keyCode>='0' && keyCode<='2') {
        state=keyCode-'0';
        updateVal();
        surface.setTitle("Current state: "+state);
      }
    }
    
    
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void updateVal() {
    
      stopVideos();
    
      switch(state) {
      case IDLE:
        val=-1;
        break;
      case STATE_CLOSE:
        val=(int)random(0, 100);
        mov[1] = new Movie(this, "transit.mov");
        mov[1].loop();
        break;
      case STATE_FAR:
        val=(int)random(100, 250);
        mov[2] = new Movie(this, "video720x480.mp4");
        mov[2].loop();
        break;
      default:
        break;
      };
    }
    
    void stopVideos() {
    
      if(mov[0]!= null) mov[0].stop();
      if(mov[1]!= null) mov[1].stop();
      if(mov[2]!= null) mov[2].stop();
    }
    
Sign In or Register to comment.